Commit e20a8b6f0dfe647b8cff3d2fdbc06058793f35d1

Stefan Sperling 2019-06-04T16:35:01

allow multiple file path arguments for 'got revert'

diff --git a/got/got.1 b/got/got.1
index 4790d3d..c9d5eab 100644
--- a/got/got.1
+++ b/got/got.1
@@ -321,17 +321,17 @@ are as follows:
 .It Fl f
 Perform the operation even if a file contains uncommitted modifications.
 .El
-.It Cm revert Ar file-path
-Revert any uncommited changes in the file at the specified path.
+.It Cm revert Ar file-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
 changes back after
 .Cm got revert !
 .Pp
-If the file was added with
+If a file was added with
 .Cm got add
 it will become an unversioned file again.
-If the file was deleted with
+If a file was deleted with
 .Cm got rm
 it will be restored.
 .It Cm commit [ Fl m Ar msg ] [ file-path ]
diff --git a/got/got.c b/got/got.c
index e5ae0d4..7fdff30 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2300,7 +2300,7 @@ done:
 __dead static void
 usage_revert(void)
 {
-	fprintf(stderr, "usage: %s revert file-path\n", getprogname());
+	fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
 	exit(1);
 }
 
@@ -2319,7 +2319,11 @@ cmd_revert(int argc, char *argv[])
 	struct got_worktree *worktree = NULL;
 	struct got_repository *repo = NULL;
 	char *cwd = NULL, *path = NULL;
