ref: d03d4c6ef7978417e7f8833577db044375d9feed
parent: d5e2a8d00b4f769d56e567905c4c6689e803a49e
author: kvik <[email protected]>
date: Thu Nov 14 22:06:12 EST 2019
A common function die() is introduced -- it terminates the program after displaying on stderr a message gives as argument. The same message is used as program exit status. All scripts are updated to use the new function and some of the messages / exit statuses got changed too. Streamline fatal error handling in rc scripts.
--- a/branch
+++ b/branch
@@ -52,23 +52,14 @@
modified=`$nl{git/query -c HEAD $base | grep '^[^-]' | sed 's/^..//'}
deleted=`$nl{git/query -c HEAD $base | grep '^-' | sed 's/^..//'}
-if(! git/walk -q $modified $deleted){
- echo branch update would clobber changes >[1=2]
- exit dirty
-}
+if(! git/walk -q $modified $deleted)
+ die 'uncommited changes'
if(! ~ $#create 0){
- if(! test -e .git/$new){
- echo branch $new: does not exist >[1=2]
- exit noexist
- }
+ if(! test -e .git/$new)
+ die 'branch does not exist:' $new
}
if not{
- commit=`{git/query $base}
- st=$status
- if(! ~ $st ''){
- echo could not find branch $base':' $st >[1=2]
- exit notfound
- }
+ commit=`{git/query $base || die 'branch does not exist:' $base}
echo $commit > .git/$new
}
--- a/clone
+++ b/clone
@@ -14,11 +14,9 @@
if(~ $#local 0)
local=`{basename $remote .git}
-if(test -e $local){
- echo $local already exists >[1=2]
- exit exists
-}
-
+if(test -e $local)
+ die 'repository already exists:' $local
+
fn clone{
mkdir -p $local/.git
mkdir -p $local/.git/objects/pack/
@@ -57,10 +55,7 @@
print headhash > ".git/HEAD"
}
}
- '} |[3] tr '\x0d' '\x0a'
- x=$status
- if(! ~ $x '|')
- exit $0:^' '^$"x
+ '} |[3] tr '\x0d' '\x0a' || die $status
tree=/mnt/git/branch/heads/master/tree
echo checking out repository...
@@ -67,10 +62,8 @@
if(test -f .git/refs/remotes/origin/master){
cp .git/refs/remotes/origin/master .git/refs/heads/master
git/fs
- @ {builtin cd $tree && tar cif /fd/1 .} | @ {tar xf /fd/0}
- x=$status
- if(! ~ $x '')
- exit 'checkout: '^$"x
+ @ {builtin cd $tree && tar cif /fd/1 .} | @ {tar xf /fd/0} \
+ || die 'checkout failed:' $status
for(f in `$nl{walk -f $tree | sed 's@^'$tree'/*@@'}){
if(! ~ $#f 0){
idx=.git/index9/tracked/$f
--- a/commit
+++ b/commit
@@ -1,4 +1,4 @@
-#!/bin/rc
+#!/bin/rc -e
rfork ne
. /bin/git/common.rc
@@ -36,11 +36,8 @@
refpath=.git/refs/$branch
initial=true
}
- if not{
- echo invalid branch $branch >[1=2]
- exit badbranch
- }
-
+ if not
+ die 'invalid branch:' $branch
}
fn editmsg{
@@ -50,7 +47,7 @@
echo '#' $nl'# ' ^ `$nl{git/walk -fAMR} >> $msgfile.tmp
sam $msgfile.tmp
if(~ `{grep -v '^[ ]*($|#.*$)' $msgfile.tmp | wc -l} 0)
- exit 'empty commit message'
+ die 'empty commit message'
grep -v '^[ ]*#' < $msgfile.tmp > $msgfile
}
@@ -67,10 +64,7 @@
msg=`"{cat $msgfile}
if(! ~ $#parents 0)
pflags='-p'^$parents
- hash=`{git/save -n $"name -e $"email -m $"msg $pflags}
- st=$status
- if(~ $hash '')
- exit nocommit
+ hash=`{git/save -n $"name -e $"email -m $"msg $pflags || die $status}
}
fn update{
@@ -96,10 +90,8 @@
msgfile=/tmp/git-msg.$pid
mkdir -p .git/refs
-if(git/walk -q){
- echo no changes to commit >[1=2]
- exit clean
-}
+if(git/walk -q)
+ die 'nothing to commit'
@{
flag e +
whoami
@@ -109,9 +101,4 @@
commit
update
cleanup
-}
-x=$status
-if(! ~ $x ''){
- echo 'could not commit:' $x >[1=2]
- exit $x
-}
+} || die 'could not commit:' $status
--- a/common.rc
+++ b/common.rc
@@ -1,17 +1,20 @@
nl='
'
+fn die{
+ >[1=2] echo $0: $*
+ exit $"*
+}
+
fn usage{
- >[2=1] echo -n 'usage:' $usage
+ >[1=2] 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'
- }
+ if(~ $#gitroot 0)
+ die 'not a git repository'
cd $gitroot
git/fs
}
--- a/export
+++ b/export
@@ -11,12 +11,7 @@
q=$*
if(~ $#q 0)
q=HEAD
-commits=`{git/query $q}
-x=$status
-if(! ~ $x ''){
- echo 'could not find commits: '$x
- exit $x
-}
+commits=`{git/query $q || die $status}
scratch=/tmp/gitexport.$pid
mkdir -p $scratch
--- a/import
+++ b/import
@@ -6,14 +6,7 @@
git/import [file ...]
'
-fn die{
- echo $patchname: $1 $2
- exit $2
-}
-
fn apply @{
- flag +e
-
git/fs
email=''
name=''
@@ -26,7 +19,7 @@
if not if(test -e /mnt/git/object/$branch/tree)
refpath=.git/HEAD
if not
- exit 'invalid branch '$branch
+ die 'invalid branch:' $branch
awk '
BEGIN{
state="headers"
@@ -63,25 +56,17 @@
END{
if(state != "diff")
exit("malformed patch: " state);
- if(name == "" || email == "" || msg == "" || date == ""){
- print "missing headers" > "/fd/2";
- exit("missingheader");
- }
+ if(name == "" || email == "" || msg == "" || date == "")
+ exit("missing headers");
printf "%s", name > "/env/name"
printf "%s", email > "/env/email"
printf "%s", msg > "/env/msg"
printf "%s", date > "/env/date"
}
- '
- st=$status
- if(! ~ $st ''){
- echo could not import: $st >[1=2]
- exit badpatch
- }
+ ' || die 'could not import:' $status
# force re-reading env
rc -c '
- flag -e
echo applying $msg | sed 1q
date=`{seconds $date}
files=`$nl{ape/patch -p1 < $diffpath | sed ''s/^patching file `(.*)''''/\1/''}
@@ -97,15 +82,6 @@
'
}
-fn import{
- apply
- st=$status
- if(! ~ $st ''){
- echo stdin: $st
- exit $st
- }
-}
-
gitup
patches=(/fd/0)
@@ -112,4 +88,4 @@
if(! ~ $#* 0)
patches=$*
for(f in $patches)
- import < $f
+ apply < $f || die $status
--- a/init
+++ b/init
@@ -1,4 +1,4 @@
-#!/bin/rc
+#!/bin/rc -e
rfork ne
. /bin/git/common.rc
@@ -32,10 +32,8 @@
if not
usage
-if(test -e $dir$sub){
- echo $dir$sub already exists >[1=2]
- exit exists
-}
+if(test -e $dir$sub)
+ die $dir$sub already exists
mkdir -p $dir$sub
dircp /sys/lib/git/template $dir/$sub
--- a/pull
+++ b/pull
@@ -15,14 +15,8 @@
upstream=$2
url=$3
dir=$4
-
- fetch=`"{git/fetch -b $branch -u $upstream $url |[2] tr '\x0d' '\x0a'}
- st=$status
- if(! ~ $st ''){
- echo fetch failed: $st
- exit $st
- }
- echo $"fetch | awk '
+
+ {git/fetch -b $branch -u $upstream $url || die $status} | awk '
/^remote/{
if($2=="HEAD")
next
@@ -68,10 +62,8 @@
if(~ $#remote 0)
remote=`{git/conf 'remote "'$upstream'".url'}
-if(~ $#remote 0){
- echo 'no idea from where to pull'
- exit upstream
-}
+if(~ $#remote 0)
+ die 'no remote to pull from'
update $branch $upstream $remote
if (~ $#checkout 0)
@@ -83,16 +75,13 @@
# we have local commits, but the remote hasn't changed.
# in this case, we want to keep the local commits untouched.
if(~ `{git/query HEAD $remote @} `{git/query $remote}){
- echo up to date >[1=2]
+ echo 'up to date' >[1=2]
exit
}
# The remote repository and our HEAD have diverged: we
# need to merge.
-if(! ~ `{git/query HEAD $remote @} `{git/query HEAD}){
- echo git/merge $remote '# diverged' >[1=2]
- exit merge
-}
-
+if(! ~ `{git/query HEAD $remote @} `{git/query HEAD})
+ die 'remote diverged:' $remote
# The remote is directly ahead of the local, and we have
# no local commits that need merging.
echo $local':' `{git/query $local} '=>' `{git/query $remote} >[1=2]
--- a/push
+++ b/push
@@ -1,4 +1,4 @@
-#!/bin/rc
+#!/bin/rc -e
rfork en
. /bin/git/common.rc
@@ -45,17 +45,12 @@
remote=`{git/conf 'remote "'$upstream'".url'}
if(~ $#remote 0)
remote=$upstream
-if(~ $#remote 0){
- echo 'no idea where to push'
- exit upstream
-}
+if(~ $#remote 0)
+ die 'no idea where to push'
if(~ $sendall '')
- updates=`$nl{git/send $force -b $branch $remove $remote}
+ updates=`$nl{git/send $force -b $branch $remove $remote || die $status}
if not
- updates=`$nl{git/send $force $remove -a $remote}
-x=$status
-if(! ~ $x '')
- exit $x
+ updates=`$nl{git/send $force $remove -a $remote || die $status}
for(ln in $updates){
u=`{echo $ln}