revert: actually check for a dirty index
The previous code mistakenly used wt_status_prepare to check whether the index had anything commitable in it; however, that function is just an init function, and will never report a dirty index. The correct way with wt_status_* would be to call wt_status_print with the output pointing to /dev/null or similar. However, that does extra work by both examining the working tree and spewing status information to nowhere. Instead, let's just implement the useful subset of wt_status_print as an "is_index_dirty" function. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 3, 2008 at 01:30 UTC
0f2d4476c12c15106be20d53c507035e157b66f1
2 files changed
+25
-4
builtin-revert.c
+16
-4
@@ -8,6 +8,8 @@
8
#include "exec_cmd.h"
9
#include "utf8.h"
10
#include "parse-options.h"
11
+#include "diff.h"
12
+#include "revision.h"
13
14
/*
15
* This implements the builtins revert and cherry-pick.
@@ -245,6 +247,17 @@ static char *help_msg(const unsigned char *sha1)
247
return helpbuf;
248
}
249
250
+static int index_is_dirty(void)
251
+{
252
+ struct rev_info rev;
253
+ init_revisions(&rev, NULL);
254
+ setup_revisions(0, NULL, &rev, "HEAD");
255
+ DIFF_OPT_SET(&rev.diffopt, QUIET);
256
+ DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
257
+ run_diff_index(&rev, 1);
258
+ return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
259
+}
260
+
261
static int revert_or_cherry_pick(int argc, const char **argv)
262
{
263
unsigned char head[20];
@@ -273,12 +286,11 @@ static int revert_or_cherry_pick(int argc, const char **argv)
286
if (write_tree(head, 0, NULL))
287
die ("Your index file is unmerged.");
288
} else {
276
- struct wt_status s;
277
-
289
if (get_sha1("HEAD", head))
290
die ("You do not have a valid HEAD");
280
- wt_status_prepare(&s);
281
- if (s.commitable)
291
+ if (read_cache() < 0)
292
+ die("could not read the index");
293
+ if (index_is_dirty())
294
die ("Dirty index: cannot %s", me);
295
discard_cache();
296
}
t/t3501-revert-cherry-pick.sh
+9
@@ -59,4 +59,13 @@ test_expect_success 'revert after renaming branch' '
59
60
'
61
62
+test_expect_success 'revert forbidden on dirty working tree' '
63
+
64
+ echo content >extra_file &&
65
+ git add extra_file &&
66
+ test_must_fail git revert HEAD 2>errors &&
67
+ grep "Dirty index" errors
68
+
69
+'
70
+
71
test_done