Commit 766841c2970cb5bef66c9c69201b231d0eefb120

Stefan Sperling 2020-08-13T19:12:57

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@

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