-	int ch;
+	struct got_pathlist_head paths;
+	struct got_pathlist_entry *pe;
+	int ch, i;
+
+	TAILQ_INIT(&paths);
 
 	while ((ch = getopt(argc, argv, "")) != -1) {
 		switch (ch) {
@@ -2332,15 +2336,23 @@ cmd_revert(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if (argc != 1)
+	if (argc < 1)
 		usage_revert();
 
-	path = realpath(argv[0], NULL);
-	if (path == NULL) {
-		error = got_error_from_errno2("realpath", argv[0]);
-		goto done;
+	for (i = 0; i < argc; i++) {
+		char *path = realpath(argv[i], NULL);
+		if (path == NULL) {
+			error = got_error_from_errno2("realpath", argv[i]);
+			goto done;
+		}
+
+		got_path_strip_trailing_slashes(path);
+		error = got_pathlist_insert(&pe, &paths, path, NULL);
+		if (error) {
+			free(path);
+			goto done;
+		}
 	}
-	got_path_strip_trailing_slashes(path);
 
 	cwd = getcwd(NULL, 0);
 	if (cwd == NULL) {
@@ -2360,7 +2372,7 @@ cmd_revert(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_worktree_revert(worktree, path,
+	error = got_worktree_revert(worktree, &paths,
 	    revert_progress, NULL, repo);
 	if (error)
 		goto done;
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 9189973..6ab8418 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -174,7 +174,8 @@ got_worktree_schedule_delete(struct got_worktree *,
  * original state in the worktree's base commit.
  */
 const struct got_error *got_worktree_revert(struct got_worktree *,
-    const char *, got_worktree_checkout_cb, void *, struct got_repository *);
+    struct got_pathlist_head *, got_worktree_checkout_cb, void *,
+    struct got_repository *);
 
 /*
  * A callback function which is invoked when a commit message is requested.
diff --git a/lib/worktree.c b/lib/worktree.c
index 2809f0b..36d9851 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2373,57 +2373,28 @@ done:
 	return err;
 }
 
-const struct got_error *
-got_worktree_revert(struct got_worktree *worktree,
+static const struct got_error *
+revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
     const char *ondisk_path,
     got_worktree_checkout_cb progress_cb, void *progress_arg,
     struct got_repository *repo)
 {
-	struct got_fileindex *fileindex = NULL;
-	struct got_fileindex_entry *ie = NULL;
-	char *relpath, *fileindex_path = NULL;
-	char *tree_path = NULL, *parent_path, *te_name;
-	FILE *index = NULL;
-	const struct got_error *err = NULL, *unlockerr = NULL;
+	const struct got_error *err = NULL;
+	char *relpath = NULL, *parent_path = NULL;
+	struct got_fileindex_entry *ie;
 	struct got_tree_object *tree = NULL;
-	struct got_object_id id, *tree_id = NULL;
+	struct got_object_id *tree_id = NULL;
 	const struct got_tree_entry *te;
+	char *tree_path = NULL, *te_name;
 	struct got_blob_object *blob = NULL;
 	unsigned char status;
 	struct stat sb;
 
-	err = lock_worktree(worktree, LOCK_EX);
-	if (err)
-		return err;
-
 	err = got_path_skip_common_ancestor(&relpath,
 	    got_worktree_get_root_path(worktree), ondisk_path);
 	if (err)
 		goto done;
 
-	fileindex = got_fileindex_alloc();
-	if (fileindex == NULL) {
-		err = got_error_from_errno("got_fileindex_alloc");
-		goto done;
-	}
-
-	if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
-	    GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
-		err = got_error_from_errno("asprintf");
-		fileindex_path = NULL;
-		goto done;
-	}
-
-	index = fopen(fileindex_path, "rb");
-	if (index == NULL) {
-		err = got_error_from_errno2("fopen", fileindex_path);
-		goto done;
-	}
-
-	err = got_fileindex_read(fileindex, index);
-	if (err)
-		goto done;
-
 	ie = got_fileindex_entry_get(fileindex, relpath);
 	if (ie == NULL) {
 		err = got_error(GOT_ERR_BAD_PATH);
@@ -2435,7 +2406,11 @@ got_worktree_revert(struct got_worktree *worktree,
 	if (err) {
 		if (err->code != GOT_ERR_BAD_PATH)
 			goto done;
-		parent_path = "/";
+		parent_path = strdup("/");
+		if (parent_path == NULL) {
+			err = got_error_from_errno("strdup");
+			goto done;
+		}
 	}
 	if (got_path_is_root_dir(worktree->path_prefix)) {
 		tree_path = strdup(parent_path);
@@ -2492,7 +2467,8 @@ got_worktree_revert(struct got_worktree *worktree,
 	case GOT_STATUS_DELETE:
 	case GOT_STATUS_MODIFY:
 	case GOT_STATUS_CONFLICT:
-	case GOT_STATUS_MISSING:
+	case GOT_STATUS_MISSING: {
+		struct got_object_id id;
 		memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
 		err = got_object_open_as_blob(&blob, repo, &id, 8192);
 		if (err)
@@ -2509,21 +2485,73 @@ got_worktree_revert(struct got_worktree *worktree,
 				goto done;
 		}
 		break;
+	}
 	default:
 			goto done;
 	}
-
-	err = sync_fileindex(fileindex, fileindex_path);
-	if (err)
-		goto done;
 done:
 	free(relpath);
+	free(parent_path);
 	free(tree_path);
 	if (blob)
 		got_object_blob_close(blob);
 	if (tree)
 		got_object_tree_close(tree);
 	free(tree_id);
+	return err;
+}
+
+const struct got_error *
+got_worktree_revert(struct got_worktree *worktree,
+    struct got_pathlist_head *ondisk_paths,
+    got_worktree_checkout_cb progress_cb, void *progress_arg,
+    struct got_repository *repo)
+{
+	struct got_fileindex *fileindex = NULL;
+	char *fileindex_path = NULL;
+	FILE *index = NULL;
+	const struct got_error *err = NULL, *unlockerr = NULL;
+	const struct got_error *sync_err = NULL;
+	struct got_pathlist_entry *pe;
+
+	err = lock_worktree(worktree, LOCK_EX);
+	if (err)
+		return err;
+
+	fileindex = got_fileindex_alloc();
+	if (fileindex == NULL) {
+		err = got_error_from_errno("got_fileindex_alloc");
+		goto done;
+	}
+
+	if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
+	    GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
+		err = got_error_from_errno("asprintf");
+		fileindex_path = NULL;
+		goto done;
+	}
+
+	index = fopen(fileindex_path, "rb");
+	if (index == NULL) {
+		err = got_error_from_errno2("fopen", fileindex_path);
+		goto done;
+	}
+
+	err = got_fileindex_read(fileindex, index);
+	if (err)
+		goto done;
+
+	TAILQ_FOREACH(pe, ondisk_paths, entry) {
+		err = revert_file(worktree, fileindex, pe->path,
+		    progress_cb, progress_arg, repo);
+		if (err)
+			goto done;
+	}
+
+done:
+	sync_err = sync_fileindex(fileindex, fileindex_path);
+	if (sync_err && err == NULL)
+		err = sync_err;
 	if (index) {
 		if (fclose(index) != 0 && err == NULL)
 			err = got_error_from_errno("fclose");
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index eec57c2..65ebe13 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -135,6 +135,55 @@ function test_revert_add {
 	test_done "$testroot" "$ret"
 }
 
+function test_revert_multiple {
+	local testroot=`test_init revert_multiple`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "modified alpha" > $testroot/wt/alpha
+	echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
+
+	echo 'R  alpha' > $testroot/stdout.expected
+	echo 'R  epsilon/zeta' >> $testroot/stdout.expected
+
+	(cd $testroot/wt && got revert alpha epsilon/zeta > $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
+
+	echo "alpha" > $testroot/content.expected
+	cat $testroot/wt/alpha > $testroot/content
+
+	cmp -s $testroot/content.expected $testroot/content
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/content.expected $testroot/content
+		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