From cfebf83947550f3ae730cef4a9b1ab6bf6b8d4f7 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Tue, 17 Aug 2021 04:31:15 +0000 Subject: 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. --- sys/lib/git/common.rc | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'sys/lib') diff --git a/sys/lib/git/common.rc b/sys/lib/git/common.rc index b98987d75..787b5babd 100644 --- a/sys/lib/git/common.rc +++ b/sys/lib/git/common.rc @@ -11,23 +11,22 @@ fn usage{ exit 'usage' } -# subst [-g] this [that] -fn subst{ - awk 'BEGIN{ - global = 0 - for(i = 1; ARGV[i] ~ /^-/; i++){ - if(ARGV[i] == "-g") - global = 1 - ARGC-- - } - this = ARGV[i++]; ARGC-- - that = ARGV[i++]; ARGC-- - } +fn subst { + awk ' + BEGIN{ARGC=0} + {sub(ARGV[1], ARGV[2]); print} + ' $* +} + +fn drop { + awk ' + BEGIN{ARGC=0} { - if(global) gsub(this, that) - else sub(this, that) + if(index($0, ARGV[1]) == 1) + $0=substr($0, length(ARGV[1])+1) print - }' $* + } + ' $* } fn present { @@ -77,7 +76,7 @@ fn gitup{ if(~ $#gitroot 0) die 'not a git repository' gitfs=$gitroot/.git/fs - gitrel=`{pwd | subst '^'$"gitroot'/?'} + gitrel=`{pwd | drop $gitroot | sed 's@^/@@'} if(~ $#gitrel 0) gitrel='.' cd $gitroot -- cgit v1.2.3