revert: Eliminate global "commit" variable
[gitweb.git] / builtin / revert.c
index 1f27c63343904a969e60ab19408495007f59d133..d6c9f1dadb8f002d0c859ce6698a6fba8874904a 100644 (file)
@@ -35,9 +35,8 @@ static const char * const cherry_pick_usage[] = {
        NULL
 };
 
-static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
+static int edit, record_origin, no_commit, mainline, signoff, allow_ff;
 static enum { REVERT, CHERRY_PICK } action;
-static struct commit *commit;
 static int commit_argc;
 static const char **commit_argv;
 static int allow_rerere_auto;
@@ -91,7 +90,7 @@ static void parse_args(int argc, const char **argv)
 
        if (action == CHERRY_PICK) {
                struct option cp_extra[] = {
-                       OPT_BOOLEAN('x', NULL, &no_replay, "append commit name"),
+                       OPT_BOOLEAN('x', NULL, &record_origin, "append commit name"),
                        OPT_BOOLEAN(0, "ff", &allow_ff, "allow fast-forward"),
                        OPT_END(),
                };
@@ -116,25 +115,25 @@ struct commit_message {
        const char *message;
 };
 
-static int get_message(const char *raw_message, struct commit_message *out)
+static int get_message(struct commit *commit, struct commit_message *out)
 {
        const char *encoding;
        const char *abbrev, *subject;
        int abbrev_len, subject_len;
        char *q;
 
-       if (!raw_message)
+       if (!commit->buffer)
                return -1;
-       encoding = get_encoding(raw_message);
+       encoding = get_encoding(commit->buffer);
        if (!encoding)
                encoding = "UTF-8";
        if (!git_commit_encoding)
                git_commit_encoding = "UTF-8";
 
        out->reencoded_message = NULL;
-       out->message = raw_message;
+       out->message = commit->buffer;
        if (strcmp(encoding, git_commit_encoding))
-               out->reencoded_message = reencode_string(raw_message,
+               out->reencoded_message = reencode_string(commit->buffer,
                                        git_commit_encoding, encoding);
        if (out->reencoded_message)
                out->message = out->reencoded_message;
@@ -167,9 +166,6 @@ static char *get_encoding(const char *message)
 {
        const char *p = message, *eol;
 
-       if (!p)
-               die (_("Could not read commit message of %s"),
-                               sha1_to_hex(commit->object.sha1));
        while (*p && *p != '\n') {
                for (eol = p + 1; *eol && *eol != '\n'; eol++)
                        ; /* do nothing */
@@ -185,20 +181,7 @@ static char *get_encoding(const char *message)
        return NULL;
 }
 
-static void add_message_to_msg(struct strbuf *msgbuf, const char *message)
-{
-       const char *p = message;
-       while (*p && (*p != '\n' || p[1] != '\n'))
-               p++;
-
-       if (!*p)
-               strbuf_addstr(msgbuf, sha1_to_hex(commit->object.sha1));
-
-       p += 2;
-       strbuf_addstr(msgbuf, p);
-}
-
-static void write_cherry_pick_head(void)
+static void write_cherry_pick_head(struct commit *commit)
 {
        int fd;
        struct strbuf buf = STRBUF_INIT;
@@ -214,15 +197,6 @@ static void write_cherry_pick_head(void)
        strbuf_release(&buf);
 }
 
-static void advise(const char *advice, ...)
-{
-       va_list params;
-
-       va_start(params, advice);
-       vreportf("hint: ", advice, params);
-       va_end(params);
-}
-
 static void print_advice(void)
 {
        char *msg = getenv("GIT_CHERRY_PICK_HELP");
@@ -380,7 +354,7 @@ static int run_git_commit(const char *defmsg)
        return run_command_v_opt(args, RUN_GIT_CMD);
 }
 
-static int do_pick_commit(void)
+static int do_pick_commit(struct commit *commit)
 {
        unsigned char head[20];
        struct commit *base, *next, *parent;
@@ -442,7 +416,7 @@ static int do_pick_commit(void)
                die(_("%s: cannot parse parent commit %s"),
                    me, sha1_to_hex(parent->object.sha1));
 
-       if (get_message(commit->buffer, &msg) != 0)
+       if (get_message(commit, &msg) != 0)
                die(_("Cannot get commit message for %s"),
                                sha1_to_hex(commit->object.sha1));
 
@@ -471,18 +445,31 @@ static int do_pick_commit(void)
                }
                strbuf_addstr(&msgbuf, ".\n");
        } else {
+               const char *p;
+
                base = parent;
                base_label = msg.parent_label;
                next = commit;
                next_label = msg.label;
-               add_message_to_msg(&msgbuf, msg.message);
-               if (no_replay) {
+
+               /*
+                * Append the commit log message to msgbuf; it starts
+                * after the tree, parent, author, committer
+                * information followed by "\n\n".
+                */
+               p = strstr(msg.message, "\n\n");
+               if (p) {
+                       p += 2;
+                       strbuf_addstr(&msgbuf, p);
+               }
+
+               if (record_origin) {
                        strbuf_addstr(&msgbuf, "(cherry picked from commit ");
                        strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
                        strbuf_addstr(&msgbuf, ")\n");
                }
                if (!no_commit)
-                       write_cherry_pick_head();
+                       write_cherry_pick_head(commit);
        }
 
        if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
@@ -560,6 +547,7 @@ static void read_and_refresh_cache(const char *me)
 static int revert_or_cherry_pick(int argc, const char **argv)
 {
        struct rev_info revs;
+       struct commit *commit;
 
        git_config(git_default_config, NULL);
        me = action == REVERT ? "revert" : "cherry-pick";
@@ -571,7 +559,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
                        die(_("cherry-pick --ff cannot be used with --signoff"));
                if (no_commit)
                        die(_("cherry-pick --ff cannot be used with --no-commit"));
-               if (no_replay)
+               if (record_origin)
                        die(_("cherry-pick --ff cannot be used with -x"));
                if (edit)
                        die(_("cherry-pick --ff cannot be used with --edit"));
@@ -582,7 +570,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
        prepare_revs(&revs);
 
        while ((commit = get_revision(&revs))) {
-               int res = do_pick_commit();
+               int res = do_pick_commit(commit);
                if (res)
                        return res;
        }