Re: [PATCH] revert: actually check for a dirty index

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Mar 02, 2008 at 11:37:53AM +0100, Alex Riesen wrote:

> Wouldn't something like what built-commit does more effective?

I assumed git-commit used wt_status_print, and it does, but only for
some code paths. When possible, it uses the more efficient version you
mentioned.

So here is my patch respun with the same method. I considered making a
globally available "is_index_dirty" function, but I think there are a
lot of assumptions that make such a function more trouble than the 6
lines it saves. E.g., "dirty versus what?", "which index?", "has the
index been loaded already?"

Thanks Alex.

-Peff

-- >8 --
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.
---
 builtin-revert.c              |   20 ++++++++++++++++----
 t/t3501-revert-cherry-pick.sh |    9 +++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/builtin-revert.c b/builtin-revert.c
index b6dee6a..1989f96 100644
--- a/builtin-revert.c
+++ b/builtin-revert.c
@@ -9,6 +9,8 @@
 #include "utf8.h"
 #include "parse-options.h"
 #include "cache-tree.h"
+#include "diff.h"
+#include "revision.h"
 
 /*
  * This implements the builtins revert and cherry-pick.
@@ -246,6 +248,17 @@ static char *help_msg(const unsigned char *sha1)
 	return helpbuf;
 }
 
+static int index_is_dirty(void)
+{
+	struct rev_info rev;
+	init_revisions(&rev, NULL);
+	setup_revisions(0, NULL, &rev, "HEAD");
+	DIFF_OPT_SET(&rev.diffopt, QUIET);
+	DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
+	run_diff_index(&rev, 1);
+	return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
+}
+
 static int revert_or_cherry_pick(int argc, const char **argv)
 {
 	unsigned char head[20];
@@ -274,12 +287,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.366.gb96b1.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux