Commit ba8a0d4daae0952f0b83279b0ffc8ab29bcc9c6c

Stefan Sperling 2019-02-10T12:30:06

clear executable bit when updating files if necessary

diff --git a/lib/worktree.c b/lib/worktree.c
index 98fe111..2761439 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -768,13 +768,21 @@ install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
 			err = got_error_from_errno();
 			goto done;
 		}
+	} else {
+		/* In case of an update stat buf has been loaded above. */
+		if (lstat(ondisk_path, &sb) == -1) {
+			err = got_error_from_errno();
+			goto done;
+		}
 	}
-	if (mode & S_IRWXU) {
-		if (!update && lstat(ondisk_path, &sb) == -1) {
+
+	if (mode & S_IXUSR) {
+		if (chmod(ondisk_path, sb.st_mode | S_IXUSR) == -1) {
 			err = got_error_from_errno();
 			goto done;
 		}
-		if (chmod(ondisk_path, sb.st_mode | S_IRWXU) == -1) {
+	} else {
+		if (chmod(ondisk_path, sb.st_mode & ~S_IXUSR) == -1) {
 			err = got_error_from_errno();
 			goto done;
 		}
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index bc2db08..8e67c9a 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -729,6 +729,64 @@ function test_update_keeps_xbit {
 	test_done "$testroot" "$ret"
 }
 
+function test_update_clears_xbit {
+	local testroot=`test_init update_clears_xbit 1`
+
+	touch $testroot/repo/xfile
+	chmod +x $testroot/repo/xfile
+	(cd $testroot/repo && git add .)
+	git_commit $testroot/repo -m "adding executable file"
+
+	got checkout $testroot/repo $testroot/wt > $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	ls -l $testroot/wt/xfile | grep -q '^-rwx'
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "file is not executable" >&2
+		ls -l $testroot/wt/xfile >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# XXX git seems to require a file edit when flipping the x bit?
+	echo foo > $testroot/repo/xfile
+	chmod -x $testroot/repo/xfile
+	git_commit $testroot/repo -m "not an executable file anymore"
+
+	echo "U  xfile" > $testroot/stdout.expected
+	echo -n "Updated to commit " >> $testroot/stdout.expected
+	git_show_head $testroot/repo >> $testroot/stdout.expected
+	echo >> $testroot/stdout.expected
+
+	(cd $testroot/wt && got update > $testroot/stdout)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cmp $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	ls -l $testroot/wt/xfile | grep -q '^-rw-'
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "file is unexpectedly executable" >&2
+		ls -l $testroot/wt/xfile >&2
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_update_basic
 run_test test_update_adds_file
 run_test test_update_deletes_file
@@ -744,3 +802,4 @@ run_test test_update_creates_missing_parent_with_subdir
 run_test test_update_file_in_subsubdir
 run_test test_update_merges_file_edits
 run_test test_update_keeps_xbit
+run_test test_update_clears_xbit