commit 0f6d7415053c3cb56d6d4bafb624866ae4554f3e from: Stefan Sperling date: Thu Aug 08 15:21:07 2019 UTC implement got revert -R commit - 65084dad1e0e79f61fa736536c15cb512df73d1d commit + 0f6d7415053c3cb56d6d4bafb624866ae4554f3e blob - d82cd6ad862e53e3b7071ce2233d5415db93e980 blob + adc87bdc25abaa098ea84b6d2eacd6ce9a6fb9d9 --- TODO +++ 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' blob - 6f1d07be09e56fd78eb314117b691196db3d5b40 blob + 7ed26ac56b31f616e7c25df822075c33721cfff9 --- got/got.1 +++ got/got.1 @@ -509,7 +509,7 @@ Perform the operation even if a file contains uncommit .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 . blob - 79ade469edb490ca280a4a6535c55aae69bee2cd blob + 079cc2a7e0ea6073669d3006c65ae8cdf10eb1ec --- got/got.c +++ 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) blob - 614e6c76c23b8eea0e4551a3f7ac9adff5d379ce blob + 67677ab2ab97f83a684ffa2a5abaaf6c5e078d38 --- regress/cmdline/revert.sh +++ regress/cmdline/revert.sh @@ -250,13 +250,74 @@ 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 + diff -u $testroot/stderr.expected $testroot/stderr + fi + 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 @@ -265,3 +326,4 @@ 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 blob - b243918da52c5dd89f59a9cf7576437b4a03ea4b blob + d21ecfe6a104e79385d30557b70718afef39b876 --- regress/cmdline/stage.sh +++ regress/cmdline/stage.sh @@ -792,6 +792,48 @@ function test_stage_revert { echo ' M alpha' > $testroot/stdout.expected echo ' D beta' >> $testroot/stdout.expected echo ' A 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 + 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="$?"