add -s option to 'got remove' which deletes files in a particular status This makes it easy to deal with files that were deleted from disk by external tooling which modified the work tree. Such files are left in missing (!) status and can now be marked for deletion in bulk via 'got rm -s\! -R .' For consistency, modified (M) files can now be removed with 'got rm -s M' which implies 'got rm -f'. Prompted by feedback from krw@
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 261 262
diff --git a/got/got.1 b/got/got.1
index 8f04918..19122e3 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1105,7 +1105,7 @@ With -R, add files even if they match a
.Cm got status
ignore pattern.
.El
-.It Cm remove Oo Fl f Oc Oo Fl k Oc Oo Fl R Oc Ar path ...
+.It Cm remove Oo Fl f Oc Oo Fl k Oc Oo Fl R Oc Oo Fl s Ar status-codes Oc Ar path ...
Remove versioned files from a work tree and schedule them for deletion
from the repository in the next commit.
.Pp
@@ -1124,6 +1124,18 @@ If this option is not specified,
will refuse to run if a specified
.Ar path
is a directory.
+.It Fl s Ar status-codes
+Only delete files with a modification status matching one of the
+single-character status codes contained in the
+.Ar status-codes
+argument.
+The following status codes may be specified:
+.Bl -column YXZ description
+.It M Ta modified file (this implies the
+.Fl f
+option)
+.It ! Ta versioned file expected on disk but missing
+.El
.El
.It Cm rm
Short alias for
diff --git a/got/got.c b/got/got.c
index 93fe92b..c344b32 100644
--- a/got/got.c
+++ b/got/got.c
@@ -6022,8 +6022,8 @@ done:
__dead static void
usage_remove(void)
{
- fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
- getprogname());
+ fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
+ "path ...\n", getprogname());
exit(1);
}
@@ -6047,14 +6047,15 @@ cmd_remove(int argc, char *argv[])
const struct got_error *error = NULL;
struct got_worktree *worktree = NULL;
struct got_repository *repo = NULL;
+ const char *status_codes = NULL;
char *cwd = NULL;
struct got_pathlist_head paths;
struct got_pathlist_entry *pe;
- int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
+ int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
TAILQ_INIT(&paths);
- while ((ch = getopt(argc, argv, "fkR")) != -1) {
+ while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
switch (ch) {
case 'f':
delete_local_mods = 1;
@@ -6065,6 +6066,21 @@ cmd_remove(int argc, char *argv[])
case 'R':
can_recurse = 1;
break;
+ case 's':
+ for (i = 0; i < strlen(optarg); i++) {
+ switch (optarg[i]) {
+ case GOT_STATUS_MODIFY:
+ delete_local_mods = 1;
+ break;
+ case GOT_STATUS_MISSING:
+ break;
+ default:
+ errx(1, "invalid status code '%c'",
+ optarg[i]);
+ }
+ }
+ status_codes = optarg;
+ break;
default:
usage_remove();
/* NOTREACHED */
@@ -6138,7 +6154,8 @@ cmd_remove(int argc, char *argv[])
}
error = got_worktree_schedule_delete(worktree, &paths,
- delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
+ delete_local_mods, status_codes, print_remove_status, NULL,
+ repo, keep_on_disk);
done:
if (repo)
got_repo_close(repo);
diff --git a/include/got_worktree.h b/include/got_worktree.h
index f0548ea..c655d81 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -187,8 +187,8 @@ 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, got_worktree_delete_cb, void *,
- struct got_repository *, int);
+ struct got_pathlist_head *, int, const char *,
+ got_worktree_delete_cb, void *, struct got_repository *, 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 68b5fac..8afc4c2 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -3742,6 +3742,7 @@ struct schedule_deletion_args {
struct got_repository *repo;
int delete_local_mods;
int keep_on_disk;
+ const char *status_codes;
};
static const struct got_error *
@@ -3776,6 +3777,28 @@ schedule_for_deletion(void *arg, unsigned char status,
if (err)
goto done;
+ if (a->status_codes) {
+ size_t ncodes = strlen(a->status_codes);
+ int i;
+ for (i = 0; i < ncodes ; i++) {
+ if (status == a->status_codes[i])
+ break;
+ }
+ if (i == ncodes) {
+ /* Do not delete files in non-matching status. */
+ free(ondisk_path);
+ return NULL;
+ }
+ if (a->status_codes[i] != GOT_STATUS_MODIFY &&
+ a->status_codes[i] != GOT_STATUS_MISSING) {
+ static char msg[64];
+ snprintf(msg, sizeof(msg),
+ "invalid status code '%c'", a->status_codes[i]);
+ err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
+ goto done;
+ }
+ }
+
if (status != GOT_STATUS_NO_CHANGE) {
if (status == GOT_STATUS_DELETE)
goto done;
@@ -3837,6 +3860,7 @@ done:
const struct got_error *
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)
{
@@ -3861,6 +3885,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.status_codes = status_codes;
TAILQ_FOREACH(pe, paths, entry) {
err = worktree_status(worktree, pe->path, fileindex, repo,
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index 73d86ed..e159de3 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -438,6 +438,83 @@ function test_rm_symlink {
test_done "$testroot" "$ret"
}
+function test_rm_status_code {
+ local testroot=`test_init rm_status_code`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified beta" > $testroot/wt/beta
+
+ echo "got: invalid status code 'x'" > $testroot/stderr.expected
+ (cd $testroot/wt && got rm -s Mx beta 2>$testroot/stderr)
+
+ 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
+
+ rm $testroot/wt/epsilon/zeta # put file into 'missing' status
+
+ echo 'D epsilon/zeta' > $testroot/stdout.expected
+ (cd $testroot/wt && got rm -R -s '!' . >$testroot/stdout)
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+
+ if [ ! -e $testroot/wt/beta ]; then
+ echo "file beta was unexpectedly removed from disk" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ # put file into 'missing' status again
+ (cd $testroot/wt && got revert epsilon/zeta > /dev/null)
+ rm $testroot/wt/epsilon/zeta
+
+ echo 'D beta' > $testroot/stdout.expected
+ echo 'D epsilon/zeta' >> $testroot/stdout.expected
+ (cd $testroot/wt && got rm -R -s 'M!' . >$testroot/stdout)
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ if [ -e $testroot/wt/beta ]; then
+ echo "removed file beta still exists on disk" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo 'D beta' > $testroot/stdout.expected
+ echo 'D epsilon/zeta' >> $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
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ test_done "$testroot" "$ret"
+}
+
+
test_parseargs "$@"
run_test test_rm_basic
run_test test_rm_with_local_mods
@@ -447,3 +524,4 @@ run_test test_rm_directory
run_test test_rm_directory_keep_files
run_test test_rm_subtree
run_test test_rm_symlink
+run_test test_rm_status_code