summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Tan <pyokagan@gmail.com>2015-06-11 18:21:57 +0800
committerJunio C Hamano <gitster@pobox.com>2015-06-14 15:36:32 -0700
commitc9d36e0c4014d6ee62ae1e5222a79041c52472c7 (patch)
tree85af4a5c1efcad9601344369f2911ff39a8b66c3
parent177ee4942f788f832e4fb6bbf9a5442d396db263 (diff)
downloadgit-c9d36e0c4014d6ee62ae1e5222a79041c52472c7.tar.gz
am: refuse to apply patches if index is dirty
Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am will refuse to apply patches if the index is dirty. Re-implement this behavior. Signed-off-by: Paul Tan <pyokagan@gmail.com>
-rw-r--r--builtin/am.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/builtin/am.c b/builtin/am.c
index 417bfde159..234762c14d 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -14,6 +14,8 @@
#include "refs.h"
#include "commit.h"
#include "lockfile.h"
+#include "diff.h"
+#include "diffcore.h"
/**
* Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -472,6 +474,43 @@ static void refresh_and_write_cache(void)
}
/**
+ * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
+ * branch, returns 1 if there are entries in the index, 0 otherwise. If an
+ * strbuf is provided, the space-separated list of files that differ will be
+ * appended to it.
+ */
+static int index_has_changes(struct strbuf *sb)
+{
+ unsigned char head[GIT_SHA1_RAWSZ];
+ int i;
+
+ if (!get_sha1_tree("HEAD", head)) {
+ struct diff_options opt;
+
+ diff_setup(&opt);
+ DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
+ if (!sb)
+ DIFF_OPT_SET(&opt, QUICK);
+ do_diff_cache(head, &opt);
+ diffcore_std(&opt);
+ for (i = 0; sb && i < diff_queued_diff.nr; i++) {
+ if (i)
+ strbuf_addch(sb, ' ');
+ strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
+ }
+ diff_flush(&opt);
+ return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
+ } else {
+ for (i = 0; sb && i < active_nr; i++) {
+ if (i)
+ strbuf_addch(sb, ' ');
+ strbuf_addstr(sb, active_cache[i]->name);
+ }
+ return !!active_nr;
+ }
+}
+
+/**
* Parses `patch` using git-mailinfo. state->msg will be set to the patch
* message. state->author_name, state->author_email, state->author_date will be
* set to the patch author's name, email and date respectively. The patch's
@@ -612,8 +651,15 @@ static void do_commit(const struct am_state *state)
*/
static void am_run(struct am_state *state)
{
+ struct strbuf sb = STRBUF_INIT;
+
refresh_and_write_cache();
+ if (index_has_changes(&sb))
+ die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
+
+ strbuf_release(&sb);
+
while (state->cur <= state->last) {
const char *patch = am_path(state, msgnum(state));