test 'got rm' behaviour on staged files
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
diff --git a/include/got_error.h b/include/got_error.h
index f37c253..5e0b55d 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -114,6 +114,7 @@
#define GOT_ERR_HISTEDIT_PATH 98
#define GOT_ERR_NO_MERGED_PATHS 99
#define GOT_ERR_COMMIT_BRANCH 100
+#define GOT_ERR_FILE_STAGED 101
static const struct got_error {
int code;
@@ -230,6 +231,7 @@ static const struct got_error {
{ GOT_ERR_NO_MERGED_PATHS, "empty list of merged paths" },
{ GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
"\"refs/heads/\" reference namespace" },
+ { GOT_ERR_FILE_STAGED, "file has staged changes" },
};
/*
diff --git a/lib/worktree.c b/lib/worktree.c
index d402893..9b859b2 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2550,13 +2550,20 @@ schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
{
const struct got_error *err = NULL;
struct got_fileindex_entry *ie = NULL;
- unsigned char status;
+ unsigned char status, staged_status;
struct stat sb;
ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
if (ie == NULL)
return got_error(GOT_ERR_BAD_PATH);
+ staged_status = get_staged_status(ie);
+ if (staged_status != GOT_STATUS_NO_CHANGE) {
+ if (staged_status == GOT_STATUS_DELETE)
+ return NULL;
+ return got_error_path(relpath, GOT_ERR_FILE_STAGED);
+ }
+
err = get_file_status(&status, &sb, ie, ondisk_path, repo);
if (err)
return err;
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index f23f8a0..0631a5b 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -191,6 +191,75 @@ function test_stage_add_already_staged_file {
test_done "$testroot" "$ret"
}
+function test_stage_rm_already_staged_file {
+ local testroot=`test_init stage_rm_already_staged_file`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified file" > $testroot/wt/alpha
+ (cd $testroot/wt && got rm beta > /dev/null)
+ echo "new file" > $testroot/wt/foo
+ (cd $testroot/wt && got add foo > /dev/null)
+
+ (cd $testroot/wt && got stage alpha beta foo > $testroot/stdout)
+
+ (cd $testroot/wt && got rm beta \
+ > $testroot/stdout 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got rm command succeeded unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+ echo "got: realpath: beta: No such file or directory" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ for f in alpha foo; do
+ echo "got: $f: file has staged changes" \
+ > $testroot/stderr.expected
+ (cd $testroot/wt && got rm $f \
+ > $testroot/stdout 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got rm command succeeded unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ done
+
+ echo ' M alpha' > $testroot/stdout.expected
+ echo ' D beta' >> $testroot/stdout.expected
+ echo ' A foo' >> $testroot/stdout.expected
+
+ (cd $testroot/wt && got status > $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_stage_basic
run_test test_stage_status
run_test test_stage_add_already_staged_file
+run_test test_stage_rm_already_staged_file