make 'got rm' behave like rm(1) for paths found missing on disk ok millert@
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
diff --git a/got/got.1 b/got/got.1
index 1ba9101..eaafde8 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1259,7 +1259,10 @@ The options for
are as follows:
.Bl -tag -width Ds
.It Fl f
-Perform the operation even if a file contains local modifications.
+Perform the operation even if a file contains local modifications,
+and do not raise an error if a specified
+.Ar path
+does not exist on disk.
.It Fl k
Keep affected files on disk.
.It Fl R
diff --git a/got/got.c b/got/got.c
index f551f71..16b939d 100644
--- a/got/got.c
+++ b/got/got.c
@@ -6981,6 +6981,7 @@ cmd_remove(int argc, char *argv[])
struct got_pathlist_head paths;
struct got_pathlist_entry *pe;
int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
+ int ignore_missing_paths = 0;
TAILQ_INIT(&paths);
@@ -6988,6 +6989,7 @@ cmd_remove(int argc, char *argv[])
switch (ch) {
case 'f':
delete_local_mods = 1;
+ ignore_missing_paths = 1;
break;
case 'k':
keep_on_disk = 1;
@@ -7002,6 +7004,7 @@ cmd_remove(int argc, char *argv[])
delete_local_mods = 1;
break;
case GOT_STATUS_MISSING:
+ ignore_missing_paths = 1;
break;
default:
errx(1, "invalid status code '%c'",
@@ -7084,7 +7087,7 @@ cmd_remove(int argc, char *argv[])
error = got_worktree_schedule_delete(worktree, &paths,
delete_local_mods, status_codes, print_remove_status, NULL,
- repo, keep_on_disk);
+ repo, keep_on_disk, ignore_missing_paths);
done:
if (repo) {
const struct got_error *close_err = got_repo_close(repo);
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 2402196..2a33834 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -194,7 +194,7 @@ const struct got_error *got_worktree_schedule_add(struct got_worktree *,
const struct got_error *
got_worktree_schedule_delete(struct got_worktree *,
struct got_pathlist_head *, int, const char *,
- got_worktree_delete_cb, void *, struct got_repository *, int);
+ got_worktree_delete_cb, void *, struct got_repository *, int, int);
/* A callback function which is used to select or reject a patch. */
typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
diff --git a/lib/worktree.c b/lib/worktree.c
index 03ed920..7b9584f 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -3954,6 +3954,7 @@ struct schedule_deletion_args {
struct got_repository *repo;
int delete_local_mods;
int keep_on_disk;
+ int ignore_missing_paths;
const char *status_codes;
};
@@ -3969,6 +3970,12 @@ schedule_for_deletion(void *arg, unsigned char status,
struct stat sb;
char *ondisk_path;
+ if (status == GOT_STATUS_NONEXISTENT) {
+ if (a->ignore_missing_paths)
+ return NULL;
+ return got_error_set_errno(ENOENT, relpath);
+ }
+
ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
if (ie == NULL)
return got_error_path(relpath, GOT_ERR_BAD_PATH);
@@ -4018,6 +4025,10 @@ schedule_for_deletion(void *arg, unsigned char status,
err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
goto done;
}
+ if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
+ err = got_error_set_errno(ENOENT, relpath);
+ goto done;
+ }
if (status != GOT_STATUS_MODIFY &&
status != GOT_STATUS_MISSING) {
err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
@@ -4073,7 +4084,7 @@ got_worktree_schedule_delete(struct got_worktree *worktree,
struct got_pathlist_head *paths, int delete_local_mods,
const char *status_codes,
got_worktree_delete_cb progress_cb, void *progress_arg,
- struct got_repository *repo, int keep_on_disk)
+ struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
{
struct got_fileindex *fileindex = NULL;
char *fileindex_path = NULL;
@@ -4096,6 +4107,7 @@ got_worktree_schedule_delete(struct got_worktree *worktree,
sda.repo = repo;
sda.delete_local_mods = delete_local_mods;
sda.keep_on_disk = keep_on_disk;
+ sda.ignore_missing_paths = ignore_missing_paths;
sda.status_codes = status_codes;
TAILQ_FOREACH(pe, paths, entry) {
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index cab753f..8b4c659 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -159,8 +159,51 @@ test_rm_and_add_elsewhere() {
return 1
fi
+ (cd $testroot/wt && got rm alpha > $testroot/stdout 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got rm command succeeded unexpectedly" >&2
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo -n '' > $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 "got: alpha: 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
+
+ (cd $testroot/wt && got rm -f alpha > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got rm command failed unexpectedly" >&2
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
echo 'D alpha' > $testroot/stdout.expected
- (cd $testroot/wt && got rm alpha > $testroot/stdout)
+ 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
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -514,6 +557,78 @@ test_rm_status_code() {
test_done "$testroot" "$ret"
}
+test_rm_nonexistent_directory() {
+ local testroot=`test_init rm_nonexistent_directory`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ rm -r $testroot/wt/epsilon
+
+ (cd $testroot/wt && got rm epsilon > $testroot/stdout \
+ 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got rm command succeeded unexpectedly" >&2
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo -n '' > $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 "got: epsilon: 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
+
+ (cd $testroot/wt && got rm -f epsilon > $testroot/stdout \
+ 2> $testroot/stderr)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got rm command failed unexpectedly" >&2
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo -n '' > $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 -n '' > $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
+
+ test_done "$testroot" "$ret"
+}
+
test_parseargs "$@"
run_test test_rm_basic
@@ -525,3 +640,4 @@ run_test test_rm_directory_keep_files
run_test test_rm_subtree
run_test test_rm_symlink
run_test test_rm_status_code
+run_test test_rm_nonexistent_directory