plan9fox/sys/lib/git/common.rc
Ori Bernstein cfebf83947 git: better handling of absolute paths, regex metachars
Git currently gets a bit confused if you try to
manipulate files by absolute path.  There were also a
number of places where user-controlled file paths ended
up getting passed to regex interpretation, which could
confuse things.

This change mainly does 2 things:

	- Adds a 'drop' function which drops
	  a non-regex prefix from a string, and uses
	  that to manipulate paths, simplifies 'subst',
	  and removes 'subst -g', which was only used
	  with fixed regexes; sed does this job fine.
	- When getting a path from a user, we
	  make it absolute and then strip out the head

Along the way it cleans up a couple of stupids:

	- 'for(f in $list) if(! ~ $#f 0) use $f:
	  $f can't be a nil list because of
	  list flattening.
	- removes a useless substitution here:

	 	all=`$nl{{git/query -c $1 $2; git/query -c $2 $3} | sed 's/^..//' | \
			gsubst '^('$ourbr'|'$basebr'|'$theirbr')/*' | sort | uniq}

	  where git/query -c doesn't produce
	  paths prefixed with the query.
2021-08-17 04:31:15 +00:00

94 lines
1.4 KiB
Text

nl='
'
fn die{
>[1=2] echo $0: $*
exit $"*
}
fn usage{
>[1=2] echo -n 'usage:' $usage
exit 'usage'
}
fn subst {
awk '
BEGIN{ARGC=0}
{sub(ARGV[1], ARGV[2]); print}
' $*
}
fn drop {
awk '
BEGIN{ARGC=0}
{
if(index($0, ARGV[1]) == 1)
$0=substr($0, length(ARGV[1])+1)
print
}
' $*
}
fn present {
if(~ $1 /dev/null && cmp $2 $3>/dev/null)
status=gone
if not if (~ $3 /dev/null && cmp $1 $2>/dev/null)
status=gone
if not
status=()
}
# merge1 out theirs base ours
fn merge1 {@{
rfork e
n=$pid
out=$1
ours=$2
base=$3
theirs=$4
tmp=$out.tmp
while(test -f $tmp){
tmp=$tmp.$n
n=`{echo $n + 1 | hoc}
}
if(! test -f $ours)
ours=/dev/null
if(! test -f $base)
base=/dev/null
if(! test -f $theirs)
theirs=/dev/null
if(! ape/diff3 -3 -m $ours $base $theirs > $tmp)
echo merge needed: $out >[1=2]
if(present $ours $base $theirs){
mv $tmp $out
git/add $out
}
if not {
rm -f $tmp $out
git/rm $out
}
}}
fn gitup{
gitroot=`{git/conf -r >[2]/dev/null}
if(~ $#gitroot 0)
die 'not a git repository'
gitfs=$gitroot/.git/fs
gitrel=`{pwd | drop $gitroot | sed 's@^/@@'}
if(~ $#gitrel 0)
gitrel='.'
cd $gitroot
startfs=()
if(! test -d $gitfs)
mkdir -p $gitfs
if(! test -e $gitfs/ctl)
startfs=true
if(! grep -s '^repo '$gitroot'$' $gitfs/ctl >[2]/dev/null)
startfs=true
if(~ $#startfs 1)
git/fs
if not
status=''
}