Commit e0233ceaa769843b74b877040ce8d8446283a403

Stefan Sperling 2019-07-25T13:04:23

fix 'got commit' from work tree with a path prefix

diff --git a/lib/worktree.c b/lib/worktree.c
index b41a123..e97e4a4 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2829,12 +2829,12 @@ match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
 
 	*match = 0;
 
-	if (strchr(ct->path, '/') == NULL) {
+	if (strchr(ct->in_repo_path, '/') == NULL) {
 		*match = got_path_is_root_dir(path);
 		return NULL;
 	}
 
-	err = got_path_dirname(&ct_parent_path, ct->path);
+	err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
 	if (err)
 		return err;
 	*match = (strcmp(path, ct_parent_path) == 0);
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index fa62e4a..4cb5ebe 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -332,6 +332,55 @@ function test_commit_added_and_modified_in_same_dir {
 	test_done "$testroot" "$ret"
 }
 
+function test_commit_path_prefix {
+	local testroot=`test_init commit_path_prefix`
+	local commit1=`git_show_head $testroot/repo`
+
+	got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "modified delta" > $testroot/wt/delta
+
+	(cd $testroot/wt && got commit -m 'changed gamma/delta' > $testroot/stdout)
+
+	local commit2=`git_show_head $testroot/repo`
+	echo "M  delta" > $testroot/stdout.expected
+	echo "Created commit $commit2" >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "diff $commit1 $commit2" > $testroot/stdout.expected
+	echo -n 'blob - ' >> $testroot/stdout.expected
+	got tree -r $testroot/repo -c $commit1 -i gamma | grep 'delta$' \
+		| cut -d' ' -f 1 >> $testroot/stdout.expected
+	echo -n 'blob + ' >> $testroot/stdout.expected
+	got tree -r $testroot/repo -c $commit2 -i gamma | grep 'delta$' | \
+		cut -d' ' -f 1 >> $testroot/stdout.expected
+	echo '--- gamma/delta' >> $testroot/stdout.expected
+	echo '+++ gamma/delta' >> $testroot/stdout.expected
+	echo '@@ -1 +1 @@' >> $testroot/stdout.expected
+	echo '-delta' >> $testroot/stdout.expected
+	echo '+modified delta' >> $testroot/stdout.expected
+
+	got diff -r $testroot/repo $commit1 $commit2 > $testroot/stdout
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_commit_basic
 run_test test_commit_new_subdir
 run_test test_commit_subdir
@@ -341,3 +390,4 @@ run_test test_commit_added_subdirs
 run_test test_commit_rejects_conflicted_file
 run_test test_commit_single_file_multiple
 run_test test_commit_added_and_modified_in_same_dir
+run_test test_commit_path_prefix