shithub: git9

Download patch

ref: ab14718d835ffae7265db405b89ea29b216de8a8
parent: 3261c06c0233588e24887f1bbfc1b8dfd4c647f9
author: Ori Bernstein <[email protected]>
date: Tue Aug 18 01:49:57 EDT 2020

convert to aux/getflags for arg parsing (thanks kvik)

--- a/add
+++ b/add
@@ -2,32 +2,20 @@
 rfork ne
 . /sys/lib/git/common.rc
 
-usage='
-	git/add [-r] file ...
-		-r        remove file
-'
-
 gitup
 
+flagfmt='r:remove'; args='file ...'
+eval `''{aux/getflags $*} || exec aux/usage
+
 add='tracked'
 del='removed'
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -r
-		add='removed'
-		del='tracked'
-	case *
-		usage
-	}
-	shift
+if(~ $remove 1){
+	add='removed'
+	del='tracked'
 }
-if(~ $1 --)
-	shift
 if(~ $#* 0)
-	usage
+	exec aux/usage
 
-if(~ $#rel 0)
-	rel=''
 if(~ $add tracked)
 	files=`$nl{walk -f $gitrel/$*}
 if not
--- a/branch
+++ b/branch
@@ -2,39 +2,15 @@
 rfork en
 . /sys/lib/git/common.rc
 
-usage='
-	git/branch [-cdsu] [-b base] [branch]
-		-b base   base new branch on "base" (default: current branch)
-		-n        create new branch, or overwrite existing branch
-		-d        delete branch
-		-s        create branch but stay on current one
-'
-
 gitup
 
-stay=()
-newbr=()
-delete=()
-baseref=()
+flagfmt='a:listall, b:baseref ref, d:delete, n:newbr, s:stay'
+args='[branch]'
+eval `''{aux/getflags $*} || exec aux/usage
+
 modified=()
 deleted=()
-listall=()
 
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -n; newbr=true
-	case -s; stay=true
-	case -b; baseref=$2; shift
-	case -d; delete=true
-	case -a; listall=true
-	case *
-		usage
-	}
-	shift
-}
-if(~ $1 --)
-	shift
-
 if(~ $#* 0){
 	if(~ $#listall 0)
 		awk '$1=="branch"{print $2}' < /mnt/git/ctl
@@ -43,19 +19,18 @@
 	exit
 }
 if(! ~ $#* 1)
-	usage
+	exec aux/usage
 
-if(~ $1 refs/heads/*)
-	new=$1
-if not if(~ $1 heads/*)
-	new=refs/$1
+branch=$1
+if(~ $branch refs/heads/*)
+	new=$name
+if not if(~ $branch heads/*)
+	new=refs/$branch
 if not
-	new=refs/heads/$1
-if (~ $#baseref 1){
-	base=`{git/query $baseref}
-	if(! ~ $status '')
-		exit badbase
-}
+	new=refs/heads/$branch
+
+if (~ $#baseref 1)
+	base=`{git/query $baseref} || exit 'bad base'
 if not if(test -e .git/$new)
 	base=`{git/query $new}
 if not
@@ -70,17 +45,18 @@
 		die 'uncommited changes would be clobbered'
 	}
 }
