shithub: front

Download patch

ref: b4e8108d6c43e95a5f1794ef8fcbed53811e1641
parent: e4373dd1038fd8e0369d595283b032d011f28e59
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Nov 9 20:31:25 EST 2024

git/save: sort argv lexicographically

our code assumed that the inputs were sorted, and
that scanning forward by prefix would group the dirs
by prefix. Sort so that assumption is true.

--- a/sys/src/cmd/git/save.c
+++ b/sys/src/cmd/git/save.c
@@ -25,6 +25,7 @@
 Idxent	*idx;
 int	idxsz;
 int	nidx;
+
 int
 gitmode(Dirent *e)
 {
@@ -41,6 +42,12 @@
 }
 
 int
+namecmp(void *pa, void *pb)
+{
+	return strcmp(*(char**)pa, *(char**)pb);
+}
+
+int
 idxcmp(void *pa, void *pb)
 {
 	Idxent *a, *b;
@@ -228,6 +235,8 @@
 		 * paths have been normalized already,
 		 * no leading or double-slashes allowed.
 		 */
+		assert(off <= strlen(s));
+		assert(off == 0 || s[off-1] == '/');
 		assert(s[off] != '\0' && s[off] != '/');
 
 		/* get next path element length (from off until '/' or nul) */
@@ -414,6 +423,7 @@
 		while(*argv[i] == '/')
 			argv[i]++;
 	}
+	qsort(argv, argc, sizeof(*argv), namecmp);
 
 	t = findroot();
 	nidx = 0;
--