allow multiple file path arguments for 'got revert'
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
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