fix and test stage/unstage with unversioned 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
diff --git a/lib/worktree.c b/lib/worktree.c
index 070abf5..c04d60e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -5126,6 +5126,9 @@ check_stage_ok(void *arg, unsigned char status,
struct got_object_id *base_commit_idp = NULL;
char *in_repo_path = NULL, *p;
+ if (status == GOT_STATUS_UNVERSIONED)
+ return NULL;
+
ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
if (ie == NULL)
return got_error_path(relpath, GOT_ERR_FILE_STATUS);
@@ -5477,6 +5480,9 @@ stage_path(void *arg, unsigned char status,
char *ondisk_path = NULL, *path_content = NULL;
uint32_t stage;
+ if (status == GOT_STATUS_UNVERSIONED)
+ return NULL;
+
ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
if (ie == NULL)
return got_error_path(relpath, GOT_ERR_FILE_STATUS);
@@ -5815,7 +5821,7 @@ unstage_path(void *arg, unsigned char status,
ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
if (ie == NULL)
- return got_error_path(relpath, GOT_ERR_BAD_PATH);
+ return got_error_path(relpath, GOT_ERR_FILE_STATUS);
if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
== -1)
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 9ef34a4..ee91795 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -82,6 +82,68 @@ function test_stage_no_changes {
test_done "$testroot" "$ret"
}
+function test_stage_unversioned {
+ local testroot=`test_init stage_unversioned`
+
+ 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
+ touch $testroot/wt/unversioned-file
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+ echo "M alpha" > $testroot/stdout.expected
+ echo "? unversioned-file" >> $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
+
+ (cd $testroot/wt && got stage > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got stage command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo " M alpha" > $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 "modified file again" > $testroot/wt/alpha
+
+ (cd $testroot/wt && got stage unversioned-file > $testroot/stdout \
+ 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got stage command succeed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo "got: no changes to stage" > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ fi
+ test_done "$testroot" "$ret"
+
+}
+
function test_stage_list {
local testroot=`test_init stage_list`
@@ -1739,6 +1801,7 @@ EOF
run_test test_stage_basic
run_test test_stage_no_changes
+run_test test_stage_unversioned
run_test test_stage_list
run_test test_stage_conflict
run_test test_stage_out_of_date
diff --git a/regress/cmdline/unstage.sh b/regress/cmdline/unstage.sh
index 9c6d712..3d3129e 100755
--- a/regress/cmdline/unstage.sh
+++ b/regress/cmdline/unstage.sh
@@ -67,6 +67,84 @@ function test_unstage_basic {
test_done "$testroot" "$ret"
}
+function test_unstage_unversioned {
+ local testroot=`test_init unstage_unversioned`
+
+ 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)
+
+ echo ' M alpha' > $testroot/stdout.expected
+ echo ' D beta' >> $testroot/stdout.expected
+ echo ' A foo' >> $testroot/stdout.expected
+ (cd $testroot/wt && got stage > /dev/null)
+
+ touch $testroot/wt/unversioned-file
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+ echo ' M alpha' > $testroot/stdout.expected
+ echo ' D beta' >> $testroot/stdout.expected
+ echo ' A foo' >> $testroot/stdout.expected
+ echo "? unversioned-file" >> $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
+
+ (cd $testroot/wt && got unstage > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got unstage command failed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo 'G alpha' > $testroot/stdout.expected
+ echo 'D beta' >> $testroot/stdout.expected
+ echo 'G foo' >> $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
+
+ (cd $testroot/wt && got stage > /dev/null)
+
+ # unstaging an unversioned path is a no-op
+ (cd $testroot/wt && got unstage unversioned > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got unstage command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo ' M alpha' > $testroot/stdout.expected
+ echo ' D beta' >> $testroot/stdout.expected
+ echo ' A foo' >> $testroot/stdout.expected
+ echo "? unversioned-file" >> $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"
+}
+
function test_unstage_patch {
local testroot=`test_init unstage_patch`
@@ -838,6 +916,7 @@ EOF
}
run_test test_unstage_basic
+run_test test_unstage_unversioned
run_test test_unstage_patch
run_test test_unstage_patch_added
run_test test_unstage_patch_removed