implement got revert -R
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
diff --git a/TODO b/TODO
index d82cd6a..adc87bd 100644
--- a/TODO
+++ b/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'
diff --git a/got/got.1 b/got/got.1
index 6f1d07b..7ed26ac 100644
--- a/got/got.1
+++ b/got/got.1
@@ -509,7 +509,7 @@ Perform the operation even if a file contains uncommitted modifications.
.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 .
diff --git a/got/got.c b/got/got.c
index 79ade46..079cc2a 100644
--- a/got/got.c
+++ b/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)
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index 614e6c7..67677ab 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -250,7 +250,7 @@ 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
@@ -259,9 +259,71 @@ function test_revert_no_arguments {
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
run_test test_revert_rm
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
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index b243918..d21ecfe 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -797,6 +797,48 @@ function test_stage_revert {
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="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
fi
test_done "$testroot" "$ret"
}