]> rtime.felk.cvut.cz Git - git.git/blobdiff - builtin-merge.c
Merge branch 'ap/merge-backend-opts'
[git.git] / builtin-merge.c
index 3aa7ea4052c2eb3c41d955b24fdbd3cfd1ec1124..3aaec7bed76af9efdfe5647be0da64373db2011e 100644 (file)
@@ -24,6 +24,7 @@
 #include "rerere.h"
 #include "help.h"
 #include "merge-recursive.h"
+#include "resolve-undo.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -54,6 +55,7 @@ static const char **xopts;
 static size_t xopts_nr, xopts_alloc;
 static const char *branch;
 static int verbosity;
+static int allow_rerere_auto;
 
 static struct strategy all_strategy[] = {
        { "recursive",  DEFAULT_TWOHEAD | NO_TRIVIAL },
@@ -73,7 +75,7 @@ static int option_parse_message(const struct option *opt,
        if (unset)
                strbuf_setlen(buf, 0);
        else if (arg) {
-               strbuf_addf(buf, "%s\n\n", arg);
+               strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
                have_message = 1;
        } else
                return error("switch `m' requires a value");
@@ -183,6 +185,7 @@ static struct option builtin_merge_options[] = {
                "allow fast-forward (default)"),
        OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
                "abort if fast-forward is not possible"),
+       OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
        OPT_CALLBACK('s', "strategy", &use_strategies, "strategy",
                "merge strategy to use", option_parse_strategy),
        OPT_CALLBACK('X', "strategy-option", &xopts, "option=value",
@@ -640,6 +643,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
                discard_cache();
                if (read_cache() < 0)
                        die("failed to read the cache");
+               resolve_undo_clear();
                return ret;
        }
 }
@@ -654,11 +658,10 @@ static void count_diff_files(struct diff_queue_struct *q,
 
 static int count_unmerged_entries(void)
 {
-       const struct index_state *state = &the_index;
        int i, ret = 0;
 
-       for (i = 0; i < state->cache_nr; i++)
-               if (ce_stage(state->cache[i]))
+       for (i = 0; i < active_nr; i++)
+               if (ce_stage(active_cache[i]))
                        ret++;
 
        return ret;
@@ -692,6 +695,7 @@ static int checkout_fast_forward(unsigned char *head, unsigned char *remote)
        opts.verbose_update = 1;
        opts.merge = 1;
        opts.fn = twoway_merge;
+       opts.msgs = get_porcelain_error_msgs();
 
        trees[nr_trees] = parse_tree_indirect(head);
        if (!trees[nr_trees++])
@@ -825,7 +829,7 @@ static int suggest_conflicts(void)
                }
        }
        fclose(fp);
-       rerere();
+       rerere(allow_rerere_auto);
        printf("Automatic merge failed; "
                        "fix conflicts and then commit the result.\n");
        return 1;
@@ -834,7 +838,7 @@ static int suggest_conflicts(void)
 static struct commit *is_old_style_invocation(int argc, const char **argv)
 {
        struct commit *second_token = NULL;
-       if (argc > 1) {
+       if (argc > 2) {
                unsigned char second_sha1[20];
 
                if (get_sha1(argv[1], second_sha1))
@@ -882,12 +886,22 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
        const char *best_strategy = NULL, *wt_strategy = NULL;
        struct commit_list **remotes = &remoteheads;
 
-       if (file_exists(git_path("MERGE_HEAD")))
-               die("You have not concluded your merge. (MERGE_HEAD exists)");
-       if (read_cache_unmerged())
-               die("You are in the middle of a conflicted merge."
-                               " (index unmerged)");
+       if (read_cache_unmerged()) {
+               die_resolve_conflict("merge");
+       }
+       if (file_exists(git_path("MERGE_HEAD"))) {
+               /*
+                * There is no unmerged entry, don't advise 'git
+                * add/rm <file>', just 'git commit'.
+                */
+               if (advice_resolve_conflict)
+                       die("You have not concluded your merge (MERGE_HEAD exists).\n"
+                           "Please, commit your changes before you can merge.");
+               else
+                       die("You have not concluded your merge (MERGE_HEAD exists).");
+       }
 
+       resolve_undo_clear();
        /*
         * Check if we are _not_ on a detached HEAD, i.e. if there is a
         * current branch.
@@ -972,11 +986,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                 * codepath so we discard the error in this
                 * loop.
                 */
-               for (i = 0; i < argc; i++)
-                       merge_name(argv[i], &msg);
-               fmt_merge_msg(option_log, &msg, &merge_msg);
-               if (merge_msg.len)
-                       strbuf_setlen(&merge_msg, merge_msg.len-1);
+               if (!have_message) {
+                       for (i = 0; i < argc; i++)
+                               merge_name(argv[i], &msg);
+                       fmt_merge_msg(option_log, &msg, &merge_msg);
+                       if (merge_msg.len)
+                               strbuf_setlen(&merge_msg, merge_msg.len-1);
+               }
        }
 
        if (head_invalid || !argc)