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.
This commit is contained in:
Ori Bernstein 2021-08-17 04:31:15 +00:00
parent e524e8d65a
commit cfebf83947
6 changed files with 30 additions and 34 deletions

View file

@ -11,23 +11,22 @@ fn usage{
exit 'usage' exit 'usage'
} }
# subst [-g] this [that] fn subst {
fn subst{ awk '
awk 'BEGIN{ BEGIN{ARGC=0}
global = 0 {sub(ARGV[1], ARGV[2]); print}
for(i = 1; ARGV[i] ~ /^-/; i++){ ' $*
if(ARGV[i] == "-g") }
global = 1
ARGC-- fn drop {
} awk '
this = ARGV[i++]; ARGC-- BEGIN{ARGC=0}
that = ARGV[i++]; ARGC--
}
{ {
if(global) gsub(this, that) if(index($0, ARGV[1]) == 1)
else sub(this, that) $0=substr($0, length(ARGV[1])+1)
print print
}' $* }
' $*
} }
fn present { fn present {
@ -77,7 +76,7 @@ fn gitup{
if(~ $#gitroot 0) if(~ $#gitroot 0)
die 'not a git repository' die 'not a git repository'
gitfs=$gitroot/.git/fs gitfs=$gitroot/.git/fs
gitrel=`{pwd | subst '^'$"gitroot'/?'} gitrel=`{pwd | drop $gitroot | sed 's@^/@@'}
if(~ $#gitrel 0) if(~ $#gitrel 0)
gitrel='.' gitrel='.'
cd $gitroot cd $gitroot

View file

@ -16,11 +16,11 @@ if(~ $remove 1){
if(~ $#* 0) if(~ $#* 0)
exec aux/usage exec aux/usage
paths=`$nl{cleanname -d $gitrel $*} paths=`$nl{cleanname -d $gitrel $* | drop $gitroot}
if(~ $add tracked) if(~ $add tracked)
files=`$nl{walk -f $paths} files=`$nl{walk -f ./$paths}
if not if not
files=`$nl{cd .git/index9/tracked/ && walk -f $paths} files=`$nl{cd .git/index9/tracked/ && walk -f ./$paths}
for(f in $files){ for(f in $files){
if(! ~ `$nl{cleanname $f} .git/*){ if(! ~ `$nl{cleanname $f} .git/*){

View file

@ -7,7 +7,7 @@ eval `''{aux/getflags $*} || exec aux/usage
if(~ $debug 1) if(~ $debug 1)
debug=(-d) debug=(-d)
remote=`{echo $1 | subst -g '/*$'} remote=`{echo $1 | sed 's@/*$@@'}
local=$2 local=$2
if(~ $#remote 0) if(~ $#remote 0)
@ -79,19 +79,17 @@ fn clone{
tree=.git/fs/HEAD/tree tree=.git/fs/HEAD/tree
lbranch=`{git/branch} lbranch=`{git/branch}
rbranch=`{echo $lbranch | subst '^heads' 'remotes/origin'} rbranch=`{echo $lbranch | subst 'heads' 'remotes/origin'}
echo checking out repository... echo checking out repository...
if(test -f .git/refs/$rbranch){ if(test -f .git/refs/$rbranch){
cp .git/refs/$rbranch .git/refs/$lbranch cp .git/refs/$rbranch .git/refs/$lbranch
git/fs git/fs
@ {builtin cd $tree && tar cif /fd/1 .} | @ {tar xf /fd/0} \ @ {builtin cd $tree && tar cif /fd/1 .} | @ {tar xf /fd/0} \
|| die 'checkout failed:' $status || die 'checkout failed:' $status
for(f in `$nl{walk -f $tree | subst '^'$tree'/*'}){ for(f in `$nl{walk -f $tree | drop $tree}){
if(! ~ $#f 0){ idx=.git/index9/tracked/$f
idx=.git/index9/tracked/$f mkdir -p `$nl{basename -d $idx}
mkdir -p `$nl{basename -d $idx} walk -eq ./$f > $idx
walk -eq $f > $idx
}
} }
} }
if not{ if not{

View file

@ -7,13 +7,12 @@ fn merge{
basebr=$gitfs/object/$2/tree basebr=$gitfs/object/$2/tree
theirbr=$gitfs/object/$3/tree theirbr=$gitfs/object/$3/tree
all=`$nl{{git/query -c $1 $2; git/query -c $2 $3} | sed 's/^..//' | \ all=`$nl{{git/query -c $1 $2; git/query -c $2 $3} | sed 's/^..//' | sort | uniq}
subst -g '^('$ourbr'|'$basebr'|'$theirbr')/*' | sort | uniq}
for(f in $all){ for(f in $all){
ours=$ourbr/$f ours=$ourbr/$f
base=$basebr/$f base=$basebr/$f
theirs=$theirbr/$f theirs=$theirbr/$f
merge1 $f $theirs $base $ours merge1 ./$f $theirs $base $ours
} }
} }

View file

@ -75,8 +75,8 @@ if(! ~ `{git/query HEAD $remote @} `{git/query HEAD}){
# The remote is directly ahead of the local, and we have # The remote is directly ahead of the local, and we have
# no local commits that need merging. # no local commits that need merging.
if(~ $#quiet 0) if(~ $#quiet 0)
git/log -s -e $local'..'$remote >[1=2] git/log -s -e $local'..'$remote
echo echo
echo $remote':' `{git/query $local} '=>' `{git/query $remote} >[1=2] echo $remote':' `{git/query $local} '=>' `{git/query $remote}
git/branch -mnb $remote $local git/branch -mnb $remote $local
exit '' exit ''

View file

@ -11,8 +11,8 @@ commit=$gitfs/HEAD
if(~ $#query 1) if(~ $#query 1)
commit=`{git/query -p $query} commit=`{git/query -p $query}
files=`$nl{cleanname -d $gitrel $*} files=`$nl{cleanname -d $gitrel $* | drop $gitroot}
for(f in `$nl{cd $commit/tree/ && walk -f $files}){ for(f in `$nl{cd $commit/tree/ && walk -f ./$files}){
mkdir -p `{basename -d $f} mkdir -p `{basename -d $f}
cp -x -- $commit/tree/$f $f cp -x -- $commit/tree/$f $f
git/add $f git/add $f