add initial implementation of '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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
diff --git a/got/got.1 b/got/got.1
index 265c985..785da13 100644
--- a/got/got.1
+++ b/got/got.1
@@ -292,6 +292,19 @@ are as follows:
.It Fl f
Perform the operation even if the file contains uncommitted modifications.
.El
+.It Cm revert Ar file-path
+Revert any uncommited changes in the file at the specified path.
+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
+.Cm got add
+it will become an unversioned file again.
+If the file was deleted with
+.Cm got rm
+it will be restored.
.El
.Sh EXIT STATUS
.Ex -std got
diff --git a/got/got.c b/got/got.c
index 4a3c791..e083023 100644
--- a/got/got.c
+++ b/got/got.c
@@ -79,6 +79,7 @@ __dead static void usage_status(void);
__dead static void usage_ref(void);
__dead static void usage_add(void);
__dead static void usage_rm(void);
+__dead static void usage_revert(void);
static const struct got_error* cmd_checkout(int, char *[]);
static const struct got_error* cmd_update(int, char *[]);
@@ -90,6 +91,7 @@ static const struct got_error* cmd_status(int, char *[]);
static const struct got_error* cmd_ref(int, char *[]);
static const struct got_error* cmd_add(int, char *[]);
static const struct got_error* cmd_rm(int, char *[]);
+static const struct got_error* cmd_revert(int, char *[]);
static struct cmd got_commands[] = {
{ "checkout", cmd_checkout, usage_checkout,
@@ -112,6 +114,8 @@ static struct cmd got_commands[] = {
"add a new file to version control" },
{ "rm", cmd_rm, usage_rm,
"remove a versioned file" },
+ { "revert", cmd_revert, usage_revert,
+ "revert uncommitted changes" },
};
int
@@ -1983,3 +1987,79 @@ done:
free(cwd);
return error;
}
+
+__dead static void
+usage_revert(void)
+{
+ fprintf(stderr, "usage: %s revert file-path\n", getprogname());
+ exit(1);
+}
+
+static void
+revert_progress(void *arg, unsigned char status, const char *path)
+{
+ while (path[0] == '/')
+ path++;
+ printf("%c %s\n", status, path);
+}
+
+static const struct got_error *
+cmd_revert(int argc, char *argv[])
+{
+ const struct got_error *error = NULL;
+ struct got_worktree *worktree = NULL;
+ struct got_repository *repo = NULL;
+ char *cwd = NULL, *path = NULL;
+ int ch;
+
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage_revert();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage_revert();
+
+ path = realpath(argv[0], NULL);
+ if (path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+ error = got_worktree_open(&worktree, cwd);
+ if (error)
+ goto done;
+
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ if (error != NULL)
+ goto done;
+
+ error = apply_unveil(got_repo_get_path(repo), 1,
+ got_worktree_get_root_path(worktree));
+ if (error)
+ goto done;
+
+ error = got_worktree_revert(worktree, path,
+ revert_progress, NULL, repo);
+ if (error)
+ goto done;
+done:
+ if (repo)
+ got_repo_close(repo);
+ if (worktree)
+ got_worktree_close(worktree);
+ free(path);
+ free(cwd);
+ return error;
+}
diff --git a/include/got_object.h b/include/got_object.h
index 2bd9503..6baad61 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -153,6 +153,10 @@ void got_object_tree_close(struct got_tree_object *);
const struct got_tree_entries *got_object_tree_get_entries(
struct got_tree_object *);
+/* Find a particular entry in a tree. */
+const struct got_tree_entry *got_object_tree_find_entry(
+ struct got_tree_object *, const char *);
+
/*
* Compare two trees and indicate whether the entry at the specified path
* differs between them. The path must not be the root path "/"; the function
diff --git a/include/got_worktree.h b/include/got_worktree.h
index d6cae67..d49c5f8 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -28,6 +28,7 @@ struct got_worktree;
#define GOT_STATUS_MISSING '!'
#define GOT_STATUS_UNVERSIONED '?'
#define GOT_STATUS_OBSTRUCTED '~'
+#define GOT_STATUS_REVERT 'R'
/*
* Attempt to initialize a new work tree on disk.
@@ -109,7 +110,7 @@ typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
* void * argument, and the path of each checked out file.
*/
const struct got_error *got_worktree_checkout_files(struct got_worktree *,
- struct got_repository *, got_worktree_checkout_cb progress, void *,
+ struct got_repository *, got_worktree_checkout_cb, void *,
got_worktree_cancel_cb, void *);
/* A callback function which is invoked to report a path's status. */
@@ -144,3 +145,10 @@ const struct got_error *got_worktree_schedule_add(struct got_worktree *,
const struct got_error *
got_worktree_schedule_delete(struct got_worktree *, const char *, int,
got_worktree_status_cb, void *, struct got_repository *);
+
+/*
+ * Revert a file at the specified path such that it matches its
+ * 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 *);
diff --git a/lib/object.c b/lib/object.c
index 911aa0e..eb89806 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1354,6 +1354,12 @@ find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
return NULL;
}
+const struct got_tree_entry *
+got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
+{
+ return find_entry_by_name(tree, name, strlen(name));
+}
+
const struct got_error *
got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
struct got_object_id *commit_id, const char *path)
diff --git a/lib/path.c b/lib/path.c
index 487e120..917cd5d 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -154,6 +154,12 @@ got_path_is_root_dir(const char *path)
}
int
+got_path_is_current_dir(const char *path)
+{
+ return (path[0] == '.' && path[1] == '\0');
+}
+
+int
got_path_is_child(const char *child, const char *parent, size_t parent_len)
{
if (parent_len == 0)
diff --git a/lib/worktree.c b/lib/worktree.c
index 694339a..106aa6f 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -871,8 +871,8 @@ static const struct got_error *
install_blob(struct got_worktree *worktree, const char *ondisk_path,
const char *path, uint16_t te_mode, uint16_t st_mode,
struct got_blob_object *blob, int restoring_missing_file,
- struct got_repository *repo, got_worktree_checkout_cb progress_cb,
- void *progress_arg)
+ int reverting_versioned_file, struct got_repository *repo,
+ got_worktree_checkout_cb progress_cb, void *progress_arg)
{
const struct got_error *err = NULL;
int fd = -1;
@@ -913,6 +913,8 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
if (restoring_missing_file)
(*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
+ else if (reverting_versioned_file)
+ (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
else
(*progress_cb)(progress_arg,
update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
@@ -1170,8 +1172,8 @@ update_blob(struct got_worktree *worktree,
goto done;
} else {
err = install_blob(worktree, ondisk_path, path, te->mode,
- sb.st_mode, blob, status == GOT_STATUS_MISSING, repo,
- progress_cb, progress_arg);
+ sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
+ repo, progress_cb, progress_arg);
if (err)
goto done;
err = update_blob_fileindex_entry(worktree, fileindex, ie,
@@ -1924,3 +1926,184 @@ done:
err = unlockerr;
return err;
}
+
+const struct got_error *
+got_worktree_revert(struct got_worktree *worktree,
+ 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, *new_fileindex_path = NULL;
+ char *tree_path = NULL, *parent_path, *te_name;
+ FILE *index = NULL, *new_index = NULL;
+ const struct got_error *err = NULL, *unlockerr = NULL;
+ struct got_tree_object *tree = NULL;
+ struct got_object_id id, *tree_id = NULL;
+ const struct got_tree_entry *te;
+ 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();
+ 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();
+ fileindex_path = NULL;
+ goto done;
+ }
+
+ index = fopen(fileindex_path, "rb");
+ if (index == NULL) {
+ err = got_error_from_errno();
+ 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);
+ goto done;
+ }
+
+ /* Construct in-repository path of tree which contains this blob. */
+ err = got_path_dirname(&parent_path, ie->path);
+ if (err) {
+ if (err->code != GOT_ERR_BAD_PATH)
+ goto done;
+ parent_path = "/";
+ }
+ if (got_path_is_root_dir(worktree->path_prefix)) {
+ tree_path = strdup(parent_path);
+ if (tree_path == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ } else {
+ if (got_path_is_root_dir(parent_path)) {
+ tree_path = strdup(worktree->path_prefix);
+ if (tree_path == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ } else {
+ if (asprintf(&tree_path, "%s/%s",
+ worktree->path_prefix, parent_path) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ }
+ }
+
+ err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
+ tree_path);
+ if (err)
+ goto done;
+
+ err = got_object_open_as_tree(&tree, repo, tree_id);
+ if (err)
+ goto done;
+
+ te_name = basename(ie->path);
+ if (te_name == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ err = get_file_status(&status, &sb, ie, ondisk_path, repo);
+ if (err)
+ goto done;
+
+ te = got_object_tree_find_entry(tree, te_name);
+ if (te == NULL && status != GOT_STATUS_ADD) {
+ err = got_error(GOT_ERR_NO_TREE_ENTRY);
+ goto done;
+ }
+
+ switch (status) {
+ case GOT_STATUS_ADD:
+ (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
+ got_fileindex_entry_remove(fileindex, ie);
+ break;
+ case GOT_STATUS_DELETE:
+ case GOT_STATUS_MODIFY:
+ case GOT_STATUS_CONFLICT:
+ case GOT_STATUS_MISSING:
+ memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
+ err = got_object_open_as_blob(&blob, repo, &id, 8192);
+ if (err)
+ goto done;
+ err = install_blob(worktree, ondisk_path, ie->path,
+ te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
+ progress_arg);
+ if (err)
+ goto done;
+ if (status == GOT_STATUS_DELETE) {
+ err = update_blob_fileindex_entry(worktree,
+ fileindex, ie, ondisk_path, ie->path, blob, 1);
+ if (err)
+ goto done;
+ }
+ break;
+ default:
+ goto done;
+ }
+
+ err = got_opentemp_named(&new_fileindex_path, &new_index,
+ fileindex_path);
+ if (err)
+ goto done;
+
+ err = got_fileindex_write(fileindex, new_index);
+ if (err)
+ goto done;
+
+ if (rename(new_fileindex_path, fileindex_path) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ free(new_fileindex_path);
+ new_fileindex_path = NULL;
+done:
+ free(relpath);
+ free(tree_path);
+ if (blob)
+ got_object_blob_close(blob);
+ if (tree)
+ got_object_tree_close(tree);
+ free(tree_id);
+ if (index) {
+ if (fclose(index) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
+ if (new_fileindex_path) {
+ if (unlink(new_fileindex_path) != 0 && err == NULL)
+ err = got_error_from_errno();
+ free(new_fileindex_path);
+ }
+ if (fileindex)
+ got_fileindex_free(fileindex);
+ unlockerr = lock_worktree(worktree, LOCK_SH);
+ if (unlockerr && err == NULL)
+ err = unlockerr;
+ return err;
+}
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
new file mode 100755
index 0000000..4c59fbd
--- /dev/null
+++ b/regress/cmdline/revert.sh
@@ -0,0 +1,140 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+function test_revert_basic {
+ local testroot=`test_init revert_basic`
+
+ 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 'R alpha' > $testroot/stdout.expected
+
+ (cd $testroot/wt && got revert alpha > $testroot/stdout)
+
+ cmp $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 $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ fi
+ test_done "$testroot" "$ret"
+
+}
+
+function test_revert_rm {
+ local testroot=`test_init revert_rm`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rm beta >/dev/null)
+
+ echo 'R beta' > $testroot/stdout.expected
+
+ (cd $testroot/wt && got revert beta > $testroot/stdout)
+
+ cmp $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 "beta" > $testroot/content.expected
+ cat $testroot/wt/beta > $testroot/content
+
+ cmp $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ fi
+ test_done "$testroot" "$ret"
+}
+
+function test_revert_add {
+ local testroot=`test_init revert_add`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "new file" > $testroot/wt/new
+ (cd $testroot/wt && got add new >/dev/null)
+
+ echo 'R new' > $testroot/stdout.expected
+
+ (cd $testroot/wt && got revert new > $testroot/stdout)
+
+ cmp $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 "new file" > $testroot/content.expected
+ cat $testroot/wt/new > $testroot/content
+
+ cmp $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 '? new' > $testroot/stdout.expected
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ cmp $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+run_test test_revert_basic
+run_test test_revert_rm
+run_test test_revert_add