Commit Diff


commit - 76d98825cd498e34b6d8e4cbff71641b3509bdff
commit + e20a8b6f0dfe647b8cff3d2fdbc06058793f35d1
blob - 4790d3daa456337e94fc78e750586101b308afa2
blob + c9d5eab615151bbeb5480b7be1367ad4ea6e6f9b
--- got/got.1
+++ 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 ]
blob - e5ae0d41cbb671551e3b2c8c02f9372d7435414c
blob + 7fdff3004641d83edc0fc7476ceaa9caa14af65f
--- got/got.c
+++ 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,8 +2319,12 @@ 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) {
 		default:
@@ -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;
blob - 91899731f8655f5772579fe3e40e0e2716cb82e3
blob + 6ab8418c059fa507bf8dfbb1f840b9f94d47a5d3
--- include/got_worktree.h
+++ 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.
blob - 2809f0b675dbf0558310513478adf02778dfe189
blob + 36d985140e0f32f8209b9707f93799f211f0ed10
--- lib/worktree.c
+++ lib/worktree.c
@@ -2373,54 +2373,25 @@ 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;
 
@@ -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");
blob - eec57c20d733b805cc1aa054046717a7aadac832
blob + 65ebe13e039ee7595a78c264b154f9aa130bc661
--- regress/cmdline/revert.sh
+++ 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