git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] revert: actually check for a dirty index

From
Jeff King <peff@peff.net>
Date
Mar 2, 2008, 07:22 UTC
Message-ID
<20080302072252.GA14214@coredump.intra.peff.net>
In-Reply-To
<20080302064449.GA6334@coredump.intra.peff.net>

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>
---
I said:
> I think it should be fixable by something like the patch below, but it
> feels a little hack-ish. Should there perhaps be an equivalent to
> "wt_status_print_updated" that just finds the commitable flag?
This is much nicer, and more efficient to boot.
 builtin-revert.c              |   34 ++++++++++++++++++++++++++++++----
 t/t3501-revert-cherry-pick.sh |    9 +++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/builtin-revert.c b/builtin-revert.c
index b6dee6a..15ab969 100644
--- a/builtin-revert.c
+++ b/builtin-revert.c
@@ -9,6 +9,9 @@
 #include "utf8.h"
 #include "parse-options.h"
 #include "cache-tree.h"
+#include "diff.h"
+#include "diffcore.h"
+#include "revision.h"
 
 /*
  * This implements the builtins revert and cherry-pick.
@@ -246,6 +249,30 @@ static char *help_msg(const unsigned char *sha1)
 	return helpbuf;
 }
 
+static void index_is_dirty_cb(struct diff_queue_struct *q,
+		struct diff_options *options,
+		void *data)
+{
+	int *is_dirty = data;
+	if (q->nr > 0)
+		*is_dirty = 1;
+}
+
+static int index_is_dirty(void)
+{
+	struct rev_info rev;
+	int is_dirty = 0;
+
+	init_revisions(&rev, NULL);
+	setup_revisions(0, NULL, &rev, "HEAD");
+	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
+	rev.diffopt.format_callback = index_is_dirty_cb;
+	rev.diffopt.format_callback_data = &is_dirty;
+	run_diff_index(&rev, 1);
+
+	return is_dirty;
+}
+
 static int revert_or_cherry_pick(int argc, const char **argv)
 {
 	unsigned char head[20];
@@ -274,12 +301,11 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 		if (write_cache_as_tree(head, 0, NULL))
 			die ("Your index file is unmerged.");
 	} else {
-		struct wt_status s;
-
 		if (get_sha1("HEAD", head))
 			die ("You do not have a valid HEAD");
-		wt_status_prepare(&s);
-		if (s.commitable)
+		if(read_cache() < 0)
+			die("could not read the index");
+		if (index_is_dirty())
 			die ("Dirty index: cannot %s", me);
 		discard_cache();
 	}
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index 2dbe04f..6da2128 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -59,4 +59,13 @@ test_expect_success 'revert after renaming branch' '
 
 '
 
+test_expect_success 'revert forbidden on dirty working tree' '
+
+	echo content >extra_file &&
+	git add extra_file &&
+	test_must_fail git revert HEAD 2>errors &&
+	grep "Dirty index" errors
+
+'
+
 test_done
-- 
1.5.4.3.365.gc1491.dirty
Previous: Jeff KingNext: Alex Riesen
Message 4 of 7 in “bug in git-revert”
  1. Jeff KingMar 2, 2008
  2. Johannes SchindelinMar 2, 2008
  3. Jeff KingMar 2, 2008
  4. revert: actually check for a dirty indexJeff King, Mar 2, 2008
  5. Alex RiesenMar 2, 2008
  6. Jeff KingMar 3, 2008
  7. Junio C HamanoMar 3, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.