Commit 81a30460ae72c2e83fd47df22f93785661b7b750

Stefan Sperling 2019-01-09T00:41:58

fix a path matching bug in collect_missing_file()

diff --git a/lib/worktree.c b/lib/worktree.c
index 408e0bd..ff90b0e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -760,6 +760,8 @@ collect_missing_file(void *args, struct got_fileindex_entry *entry)
 		return NULL;
 
 	start = entry->path + strlen(a->current_subdir);
+	if (a->current_subdir[0] != '\0' && start[0] != '/')
+		return NULL;
 	while (start[0] == '/')
 		start++;
 	end = strchr(start, '/');
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index eb351e5..2faad6b 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -240,9 +240,72 @@ function test_update_deletes_dir_recursively {
 	test_done "$testroot" "0"
 }
 
+function test_update_with_sibling_dirs_with_common_prefix {
+	local testroot=`test_init update_with_sibling_dirs_with_common_prefix`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	if [ "$?" != "0" ]; then
+		test_done "$testroot" "$?"
+		return 1
+	fi
+
+	mkdir $testroot/repo/epsilon2
+	echo mu > $testroot/repo/epsilon2/mu
+	(cd $testroot/repo && git add epsilon2/mu)
+	git_commit $testroot/repo -m "adding sibling of epsilon"
+	echo change > $testroot/repo/epsilon/zeta
+	git_commit $testroot/repo -m "changing epsilon/zeta"
+
+	echo "U  epsilon/zeta" > $testroot/stdout.expected
+	echo "A  epsilon2/mu" >> $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)
+
+	cmp $testroot/stdout.expected $testroot/stdout
+	if [ "$?" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$?"
+		return 1
+	fi
+
+	echo "another change" > $testroot/repo/epsilon/zeta
+	git_commit $testroot/repo -m "changing epsilon/zeta again"
+
+	echo "U  epsilon/zeta" > $testroot/stdout.expected
+	echo -n "Updated to commit " >> $testroot/stdout.expected
+	git_show_head $testroot/repo >> $testroot/stdout.expected
+	echo >> $testroot/stdout.expected
+
+	# Bug: This update used to do delete/add epsilon2/mu again:
+	# U  epsilon/zeta
+	# D  epsilon2/mu <--- not intended
+	# A  epsilon2/mu <--- not intended
+	(cd $testroot/wt && got update > $testroot/stdout)
+
+	cmp $testroot/stdout.expected $testroot/stdout
+	if [ "$?" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$?"
+		return 1
+	fi
+
+	cmp $testroot/stdout.expected $testroot/stdout
+	if [ "$?" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$?"
+		return 1
+	fi
+
+	test_done "$testroot" "0"
+}
+
 run_test test_update_basic
 run_test test_update_adds_file
 run_test test_update_deletes_file
 run_test test_update_deletes_dir
 run_test test_update_deletes_dir_with_path_prefix
 run_test test_update_deletes_dir_recursively
+run_test test_update_with_sibling_dirs_with_common_prefix