shithub: git9

ref: ec707628ca8f461a1088a2a9e07bd89f0990338f
dir: /commit/

View raw version
#!/bin/rc -e
rfork ne
. /sys/lib/git/common.rc

usage='
	git/commit files...
'

fn whoami{
	name=`{git/conf user.name}
	email=`{git/conf user.email}
	msgfile=.git/git-msg.$pid
	if(test -f /adm/keys.who){
		if(~ $name '')
			name=`{awk -F'|' '$1=="'$user'" {x=$3} END{print x}' </adm/keys.who}
		if(~ $email '')
			email=`{awk -F'|' '$1=="'$user'" {x=$5} END{print x}' </adm/keys.who}
	}
	if(~ $name '')
		name=glenda
	if(~ $email '')
		email=glenda@9front.local
}

fn findbranch{
	branch=`{awk '$1=="branch"{print $2}' < /mnt/git/ctl}
	if(test -e /mnt/git/branch/$branch/tree){
		refpath=.git/refs/$branch
		initial=false
	}
	if not if(test -e /mnt/git/object/$branch/tree){
		refpath=.git/HEAD
		initial=false
	}
	if not if(! test -e /mnt/git/HEAD/tree){
		refpath=.git/refs/$branch
		initial=true
	}
	if not
		die 'invalid branch:' $branch
}

fn editmsg{
	echo '# Author: '$"name' <'$"email'>' >> $msgfile.tmp
	echo '#' $nl'# ' ^ `$nl{git/walk -fAMR $files} >> $msgfile.tmp
	echo '#' >> $msgfile.tmp
	echo '# Commit message:' >> $msgfile.tmp
	giteditor=`{git/conf core.editor}
	if(~ $#editor 0)
		editor=$giteditor
	if(~ $#editor 0)
		editor=hold
	$editor $msgfile.tmp
	if(~ `{grep -v '^[ 	]*($|#.*$)' $msgfile.tmp | wc -l} 0)
		die 'empty commit message'
	grep -v '^[ 	]*#' < $msgfile.tmp > $msgfile
}

fn parents{
	if(test -f .git/index9/merge-parents)
		parents=`{cat .git/index9/merge-parents}
	if not if(~ $initial true)
		parents=()
	if not
		parents=$branch
}

fn commit{
	msg=`"{cat $msgfile}
	if(! ~ $#parents 0)
		pflags='-p'^$parents
	hash=`{git/save -n $"name -e $"email  -m $"msg $pflags $files || die $status}
}

fn update{
	mkdir -p `{basename -d $refpath}
	# Paranoia: let's not mangle the repo.
	if(~ $#hash 0)
		die 'botched commit'
	echo $hash > $refpath
	for(f in $files){
		if(test -e .git/index9/removed/$f){
			rm -f .git/index9/removed/$f
			rm -f .git/index9/tracked/$f
		}
		if not{
			mkdir -p `{basename -d $f}
			walk -eq $f > .git/index9/tracked/$f
		}
	}
}

fn cleanup{
	rm -f $msgfile
	rm -f .git/index9/merge-parents
}

gitup

msgfile=/tmp/git-msg.$pid
mkdir -p .git/refs
if(~ $#* 0)
	usage
files=`{git/walk -c $*}
if(~ $status '' || ~ $#files 0asdf)
	die 'nothing to commit'
@{
	flag e +
	whoami
	findbranch
	parents
	editmsg
	commit
	update
	cleanup
} || die 'could not commit:' $status