git: bikeshed git/walk relative path calculation and add tests

This commit is contained in:
Jacob Moody 2024-12-22 22:41:02 +00:00
parent 7814ec46c3
commit 248634879a
3 changed files with 62 additions and 18 deletions

46
sys/src/cmd/git/test/diff.rc Executable file
View file

@ -0,0 +1,46 @@
#!/bin/rc
. util.rc
nl='
'
rm -fr scratch
mkdir -p scratch/subdir/subdir2
mkdir -p scratch/subdir3
echo @@git diff -s relative@@
@{
cd scratch
q git/init
echo hello > file.txt
echo hello1 > subdir/file1.txt
echo hello2 > subdir/subdir2/file2.txt
echo hello3 > subdir3/file3.txt
q git/add file.txt subdir/file1.txt subdir/subdir2/file2.txt subdir3/file3.txt
q git/commit -m initial .
echo >file.txt
echo >subdir/file1.txt
echo >subdir/subdir2/file2.txt
echo >subdir3/file3.txt
out=`$nl{git/diff -s . | awk '{ print $2 }'}
~ $out(1) file.txt && ~ $out(2) subdir/file1.txt && ~ $out(3) subdir/subdir2/file2.txt \
~ $out(4) subdir3/file3.txt || die 'base level fail'
cd subdir
out=`$nl{git/diff -s .. | awk '{ print $2 }'}
~ $out(1) ../file.txt && ~ $out(2) file1.txt && ~ $out(3) subdir2/file2.txt \
~ $out(4) ../subdir3/file3.txt || die 'subdir1 level fail'
cd subdir2
out=`$nl{git/diff -s ../.. | awk '{ print $2 }'}
~ $out(1) ../../file.txt && ~ $out(2) ../file1.txt && ~ $out(3) file2.txt \
~ $out(4) ../../subdir3/file3.txt || die 'subdir2 level fail'
cd ../../subdir3
out=`$nl{git/diff -s .. | awk '{ print $2 }'}
~ $out(1) ../file.txt && ~ $out(2) ../subdir/file1.txt && ~ $out(3) ../subdir/subdir2/file2.txt \
~ $out(4) file3.txt || die 'subdir3 level fail'
! git/diff -s ../.. >[2]/dev/null
}

View file

@ -3,6 +3,7 @@
TEST=\
add\
basic\
diff\
export\
ftype\
lca\

View file

@ -34,7 +34,6 @@ Idxed idxtab[NCACHE];
char repopath[1024];
char wdirpath[1024];
char relapath[1024];
char slashes[1024];
int nslash;
char *rstr = "R ";
char *mstr = "M ";
@ -350,31 +349,33 @@ reporel(char *s)
void
show(Biobuf *o, int flg, char *str, char *path)
{
char *pa, *pb, *suffix;
int ncommon = 0;
char *pa, *pb;
int n;
dirty |= flg;
if(!quiet && (printflg & flg)){
if(nslash){
suffix = path;
Bprint(o, str);
n = nslash;
if(n){
for(pa = relapath, pb = path; *pa && *pb; pa++, pb++){
if(*pa != *pb)
break;
if(*pa == '/'){
ncommon++;
suffix = pb+1;
n--;
path = pb+1;
}
}
Bprint(o, "%s%.*s%s\n", str, (nslash-ncommon)*3, slashes, suffix);
} else
Bprint(o, "%s%s\n", str, path);
while(n-- > 0)
Bprint(o, "../");
}
Bprint(o, "%s\n", path);
}
}
void
findslashes(char *path)
{
char *s, *p;
char *p;
p = cleanname(path);
if(p[0] == '.'){
@ -389,13 +390,9 @@ findslashes(char *path)
if(*p == '/')
p++;
s = slashes;
for(; *p; p++){
if(*p != '/')
continue;
nslash++;
s = seprint(s, slashes + sizeof slashes, "../");
}
for(; *p; p++)
if(*p == '/')
nslash++;
}
void