summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builtin-commit.c13
-rw-r--r--builtin-revert.c13
-rw-r--r--diff-lib.c15
-rw-r--r--diff.h2
4 files changed, 20 insertions, 23 deletions
diff --git a/builtin-commit.c b/builtin-commit.c
index d6a3a6203a..46e649cd7c 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -561,7 +561,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
commitable = run_status(fp, index_file, prefix, 1);
wt_status_use_color = saved_color_setting;
} else {
- struct rev_info rev;
unsigned char sha1[20];
const char *parent = "HEAD";
@@ -573,16 +572,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
if (get_sha1(parent, sha1))
commitable = !!active_nr;
- else {
- init_revisions(&rev, "");
- rev.abbrev = 0;
- setup_revisions(0, NULL, &rev, parent);
- DIFF_OPT_SET(&rev.diffopt, QUIET);
- DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
- run_diff_index(&rev, 1 /* cached */);
-
- commitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
- }
+ else
+ commitable = index_differs_from(parent, 0);
}
fclose(fp);
diff --git a/builtin-revert.c b/builtin-revert.c
index d48313c745..d210150671 100644
--- a/builtin-revert.c
+++ b/builtin-revert.c
@@ -223,17 +223,6 @@ 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 struct tree *empty_tree(void)
{
struct tree *tree = xcalloc(1, sizeof(struct tree));
@@ -279,7 +268,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
} else {
if (get_sha1("HEAD", head))
die ("You do not have a valid HEAD");
- if (index_is_dirty())
+ if (index_differs_from("HEAD", 0))
die ("Dirty index: cannot %s", me);
}
discard_cache();
diff --git a/diff-lib.c b/diff-lib.c
index a41e1ec07c..79d0606834 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -513,3 +513,18 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
exit(128);
return 0;
}
+
+int index_differs_from(const char *def, int diff_flags)
+{
+ struct rev_info rev;
+
+ init_revisions(&rev, NULL);
+ setup_revisions(0, NULL, &rev, def);
+ DIFF_OPT_SET(&rev.diffopt, QUIET);
+ DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
+ rev.diffopt.flags |= diff_flags;
+ run_diff_index(&rev, 1);
+ if (rev.pending.alloc)
+ free(rev.pending.objects);
+ return (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);
+}
diff --git a/diff.h b/diff.h
index 23cd90c2e6..6703a4fb4f 100644
--- a/diff.h
+++ b/diff.h
@@ -265,4 +265,6 @@ extern int diff_result_code(struct diff_options *, int);
extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
+extern int index_differs_from(const char *def, int diff_flags);
+
#endif /* DIFF_H */