Commit f2b0a8b0a1881cbc7388392deaa518caf38be151

Stefan Sperling 2020-07-31T12:54:34

fix committing file additions from a work tree with a path prefix New files were added under the wrong tree in the repository if the work tree has a path prefix. Fix this problem and catch it in the existing commit_with_path_prefix regression test.

diff --git a/lib/worktree.c b/lib/worktree.c
index d3a3569..68b5fac 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -5004,12 +5004,12 @@ write_tree(struct got_object_id **new_tree_id, int *nentries,
 		    (ct->flags & GOT_COMMITABLE_ADDED))
 			continue;
 
-		 if (!got_path_is_child(pe->path, path_base_tree,
+		 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
 		     strlen(path_base_tree)))
 			continue;
 
 		err = got_path_skip_common_ancestor(&child_path, path_base_tree,
-		    pe->path);
+		    ct->in_repo_path);
 		if (err)
 			goto done;
 
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index b877e38..5e1cf69 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -444,8 +444,58 @@ function test_commit_path_prefix {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got rm delta > /dev/null)
+	echo new > $testroot/wt/new
+	(cd $testroot/wt && got add new > /dev/null)
+
+	(cd $testroot/wt && got commit -m 'remove gamma/delta; add gamma/new' \
+		> $testroot/stdout)
+
+	local commit3=`git_show_head $testroot/repo`
+	echo "A  new" > $testroot/stdout.expected
+	echo "D  delta" >> $testroot/stdout.expected
+	echo "Created commit $commit3" >> $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 $commit2 $commit3" > $testroot/stdout.expected
+	echo -n 'blob - ' >> $testroot/stdout.expected
+	got tree -r $testroot/repo -c $commit2 -i gamma | grep 'delta$' \
+		| cut -d' ' -f 1 | sed -e 's/$/ (mode 644)/' \
+		>> $testroot/stdout.expected
+	echo 'blob + /dev/null' >> $testroot/stdout.expected
+	echo '--- gamma/delta' >> $testroot/stdout.expected
+	echo '+++ /dev/null' >> $testroot/stdout.expected
+	echo '@@ -1 +0,0 @@' >> $testroot/stdout.expected
+	echo '-modified delta' >> $testroot/stdout.expected
+	echo 'blob - /dev/null' >> $testroot/stdout.expected
+	echo -n 'blob + ' >> $testroot/stdout.expected
+	got tree -r $testroot/repo -c $commit3 -i gamma | grep 'new$' | \
+		cut -d' ' -f 1 | sed -e 's/$/ (mode 644)/' \
+		>> $testroot/stdout.expected
+	echo '--- /dev/null' >> $testroot/stdout.expected
+	echo '+++ gamma/new' >> $testroot/stdout.expected
+	echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
+	echo '+new' >> $testroot/stdout.expected
+
+	got diff -r $testroot/repo $commit2 $commit3 > $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"
+	return "$ret"
 }
 
 function test_commit_dir_path {