shithub: git9

Download patch

ref: d5e2a8d00b4f769d56e567905c4c6689e803a49e
parent: 92eaddb3cdb3bd9921d9178b0d21ecc0771d76d1
author: kvik <[email protected]>
date: Thu Nov 14 22:05:51 EST 2019

This change factors out a common initialization sequence done by most
scripts into a shared gitup function that 1) determines if it was run
in a git repository (exiting if not), 2) stashes the repository root
path into a shared $gitroot variable, 3) changes directory to
$gitroot, and 4) runs git/fs.

git/add and git/revert are updated to make use of $gitroot, resulting
in some further cleanup.

Move git/fs init into common.rc; update scripts.

--- a/add
+++ b/add
@@ -7,6 +7,8 @@
 		-r        remove file
 '
 
+gitup
+
 add='tracked'
 del='removed'
 while(~ $1 -* && ! ~ $1 --){
@@ -22,16 +24,7 @@
 if(~ $1 --)
 	shift
 
-dir=`{pwd}
-base=`{git/conf -r}
-x=$status
-if(! ~ $x ''){
-	echo git/conf: $x `{pwd} >[1=2]
-	exit $x
-}
-
-cd $base
-rel=`{sed 's@^'$base'/*@@' <{echo $dir}}
+rel=`{sed 's@^'$gitroot'/*@@' <{echo $dir}}
 if(~ $#rel 0)
 	rel=''
 for(f in $*){
--- a/branch
+++ b/branch
@@ -9,11 +9,7 @@
 		-s        create a branch but stay on current one
 '
 
-if(! cd `{git/conf -r}){
-	exit 'not in git repository'
-	exit notgit
-}
-git/fs
+gitup
 
 stay=()
 create=()
--- a/commit
+++ b/commit
@@ -92,10 +92,9 @@
 	rm -f .git/index9/merge-parents
 }
 
+gitup
+
 msgfile=/tmp/git-msg.$pid
-if(! cd `{git/conf -r})
-	exit 'not in git repository'
-git/fs
 mkdir -p .git/refs
 if(git/walk -q){
 	echo no changes to commit >[1=2]
--- a/common.rc
+++ b/common.rc
@@ -5,3 +5,13 @@
 	>[2=1] echo -n 'usage:' $usage
 	exit 'usage'
 }
+
+fn gitup{
+	gitroot=`{git/conf -r >[2]/dev/null}
+	if(~ $#gitroot 0){
+		>[2=1] echo 'not a git repository'
+		exit 'not a git repository'
+	}
+	cd $gitroot
+	git/fs
+}
--- a/diff
+++ b/diff
@@ -7,11 +7,7 @@
 		-b        diff relative to branch "branch" 
 '
 
-if(! cd `{git/conf -r}){
-	echo 'not a git repository' >[1=2]
-	exit notgit
-}
-git/fs
+gitup
 
 while(~ $1 -* && ! ~ $1 --){
 	switch($1){
--- a/export
+++ b/export
@@ -6,9 +6,8 @@
 	git/export [query]
 '
 
-if(! cd `{git/conf -r})
-	exit 'not in git repository'
-git/fs
+gitup
+
 q=$*
 if(~ $#q 0)
 	q=HEAD
--- a/import
+++ b/import
@@ -6,9 +6,6 @@
 	git/import [file ...]
 '
 
-if(! cd `{git/conf -r})
-	exit 'not in git repository'
-
 fn die{
 	echo $patchname: $1 $2
 	exit $2
@@ -108,6 +105,8 @@
 		exit $st
 	}
 }
+
+gitup
 
 patches=(/fd/0)
 if(! ~ $#* 0)
--- a/log
+++ b/log
@@ -6,8 +6,9 @@
 	git/log [-q query] [file ...]
 '
 
+gitup
+
 base=/mnt/git/object/
-git/fs
 branch=`{git/branch}
 query=()
 
--- a/merge
+++ b/merge
@@ -25,13 +25,11 @@
 	}
 }
 
+gitup
+
 if(! ~ $#* 1)
 	usage
-if(! cd `{git/conf -r}){
-	echo 'not in git repository'
-	exit 'badrepo'
-}
-git/fs
+
 theirs=`{git/query $1}
 ours=`{git/query HEAD}
 base=`{git/query $theirs ^ ' ' ^ $ours ^ '@'}
--- a/pull
+++ b/pull
@@ -37,11 +37,8 @@
 	'
 }
 
-if(! cd `{git/conf -r}){
-	echo 'not in git repository' >[1=2]
-	exit notgit
-}
-git/fs
+gitup
+
 branch=refs/`{git/branch}
 remote=()
 incoming=()
--- a/push
+++ b/push
@@ -10,10 +10,7 @@
 		-u       push to remote "upstream" (default: origin)
 '
 
-if(! cd `{git/conf -r})
-	exit 'not in git repository'
-
-git/fs
+gitup
 
 remote=()
 sendall=''
--- a/revert
+++ b/revert
@@ -7,12 +7,14 @@
 		-c        revert to commit "query" (default: HEAD)
 '
 
+rel=`{pwd}
+gitup
+
 commit=/mnt/git/HEAD
-pfx=`{pwd}
 while(~ $1 -* && ! ~ $1 --){
 	switch($1){
 	case -c;
-		commit=`{git/query -p $2};
+		commit=`{git/query -p $2}
 		shift
 	case *;
 		usage
@@ -20,15 +22,9 @@
 	shift
 }
 
-if(! cd `{git/conf -r}){
-	echo not a git repository >[1=2]
-	exit notgit
-}
-pfx=`{echo $pfx | sed 's@^'^`{pwd}^'@@g'}
-if(~ $#pfx 0)
-	pfx=''
-git/fs
+rel=`{echo $rel | sed 's@^'$gitroot'/?@@'}
+if(~ $#rel 0)
+	rel=''
 
-for(f in `$nl{cd $commit/tree/ && walk -f ./$pfx/$*})
-	cp -- $commit/tree/$pfx/$f ./$pfx/$f
-
+for(f in `$nl{cd $commit/tree/ && walk -f ./$rel/$*})
+	cp -- $commit/tree/$f $f