Commit 5c99ca9fe447187c440a413772fb14201f30a1d2

Stefan Sperling 2019-03-27T07:41:37

fix behaviour when 'got add' is used twice

diff --git a/lib/worktree.c b/lib/worktree.c
index 45ca7be..f03a291 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1621,10 +1621,6 @@ got_worktree_schedule_add(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
-	if (err)
-		goto done;
-
 	fileindex = got_fileindex_alloc();
 	if (fileindex == NULL) {
 		err = got_error_from_errno();
@@ -1648,6 +1644,15 @@ got_worktree_schedule_add(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
+	if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
+		err = got_error_set_errno(EEXIST);
+		goto done;
+	}
+
+	err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
+	if (err)
+		goto done;
+
 	err = got_fileindex_entry_add(fileindex, ie);
 	if (err)
 		goto done;
@@ -1681,7 +1686,7 @@ done:
 			err = got_error_from_errno();
 		free(new_fileindex_path);
 	}
-	if (!ie_added)
+	if (ie && !ie_added)
 		got_fileindex_entry_free(ie);
 	if (fileindex)
 		got_fileindex_free(fileindex);
diff --git a/regress/cmdline/add.sh b/regress/cmdline/add.sh
index 1064161..89d8177 100755
--- a/regress/cmdline/add.sh
+++ b/regress/cmdline/add.sh
@@ -39,4 +39,34 @@ function test_add_basic {
 	test_done "$testroot" "$ret"
 }
 
+function test_double_add {
+	local testroot=`test_init double_add`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "new file" > $testroot/wt/foo
+	(cd $testroot/wt && got add foo > /dev/null)
+
+	echo "got: File exists" > $testroot/stderr.expected
+	(cd $testroot/wt && got add foo 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got add command succeeded unexpectedly" >&2
+		test_done "$testroot" 1
+	fi
+
+	cmp $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_add_basic
+run_test test_double_add