shithub: git9

Download patch

ref: 406e308504e27954b2036ced7c75362ee347b567
parent: e2232a8ce3e56bd8bc0d6a83d82c28e49beacfec
author: glenda <glenda@9front.local>
date: Mon Sep 2 20:33:25 EDT 2019

Import git/import.

--- a/export
+++ b/export
@@ -33,7 +33,7 @@
 		
 		echo From $c
 		echo From: `{cat $cp/author}
-		echo Date: `{date -t `{mtime $cp/author}}
+		echo Date: `{date -m `{mtime $cp/author}}
 		echo Subject: [PATCH] `{sed 1q $cp/msg}
 		echo
 		sed '1d' $cp/msg
--- /dev/null
+++ b/import
@@ -1,0 +1,99 @@
+#!/bin/rc
+
+rfork ne
+
+nl='
+'
+if(! cd `{git/conf -r})
+	exit 'not in git repository'
+
+fn die{
+	echo $patchname: $1 $2
+	exit $2
+}
+
+fn apply @{
+	flag +e
+
+	git/fs
+	email=''
+	name=''
+	msg=''
+	parents='-p'^`{cat /mnt/git/HEAD/parent}
+	diffpath=/tmp/gitimport.$pid.diff
+	branch=`{awk '$1=="branch"{print $2}' < /mnt/git/ctl}
+	if(test -e /mnt/git/branch/$branch/tree)
+		refpath=.git/refs/$branch
+	if not if(test -e /mnt/git/object/$branch/tree)
+		refpath=.git/HEAD
+	if not
+		exit 'invalid branch '$branch
+
+	awk '
+	BEGIN{
+		state="begin"
+	}
+	state=="begin" && /^From[ \t]/ {
+		state="headers"
+	}
+	state=="headers" && /^From:/ {
+		sub(/^From:*[ \t]*/, "", $0);
+		name=$0;
+		email=$0;
+		sub(/<.*$/, "", name);
+		sub(/.*</, "", email);
+		sub(/>/, "", email);
+	}
+	state=="headers" && /^Date:/{
+		sub(/^Date:[ \t]*/, "", $0)
+		date=$0
+	}
+	state=="headers" && /^Subject:/{
+		sub(/^Subject: (\[PATCH\])*[ \t]*/, "", $0);
+		msg=msg $0 "\n"
+	}
+	state=="headers" && /^$/ {
+		state="body"
+	}
+	(state=="headers" || state=="body") && /^diff/ {
+		state="diff"
+		print > ENVIRON["diffpath"]
+	}
+	state=="body" {
+		print > "/env/msg"
+	}
+	state=="diff" {
+		print > ENVIRON["diffpath"]
+	}
+	END{
+		if(state != "diff")
+			exit("malformed patch: " state);
+		if(name == "" || email == "" || msg == "" || date=="")
+			print "missing headers"
+		printf "%s", name > "/env/name"
+		printf "%s", email > "/env/email"
+		printf "%s", msg > "/env/msg"
+		printf "%s", date > "/env/date"
+	}
+	'
+
+	date=`{seconds $date}
+	ape/patch -p1 < $diffpath
+	hash=`{git/save -n $name -e $email -m $msg -d $date $parents}
+	echo $hash > $refpath
+}
+
+fn import{
+	apply
+	st=$status
+	if(! ~ $st ''){
+		echo stdin: $st
+		exit $st
+	}
+}
+
+if(~ $#* 0){
+	import
+if not
+	for(f in $*)
+		import < $f
--- a/mkfile
+++ b/mkfile
@@ -17,6 +17,7 @@
 	commit\
 	diff\
 	export\
+	import\
 	init\
 	log\
 	merge\
@@ -24,6 +25,7 @@
 	push\
 
 OFILES=\
+	date.$O\
 	objset.$O\
 	ols.$O\
 	pack.$O\
--- a/save.c
+++ b/save.c
@@ -199,7 +199,7 @@
 
 
 void
-mkcommit(Hash *c, char *msg, char *name, char *email, Hash *parents, int nparents, Hash tree)
+mkcommit(Hash *c, char *msg, char *name, char *email, vlong date, Hash *parents, int nparents, Hash tree)
 {
 	char *s, h[64];
 	int ns, nh, i;
@@ -209,8 +209,8 @@
 	fmtprint(&f, "tree %H\n", tree);
 	for(i = 0; i < nparents; i++)
 		fmtprint(&f, "parent %H\n", parents[i]);
-	fmtprint(&f, "author %s <%s> %lld +0000\n", name, email, (vlong)time(nil));
-	fmtprint(&f, "committer %s <%s> %lld +0000\n", name, email, (vlong)time(nil));
+	fmtprint(&f, "author %s <%s> %lld +0000\n", name, email, date);
+	fmtprint(&f, "committer %s <%s> %lld +0000\n", name, email, date);
 	fmtprint(&f, "\n");
 	fmtprint(&f, "%s", msg);
 	s = fmtstrflush(&f);
@@ -232,13 +232,15 @@
 main(int argc, char **argv)
 {
 	Hash c, t, parents[Maxparents];
-	char *msg, *name, *email;
+	char *msg, *name, *email, *dstr;
 	int r, nparents;
+	vlong date;
 
-
 	msg = nil;
 	name = nil;
 	email = nil;
+	dstr = nil;
+	date = time(nil);
 	nparents = 0;
 	gitinit();
 	ARGBEGIN{
@@ -245,6 +247,7 @@
 	case 'm':	msg = EARGF(usage());	break;
 	case 'n':	name = EARGF(usage());	break;
 	case 'e':	email = EARGF(usage());	break;
+	case 'd':	dstr = EARGF(usage());	break;
 	case 'p':
 		if(nparents >= Maxparents)
 			sysfatal("too many parents");
@@ -251,11 +254,19 @@
 		if(resolveref(&parents[nparents++], EARGF(usage())) == -1)
 			sysfatal("invalid parent: %r");
 		break;
+	default:
+		usage();
 	}ARGEND;
 
 	if(!msg) sysfatal("missing message");
 	if(!name) sysfatal("missing name");
 	if(!email) sysfatal("missing email");
+	if(dstr){
+		date=strtoll(dstr, &dstr, 10);
+		if(strlen(dstr) != 0)
+			sysfatal("could not parse date %s", dstr);
+	}
+		
 	if(!msg || !name)
 		usage();
 
@@ -267,7 +278,7 @@
 		sysfatal("could not commit: %r\n");
 	if(r == 0)
 		sysfatal("empty commit: aborting");
-	mkcommit(&c, msg, name, email, parents, nparents, t);
+	mkcommit(&c, msg, name, email, date, parents, nparents, t);
 	print("%H\n", c);
 	exits(nil);
 }