Commit 0f6d7415053c3cb56d6d4bafb624866ae4554f3e

Stefan Sperling 2019-08-08T17:21:07

implement got revert -R

diff --git a/TODO b/TODO
index d82cd6a..adc87bd 100644
--- a/TODO
+++ b/TODO
@@ -15,7 +15,6 @@ got:
 - 'histedit -c' prompts for log message even if there are no changes to commit
 - recursive addition: got add -R
 - recursive removal: got rm -R
-- recursive revert: got revert -R
 - add 'got stage' command with 'git add -p' style functionality
 - add 'got unstage' command to undo effects of 'got stage'
 - add 'git checkout -p' style functionality to 'got revert' and 'got unstage'
diff --git a/got/got.1 b/got/got.1
index 6f1d07b..7ed26ac 100644
--- a/got/got.1
+++ b/got/got.1
@@ -509,7 +509,7 @@ Perform the operation even if a file contains uncommitted modifications.
 .It Cm rm
 Short alias for
 .Cm remove .
-.It Cm revert Ar file-path ...
+.It Cm revert [ Fl R ] Ar path ...
 Revert any uncommited changes in files at the specified paths.
 File contents will be overwritten with those contained in the
 work tree's base commit. There is no way to bring discarded
@@ -522,6 +522,19 @@ it will become an unversioned file again.
 If a file was deleted with
 .Cm got remove
 it will be restored.
+.Pp
+The options for
+.Cm got revert
+are as follows:
+.Bl -tag -width Ds
+.It Fl R
+Permit recursion into directories.
+If this option is not specified,
+.Cm got revert
+will refuse to run if a specified
+.Ar path
+is a directory.
+.El
 .It Cm rv
 Short alias for
 .Cm revert .
diff --git a/got/got.c b/got/got.c
index 79ade46..079cc2a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3104,7 +3104,7 @@ done:
 __dead static void
 usage_revert(void)
 {
-	fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
+	fprintf(stderr, "usage: %s revert [-R] path ...\n", getprogname());
 	exit(1);
 }
 
@@ -3125,12 +3125,16 @@ cmd_revert(int argc, char *argv[])
 	struct got_repository *repo = NULL;
 	char *cwd = NULL, *path = NULL;
 	struct got_pathlist_head paths;
-	int ch;
+	struct got_pathlist_entry *pe;
+	int ch, can_recurse = 0;
 
 	TAILQ_INIT(&paths);
 
-	while ((ch = getopt(argc, argv, "")) != -1) {
+	while ((ch = getopt(argc, argv, "R")) != -1) {
 		switch (ch) {
+		case 'R':
+			can_recurse = 1;
+			break;
 		default:
 			usage_revert();
 			/* NOTREACHED */
@@ -3170,6 +3174,35 @@ cmd_revert(int argc, char *argv[])
 	if (error)
 		goto done;
 
+	if (!can_recurse) {
+		char *ondisk_path;
+		struct stat sb;
+		TAILQ_FOREACH(pe, &paths, entry) {
+			if (asprintf(&ondisk_path, "%s/%s",
+			    got_worktree_get_root_path(worktree),
+			       pe->path) == -1) {
+				error = got_error_from_errno("asprintf");
+				goto done;
+			}
+			if (lstat(ondisk_path, &sb) == -1) {
+				if (errno == ENOENT) {
+					free(ondisk_path);
+					continue;
+				}
+				error = got_error_from_errno2("lstat",
+				    ondisk_path);
+				free(ondisk_path);
+				goto done;
+			}
+			free(ondisk_path);
+			if (S_ISDIR(sb.st_mode)) {
+				error = got_error_msg(GOT_ERR_BAD_PATH,
+				    "reverting directories requires -R option");
+				goto done;
+			}
+		}
+	}
+
 	error = got_worktree_revert(worktree, &paths,
 	    revert_progress, NULL, repo);
 	if (error)
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index 614e6c7..67677ab 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -250,7 +250,7 @@ function test_revert_no_arguments {
 		return 1
 	fi
 
-	echo "usage: got revert file-path ..." > $testroot/stderr.expected
+	echo "usage: got revert [-R] path ..." > $testroot/stderr.expected
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret="$?"
 	if [ "$ret" != "0" ]; then
@@ -259,9 +259,71 @@ function test_revert_no_arguments {
 	test_done "$testroot" "$ret"
 }
 
+function test_revert_directory {
+	local testroot=`test_init revert_directory`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
+
+	(cd $testroot/wt && got revert . > $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got revert command succeeded unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+	echo "got: reverting directories requires -R option" \
+		> $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
+
+	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
+
+	(cd $testroot/wt && got revert -R . > $testroot/stdout)
+
+	echo 'R  epsilon/zeta' > $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 "zeta" > $testroot/content.expected
+	cat $testroot/wt/epsilon/zeta > $testroot/content
+
+	cmp -s $testroot/content.expected $testroot/content
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/content.expected $testroot/content
+	fi
+	test_done "$testroot" "$ret"
+
+}
+
 run_test test_revert_basic
 run_test test_revert_rm
 run_test test_revert_add
 run_test test_revert_multiple
 run_test test_revert_file_in_new_subdir
 run_test test_revert_no_arguments
+run_test test_revert_directory
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index b243918..d21ecfe 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -797,6 +797,48 @@ function test_stage_revert {
 	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
+	echo "modified added file again" >> $testroot/wt/foo
+
+	(cd $testroot/wt && got revert -R . > $testroot/stdout \
+		2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "revert command succeeded unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "R  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 "got: beta: file is staged" > $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
+
+	echo ' M alpha' > $testroot/stdout.expected
+	echo ' D beta' >> $testroot/stdout.expected
+	echo 'MA 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"
 }