fix behaviour when 'got add' is used twice
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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