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=\ TEST=\
add\ add\
basic\ basic\
diff\
export\ export\
ftype\ ftype\
lca\ lca\

View file

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