-if(! ~ $#delete 0){
+if(~ $delete 1){
 	rm -f .git/$new
+	echo 'deleted branch' $new
 	exit
 }
 if(~ $#newbr 0){
 	if(! test -e .git/$new)
-		die could not find branch $1
+		die could not find branch $branch
 	if(! ~ $#baseref 0)
-		die update would clobber $1 with $baseref
+		die update would clobber $branch with $baseref
 }
-commit=`{git/query $base || die 'branch does not exist:' $base}
+commit=`{git/query $base} || die 'branch does not exist:' $base
 echo updating $new to $commit
 echo $commit > .git/$new
 
--- a/clone
+++ b/clone
@@ -2,15 +2,14 @@
 rfork en
 . /sys/lib/git/common.rc
 
-usage='
-	git/clone remote [local]
-'
+flagfmt=''; args='remote [local]'
+eval `''{aux/getflags $*} || exec aux/usage
 
 remote=`{echo $1 | subst -g '/*$'}
 local=$2
 
 if(~ $#remote 0)
-	usage
+	exec aux/usage
 if(~ $#local 0)
 	local=`{basename $remote .git}
 
--- a/commit
+++ b/commit
@@ -2,10 +2,6 @@
 rfork ne
 . /sys/lib/git/common.rc
 
-usage='
-	git/commit files...
-'
-
 fn whoami{
 	name=`{git/conf user.name}
 	email=`{git/conf user.email}
@@ -120,18 +116,14 @@
 
 gitup
 
-msgfile=/tmp/git-msg.$pid
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -m
-		shift; echo $1 >$msgfile.tmp
-	}
-	shift
-}
-if(~ $1 --)
-	shift
+flagfmt='m:msg message'; args='file ...'
+eval `''{aux/getflags $*} || exec aux/usage
 if(~ $#* 0)
-	usage
+	exec aux/usage
+
+msgfile=/tmp/git-msg.$pid
+if(~ $#msg 1)
+	echo $msg >$msgfile.tmp
 
 files=`{git/walk -c `{cleanname $gitrel/$*}}
 if(~ $status '' || ~ $#files 0 && ! test -f .git/index9/merge-parents)
--- a/diff
+++ b/diff
@@ -2,25 +2,14 @@
 rfork ne
 . /sys/lib/git/common.rc
 
-usage='
-	git/diff [-c branch] [-s ] [file ...]
-'
-
 gitup
 
+flagfmt='c:commit branch, s:summarize'; args='[file ...]'
+eval `''{aux/getflags $*} || exec aux/usage
+
+if(~ $#commit 0)
+	commit=HEAD
 files=()
-summarize=()
-commit=HEAD
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -c; commit=$2; shift
-	case -s; summarize=1
-	case *; usage
-	}
-	shift
-}
-if(~ $1 --)
-	shift
 if(! ~ $#* 0)
 	files=`{cleanname $gitrel/$*}
 branch=`{git/query -p $commit}
--- a/export
+++ b/export
@@ -2,28 +2,20 @@
 rfork ne
 . /sys/lib/git/common.rc
 
-usage='
-	git/export [-o patchdir] [query]
-'
+patchname=/tmp/git.patchname.$pid
+patchfile=/tmp/git.patchfile.$pid
+fn sigexit{
+	rm -f $patchname $patchfile
+}
 
 gitup
-ramfs
 
-stdout='true'
-patchdir='.'
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -o
-		stdout='false'
-		shift; patchdir=$1
-		if(! test -d $patchdir)
-			mkdir -p $patchdir
-	}
-	shift
-}
-if(~ $1 --)
-	shift
+flagfmt='o:patchdir patchdir'; args='[query]'
+eval `''{aux/getflags $*} || exec aux/usage
 
+if(~ $#patchdir 1 && ! test -d $patchdir)
+	mkdir -p $patchdir
+
 q=$*
 if(~ $#q 0)
 	q=HEAD
@@ -60,18 +52,18 @@
 			
 			gsub("^[ 	]|[ 	]$", "", msg)
 			gsub("[/ 	]", "-", msg)
-			printf "%.4d-%s.patch", n, msg >"/tmp/patchname"
+			printf "%.4d-%s.patch", n, msg >ENVIRON["patchname"]
 			next
 		}
 		{print; printf "\n"}'
 		ape/diff -urN a b
-	} >/tmp/patchfile
-	if(~ $stdout true){
-		cat /tmp/patchfile
+	} >$patchfile
+	if(~ $#patchdir 0){
+		cat $patchfile
 		! ~ $n $m && echo
 	}
 	if not
-		mv /tmp/patchfile $patchdir/`{cat /tmp/patchname}
+		mv $patchfile $patchdir/`{cat $patchname}
 	n=`{echo $n + 1 | bc}
 }
 exit ''
--- a/import
+++ b/import
@@ -2,10 +2,6 @@
 rfork ne
 . /sys/lib/git/common.rc
 
-usage='
-	git/import [file ...]
-'
-
 diffpath=/tmp/gitimport.$pid.diff
 fn sigexit {
 	rm -f $diffpath
@@ -92,6 +88,9 @@
 }
 
 gitup
+
+flagfmt=''; args='file ...'
+eval `''{aux/getflags $*} || exec aux/usage
 
 patches=(/fd/0)
 if(! ~ $#* 0)
--- a/log
+++ b/log
@@ -2,39 +2,20 @@
 rfork en
 . /sys/lib/git/common.rc
 
-usage='
-	git/log [-e expr|-c commit] [-s] [file ...]
-'
-
 gitup
 
-base=/mnt/git/object/
+flagfmt='e:expr expression, c:base commit, s:short'; args='file ...'
+eval `''{aux/getflags $*} || exec aux/usage
+
+if(~ $#base 0)
+	base=/mnt/git/object/
 branch=`{git/branch}
-expr=()
-short=()
-files=()
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -c
-		base=$2
-		shift
-	case -e
-		expr=$2
-		shift
-	case -s
-		short=true
-	case *
-		usage
-	}
-	shift
-}
-if(~ $1 --)
-	shift
 if(~ $#expr 0)
 	commits=`{git/query $branch}
 if not
 	commits=`{git/query $expr}
 
+files=()
 if(! ~ $#* 0)
 	files=`"{walk -f $gitrel/^$* | subst '^\./' | sort}
 
@@ -54,7 +35,7 @@
 		commits=`$nl{walk -emp -n0 $base^$commits | sort -rn | uniq | awk -F/ '{print $NF}'}
 
 	if(~ $#files 0 || ~ $show true){
-		if(~ $short true)
+		if(~ $short 1)
 			echo $c `{cat $base/$c/msg | sed 1q}
 		if not{
 			echo -n 'Hash:	'`''{cat $base/$c/hash}
--- a/merge
+++ b/merge
@@ -2,10 +2,6 @@
 rfork ne
 . /sys/lib/git/common.rc
 
-usage='
-	git/merge theirs
-'
-
 fn merge{
 	ourbr=$1/tree
 	basebr=$2/tree
@@ -35,8 +31,11 @@
 
 gitup
 
+flagfmt=''; args='theirs'
+eval `''{aux/getflags $*} || exec aux/usage
+
 if(! ~ $#* 1)
-	usage
+	exec aux/usage
 
 theirs=`{git/query $1}
 ours=`{git/query HEAD}
--- a/pull
+++ b/pull
@@ -2,14 +2,6 @@
 rfork en
 . /sys/lib/git/common.rc
 
-usage='
-	git/pull [-u upstream] [-b branch]
-		-b       work with branch "branch"
-		-f       fetch updates without checking out
-		-i       display incoming commits
-		-u       pull from "upstream" (default "origin")
-'
-
 fn update{
 	branch=$1
 	upstream=$2
@@ -35,46 +27,24 @@
 
 gitup
 
-branch=refs/`{git/branch}
-remote=()
-incoming=true
-checkout='true'
-upstream=origin
+flagfmt='a:allbranch, b:branch branch, f:fetchonly,
+         u:upstream upstream, q:quiet'
+args=''
+eval `''{aux/getflags $*} || exec aux/usage
 
-while(~ $1 -*){
-	switch($1){
-	case -u
-		upstream=$2
-		remote=`{git/conf 'remote "'$upstream'".url'}
-		if(~ $#remote 0){
-			upstream=SOMEONE
-			remote=$2
-		}
-		shift
-	case -b
-		branch=$2
-		shift
-	case -a
-		branch=''
-	case -f
-		checkout=()
-	case -q
-		incoming=()
-	case *
-		usage
-	}
-	shift
-}
-if(! ~ $#* 0)
-	usage
+if(~ $#branch 0)
+	branch=refs/`{git/branch}
+if(~ $allbranch 1)
+	branch=''
 
+if(~ $#upstream 0)
+	upstream=origin
+remote=`{git/conf 'remote "'$upstream'".url'}
 if(~ $#remote 0)
-	remote=`{git/conf 'remote "'$upstream'".url'}
-if(~ $#remote 0)
 	die 'no remote to pull from'
 
 update $branch $upstream $remote
-if (~ $#checkout 0)
+if (~ $fetchonly 1)
 	exit
 
 local=`{git/branch}
@@ -99,7 +69,7 @@
 }
 # The remote is directly ahead of the local, and we have
 # no local commits that need merging.
-if(! ~ $#incoming 0)
+if(~ $quiet 0)
 	git/log -s -e $local'..'$remote >[1=2]
 echo
 echo $remote':' `{git/query $local} '=>' `{git/query $remote}  >[1=2]
--- a/push
+++ b/push
@@ -2,53 +2,32 @@
 rfork en
 . /sys/lib/git/common.rc
 
-usage='
-	git/push [-a] [-u upstream] [-b branch] [-r branch]
-		-a       push all branches
-		-b       push branch "branch" (default: current branch)
-		-r       delete branch "branch" from upstream
-		-u       push to remote "upstream" (default: origin)
-'
-
 gitup
 
-remote=()
-sendall=''
-remove=()
-force=()
-upstream='origin'
-branch=-b`{git/branch}
-while(~ $1 -*){
-	switch($1){
-	case -u
-		shift
-		upstream=$1
-	case -a
-		branch=-b`$nl{cd .git/refs/heads && walk -f}
-	case -b
-		shift
-		branch=(-b$1 $branch)
-	case -r
-		shift
-		remove=(-r$1 $remove);
-	case -f
-		force=-f
-	case *
-		usage
-	}
-	shift
-}
-
+flagfmt='a:pushall, b:branch branch, f:force,
+         r:remove remove, u:upstream upstream' args=''
+eval `''{aux/getflags $*} || exec aux/usage
 if(! ~ $#* 0)
-	usage
-if(~ $#remote 0)
-	remote=`{git/conf 'remote "'$upstream'".url'}
-if(~ $#remote 0)
-	remote=$upstream
-if(~ $#remote 0)
-	die 'no idea where to push'
+	exec aux/usage
+
+if(~ $pushall 1)
+	branch=`$nl{cd .git/refs/heads && walk -f}
 if(~ $#branch 0)
+	branch=`{git/branch}
+if(~ $#branch 0)
 	die 'no branches'
+if(~ $force 1)
+	force=-f
+
+if(~ $#upstream 0)
+	upstream=origin
+
+remote=`{git/conf 'remote "'$upstream'".url'}
+if(~ $#remote 0)
+	remote=$upstream
+branch=-b^$branch
+if(! ~ $#remove 0)
+	remove=-r^$remove
 updates=`$nl{git/send $force $branch $remove $remote || die $status}
 for(ln in $updates){
 	u=`{echo $ln}
--- a/revert
+++ b/revert
@@ -2,26 +2,14 @@
 rfork e
 . /sys/lib/git/common.rc
 
-usage='
-	git/revert [-c query] file...
-		-c        revert to commit "query" (default: HEAD)
-'
-
 gitup
 
+flagfmt='c:query query' args='file ...'
+eval `''{aux/getflags $*} || exec aux/usage
+
 commit=/mnt/git/HEAD
-while(~ $1 -* && ! ~ $1 --){
-	switch($1){
-	case -c;
-		commit=`{git/query -p $2}
-		shift
-	case *;
-		usage
-	}
-	shift
-}
-if(~ $#* 0)
-	usage
+if(~ $#query 1)
+	commit=`{git/query -p $commit}
 
 for(f in `$nl{cd $commit/tree/ && walk -f ./$gitrel/$*}){
 	mkdir -p `{basename -d $f}