Merge branch 'nd/ls-remote-does-not-have-u-option' into maint
[gitweb.git] / builtin / am.c
index 9c576779c36caa41edf20d7dd94a51893b00206c..3bd4fd701bdccab91f1343b371cf19e822c14db5 100644 (file)
@@ -10,6 +10,7 @@
 #include "dir.h"
 #include "run-command.h"
 #include "quote.h"
+#include "tempfile.h"
 #include "lockfile.h"
 #include "cache-tree.h"
 #include "refs.h"
@@ -199,19 +200,13 @@ static inline const char *am_path(const struct am_state *state, const char *path
 static int write_state_text(const struct am_state *state,
                            const char *name, const char *string)
 {
-       const char *fmt;
-
-       if (*string && string[strlen(string) - 1] != '\n')
-               fmt = "%s\n";
-       else
-               fmt = "%s";
-       return write_file(am_path(state, name), fmt, string);
+       return write_file(am_path(state, name), "%s", string);
 }
 
 static int write_state_count(const struct am_state *state,
                             const char *name, int value)
 {
-       return write_file(am_path(state, name), "%d\n", value);
+       return write_file(am_path(state, name), "%d", value);
 }
 
 static int write_state_bool(const struct am_state *state,
@@ -1212,6 +1207,33 @@ static void NORETURN die_user_resolve(const struct am_state *state)
        exit(128);
 }
 
+static void am_signoff(struct strbuf *sb)
+{
+       char *cp;
+       struct strbuf mine = STRBUF_INIT;
+
+       /* Does it end with our own sign-off? */
+       strbuf_addf(&mine, "\n%s%s\n",
+                   sign_off_header,
+                   fmt_name(getenv("GIT_COMMITTER_NAME"),
+                            getenv("GIT_COMMITTER_EMAIL")));
+       if (mine.len < sb->len &&
+           !strcmp(mine.buf, sb->buf + sb->len - mine.len))
+               goto exit; /* no need to duplicate */
+
+       /* Does it have any Signed-off-by: in the text */
+       for (cp = sb->buf;
+            cp && *cp && (cp = strstr(cp, sign_off_header)) != NULL;
+            cp = strchr(cp, '\n')) {
+               if (sb->buf == cp || cp[-1] == '\n')
+                       break;
+       }
+
+       strbuf_addstr(sb, mine.buf + !!cp);
+exit:
+       strbuf_release(&mine);
+}
+
 /**
  * Appends signoff to the "msg" field of the am_state.
  */
@@ -1220,7 +1242,7 @@ static void am_append_signoff(struct am_state *state)
        struct strbuf sb = STRBUF_INIT;
 
        strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len);
-       append_signoff(&sb, 0, 0);
+       am_signoff(&sb);
        state->msg = strbuf_detach(&sb, &state->msg_len);
 }
 
@@ -1324,7 +1346,7 @@ static int parse_mail(struct am_state *state, const char *mail)
        stripspace(&msg, 0);
 
        if (state->signoff)
-               append_signoff(&msg, 0, 0);
+               am_signoff(&msg);
 
        assert(!state->author_name);
        state->author_name = strbuf_detach(&author_name, NULL);
@@ -1981,16 +2003,49 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
        return 0;
 }
 
+/**
+ * Merges a tree into the index. The index's stat info will take precedence
+ * over the merged tree's. Returns 0 on success, -1 on failure.
+ */
+static int merge_tree(struct tree *tree)
+{
+       struct lock_file *lock_file;
+       struct unpack_trees_options opts;
+       struct tree_desc t[1];
+
+       if (parse_tree(tree))
+               return -1;
+
+       lock_file = xcalloc(1, sizeof(struct lock_file));
+       hold_locked_index(lock_file, 1);
+
+       memset(&opts, 0, sizeof(opts));
+       opts.head_idx = 1;
+       opts.src_index = &the_index;
+       opts.dst_index = &the_index;
+       opts.merge = 1;
+       opts.fn = oneway_merge;
+       init_tree_desc(&t[0], tree->buffer, tree->size);
+
+       if (unpack_trees(1, t, &opts)) {
+               rollback_lock_file(lock_file);
+               return -1;
+       }
+
+       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
+               die(_("unable to write new index file"));
+
+       return 0;
+}
+
 /**
  * Clean the index without touching entries that are not modified between
  * `head` and `remote`.
  */
 static int clean_index(const unsigned char *head, const unsigned char *remote)
 {
-       struct lock_file *lock_file;
        struct tree *head_tree, *remote_tree, *index_tree;
        unsigned char index[GIT_SHA1_RAWSZ];
-       struct pathspec pathspec;
 
        head_tree = parse_tree_indirect(head);
        if (!head_tree)
@@ -2015,18 +2070,8 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
        if (fast_forward_to(index_tree, remote_tree, 0))
                return -1;
 
-       memset(&pathspec, 0, sizeof(pathspec));
-
-       lock_file = xcalloc(1, sizeof(struct lock_file));
-       hold_locked_index(lock_file, 1);
-
-       if (read_tree(remote_tree, 0, &pathspec)) {
-               rollback_lock_file(lock_file);
+       if (merge_tree(remote_tree))
                return -1;
-       }
-
-       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
-               die(_("unable to write new index file"));
 
        remove_branch_state();
 
@@ -2039,11 +2084,6 @@ static int clean_index(const unsigned char *head, const unsigned char *remote)
 static void am_rerere_clear(void)
 {
        struct string_list merge_rr = STRING_LIST_INIT_DUP;
-       int fd = setup_rerere(&merge_rr, 0);
-
-       if (fd < 0)
-               return;
-
        rerere_clear(&merge_rr);
        string_list_clear(&merge_rr, 1);
 }
@@ -2168,6 +2208,17 @@ enum resume_mode {
        RESUME_ABORT
 };
 
+static int git_am_config(const char *k, const char *v, void *cb)
+{
+       int status;
+
+       status = git_gpg_config(k, v, NULL);
+       if (status)
+               return status;
+
+       return git_default_config(k, v, NULL);
+}
+
 int cmd_am(int argc, const char **argv, const char *prefix)
 {
        struct am_state state;
@@ -2187,7 +2238,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_BOOL('i', "interactive", &state.interactive,
                        N_("run interactively")),
                OPT_HIDDEN_BOOL('b', "binary", &binary,
-                       N_("(historical option -- no-op")),
+                       N_("historical option -- no-op")),
                OPT_BOOL('3', "3way", &state.threeway,
                        N_("allow fall back on 3way merging if needed")),
                OPT__QUIET(&state.quiet, N_("be quiet")),
@@ -2268,7 +2319,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
-       git_config(git_default_config, NULL);
+       git_config(git_am_config, NULL);
 
        am_state_init(&state, git_path("rebase-apply"));