"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Mark Jamsek <mark@jamsek.com>
Subject:
make worktree diffs record xbit of new files
To:
Game of Trees <gameoftrees@openbsd.org>
Date:
Wed, 21 Sep 2022 22:22:39 +1000

Download raw body.

Thread
Related to the recent work: when running `got diff` in a worktree with
a file that has the x-bit and has been added with `got add`, we don't
presently record this mode like we do with `got diff obj1 obj2` or
`got diff -c obj`.

The below change makes `got diff` in a worktree record this information
like other diff modes.

-----------------------------------------------
commit ee7e13140a566f39caae8c2442443c97af77c2b8 (main)
from: Mark Jamsek <mark@jamsek.dev>
date: Wed Sep 21 12:18:39 2022 UTC
 
 make worktree diffs record the file mode if x-bit is set
 
diff 611e5fc2074d428e17f920dc595496af4dd0dc77 ee7e13140a566f39caae8c2442443c97af77c2b8
commit - 611e5fc2074d428e17f920dc595496af4dd0dc77
commit + ee7e13140a566f39caae8c2442443c97af77c2b8
blob - ec271a03249db4f54f213d8fd71a18c6220153ef
blob + 55131b6cfdbed599216a2961e1e012c2b7349531
--- lib/diff.c
+++ lib/diff.c
@@ -239,9 +239,15 @@ diff_blob_file(struct got_diffreg_result **resultp,
 		idstr1 = "/dev/null";
 
 	if (outfile) {
+		struct stat sb;
+
+		if (f2_exists)
+			if (fstat(fileno(f2), &sb) == -1)
+				return got_error_from_errno2("stat", label2);
 		fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
-		fprintf(outfile, "file + %s\n",
-		    f2_exists ? label2 : "/dev/null");
+		fprintf(outfile, "file + %s%s\n",
+		    f2_exists ? label2 : "/dev/null",
+		    f2_exists && sb.st_mode & S_IXUSR ? " (mode 755)" : "");
 	}
 
 	err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
blob - b01acc2d4192df91e119abcea319ac890c164de0
blob + becf27d3e00feb1fd2b32823931dc0d1b580cfb6
--- libexec/got-read-patch/got-read-patch.c
+++ libexec/got-read-patch/got-read-patch.c
@@ -231,7 +231,8 @@ find_diff(int *done, int *next, FILE *fp, int git, con
 		} else if (!strncmp(line, "+++ ", 4)) {
 			free(new);
 			err = filename(line+4, &new);
-		} else if (!strncmp(line, "blob + ", 7)) {
+		} else if (!strncmp(line, "blob + ", 7) ||
+		    !strncmp(line, "file + ", 7)) {
 			xbit = filexbit(line);
 		} else if (!git && !strncmp(line, "blob - ", 7)) {
 			free(blob);
blob - 43f41172dfa14e7ca103f635f10f4b25a6f11cf8
blob + 3dd406d46df77adc838bdfc9be2f93687dcf69d8
--- regress/cmdline/diff.sh
+++ regress/cmdline/diff.sh
@@ -1327,6 +1327,48 @@ EOF
 	test_done "$testroot" $ret
 }
 
+test_diff_newfile_xbit() {
+	local testroot=`test_init newfile_xbit`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" $ret
+		return 1
+	fi
+
+	echo xfile > $testroot/wt/xfile
+	chmod +x $testroot/wt/xfile
+	(cd $testroot/wt && got add xfile) > /dev/null
+	(cd $testroot/wt && got diff) > $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" $ret
+		return 1
+	fi
+
+	local commit_id=`git_show_head $testroot/repo`
+	cat <<EOF > $testroot/stdout.expected
+diff $testroot/wt
+commit - $commit_id
+path + $testroot/wt
+blob - /dev/null
+file + xfile (mode 755)
+--- /dev/null
++++ xfile
+@@ -0,0 +1 @@
++xfile
+EOF
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		echo "failed to record mode 755"
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" $ret
+}
+
 test_parseargs "$@"
 run_test test_diff_basic
 run_test test_diff_shows_conflict
@@ -1340,3 +1382,4 @@ run_test test_diff_binary_files
 run_test test_diff_commits
 run_test test_diff_ignored_file
 run_test test_diff_crlf
+run_test test_diff_newfile_xbit


-- 
Mark Jamsek <fnc.bsdbox.org>
GPG: F2FF 13DE 6A06 C471 CA80  E6E2 2930 DC66 86EE CF68