shithub: riscv

Download patch

ref: 248634879a9b78f08ddaeda9c7a50ba413fd4804
parent: 7814ec46c39671d5c0721626c52c6d86c21cb311
author: Jacob Moody <moody@posixcafe.org>
date: Sun Dec 22 17:41:02 EST 2024

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

--- /dev/null
+++ b/sys/src/cmd/git/test/diff.rc
@@ -1,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
+}
--- a/sys/src/cmd/git/test/mkfile
+++ b/sys/src/cmd/git/test/mkfile
@@ -3,6 +3,7 @@
 TEST=\
 	add\
 	basic\
+	diff\
 	export\
 	ftype\
 	lca\
--- a/sys/src/cmd/git/walk.c
+++ b/sys/src/cmd/git/walk.c
@@ -34,7 +34,6 @@
 char	repopath[1024];
 char	wdirpath[1024];
 char	relapath[1024];
-char	slashes[1024];
 int	nslash;
 char	*rstr	= "R ";
 char	*mstr	= "M ";
@@ -350,24 +349,26 @@
 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);
 	}
 }
 
@@ -374,7 +375,7 @@
 void
 findslashes(char *path)
 {
-	char *s, *p;
+	char *p;
 
 	p = cleanname(path);
 	if(p[0] == '.'){
@@ -389,13 +390,9 @@
 	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
--