builtin-revert.con commit git-revert: Revert revert message to old behaviour (e43b010)
   1#include "cache.h"
   2#include "builtin.h"
   3#include "object.h"
   4#include "commit.h"
   5#include "tag.h"
   6#include "wt-status.h"
   7#include "run-command.h"
   8#include "exec_cmd.h"
   9#include "utf8.h"
  10
  11/*
  12 * This implements the builtins revert and cherry-pick.
  13 *
  14 * Copyright (c) 2007 Johannes E. Schindelin
  15 *
  16 * Based on git-revert.sh, which is
  17 *
  18 * Copyright (c) 2005 Linus Torvalds
  19 * Copyright (c) 2005 Junio C Hamano
  20 */
  21
  22static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] <commit-ish>";
  23
  24static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-r] [-x] <commit-ish>";
  25
  26static int edit;
  27static int replay;
  28enum { REVERT, CHERRY_PICK } action;
  29static int no_commit;
  30static struct commit *commit;
  31static int needed_deref;
  32
  33static const char *me;
  34
  35#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
  36
  37static void parse_options(int argc, const char **argv)
  38{
  39        const char *usage_str = action == REVERT ?
  40                revert_usage : cherry_pick_usage;
  41        unsigned char sha1[20];
  42        const char *arg;
  43        int i;
  44
  45        if (argc < 2)
  46                usage(usage_str);
  47
  48        for (i = 1; i < argc - 1; i++) {
  49                arg = argv[i];
  50                if (!strcmp(arg, "-n") || !strcmp(arg, "--no-commit"))
  51                        no_commit = 1;
  52                else if (!strcmp(arg, "-e") || !strcmp(arg, "--edit"))
  53                        edit = 1;
  54                else if (!strcmp(arg, "--no-edit"))
  55                        edit = 0;
  56                else if (!strcmp(arg, "-x") || !strcmp(arg, "--i-really-want-"
  57                                "to-expose-my-private-commit-object-name"))
  58                        replay = 0;
  59                else if (strcmp(arg, "-r"))
  60                        usage(usage_str);
  61        }
  62
  63        arg = argv[argc - 1];
  64        if (get_sha1(arg, sha1))
  65                die ("Cannot find '%s'", arg);
  66        commit = (struct commit *)parse_object(sha1);
  67        if (!commit)
  68                die ("Could not find %s", sha1_to_hex(sha1));
  69        if (commit->object.type == OBJ_TAG) {
  70                commit = (struct commit *)
  71                        deref_tag((struct object *)commit, arg, strlen(arg));
  72                needed_deref = 1;
  73        }
  74        if (commit->object.type != OBJ_COMMIT)
  75                die ("'%s' does not point to a commit", arg);
  76}
  77
  78static char *get_oneline(const char *message)
  79{
  80        char *result;
  81        const char *p = message, *abbrev, *eol;
  82        int abbrev_len, oneline_len;
  83
  84        if (!p)
  85                die ("Could not read commit message of %s",
  86                                sha1_to_hex(commit->object.sha1));
  87        while (*p && (*p != '\n' || p[1] != '\n'))
  88                p++;
  89
  90        if (*p) {
  91                p += 2;
  92                for (eol = p + 1; *eol && *eol != '\n'; eol++)
  93                        ; /* do nothing */
  94        } else
  95                eol = p;
  96        abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
  97        abbrev_len = strlen(abbrev);
  98        oneline_len = eol - p;
  99        result = xmalloc(abbrev_len + 5 + oneline_len);
 100        memcpy(result, abbrev, abbrev_len);
 101        memcpy(result + abbrev_len, "... ", 4);
 102        memcpy(result + abbrev_len + 4, p, oneline_len);
 103        result[abbrev_len + 4 + oneline_len] = '\0';
 104        return result;
 105}
 106
 107char *get_encoding(const char *message)
 108{
 109        const char *p = message, *eol;
 110
 111        if (!p)
 112                die ("Could not read commit message of %s",
 113                                sha1_to_hex(commit->object.sha1));
 114        while (*p && *p != '\n') {
 115                for (eol = p + 1; *eol && *eol != '\n'; eol++)
 116                        ; /* do nothing */
 117                if (!prefixcmp(p, "encoding ")) {
 118                        char *result = xmalloc(eol - 8 - p);
 119                        strlcpy(result, p + 9, eol - 8 - p);
 120                        return result;
 121                }
 122                p = eol;
 123                if (*p == '\n')
 124                        p++;
 125        }
 126        return NULL;
 127}
 128
 129struct lock_file msg_file;
 130static int msg_fd;
 131
 132static void add_to_msg(const char *string)
 133{
 134        int len = strlen(string);
 135        if (write_in_full(msg_fd, string, len) < 0)
 136                die ("Could not write to .msg");
 137}
 138
 139static void add_message_to_msg(const char *message)
 140{
 141        const char *p = message;
 142        while (*p && (*p != '\n' || p[1] != '\n'))
 143                p++;
 144
 145        if (!*p)
 146                add_to_msg(sha1_to_hex(commit->object.sha1));
 147
 148        p += 2;
 149        add_to_msg(p);
 150        return;
 151}
 152
 153static void set_author_ident_env(const char *message)
 154{
 155        const char *p = message;
 156        if (!p)
 157                die ("Could not read commit message of %s",
 158                                sha1_to_hex(commit->object.sha1));
 159        while (*p && *p != '\n') {
 160                const char *eol;
 161
 162                for (eol = p; *eol && *eol != '\n'; eol++)
 163                        ; /* do nothing */
 164                if (!prefixcmp(p, "author ")) {
 165                        char *line, *pend, *email, *timestamp;
 166
 167                        p += 7;
 168                        line = xmalloc(eol + 1 - p);
 169                        memcpy(line, p, eol - p);
 170                        line[eol - p] = '\0';
 171                        email = strchr(line, '<');
 172                        if (!email)
 173                                die ("Could not extract author email from %s",
 174                                        sha1_to_hex(commit->object.sha1));
 175                        if (email == line)
 176                                pend = line;
 177                        else
 178                                for (pend = email; pend != line + 1 &&
 179                                                isspace(pend[-1]); pend--);
 180                                        ; /* do nothing */
 181                        *pend = '\0';
 182                        email++;
 183                        timestamp = strchr(email, '>');
 184                        if (!timestamp)
 185                                die ("Could not extract author email from %s",
 186                                        sha1_to_hex(commit->object.sha1));
 187                        *timestamp = '\0';
 188                        for (timestamp++; *timestamp && isspace(*timestamp);
 189                                        timestamp++)
 190                                ; /* do nothing */
 191                        setenv("GIT_AUTHOR_NAME", line, 1);
 192                        setenv("GIT_AUTHOR_EMAIL", email, 1);
 193                        setenv("GIT_AUTHOR_DATE", timestamp, 1);
 194                        free(line);
 195                        return;
 196                }
 197                p = eol;
 198                if (*p == '\n')
 199                        p++;
 200        }
 201        die ("No author information found in %s",
 202                        sha1_to_hex(commit->object.sha1));
 203}
 204
 205static int merge_recursive(const char *base_sha1,
 206                const char *head_sha1, const char *head_name,
 207                const char *next_sha1, const char *next_name)
 208{
 209        char buffer[256];
 210        const char *argv[6];
 211
 212        sprintf(buffer, "GITHEAD_%s", head_sha1);
 213        setenv(buffer, head_name, 1);
 214        sprintf(buffer, "GITHEAD_%s", next_sha1);
 215        setenv(buffer, next_name, 1);
 216
 217        /*
 218         * This three way merge is an interesting one.  We are at
 219         * $head, and would want to apply the change between $commit
 220         * and $prev on top of us (when reverting), or the change between
 221         * $prev and $commit on top of us (when cherry-picking or replaying).
 222         */
 223        argv[0] = "merge-recursive";
 224        argv[1] = base_sha1;
 225        argv[2] = "--";
 226        argv[3] = head_sha1;
 227        argv[4] = next_sha1;
 228        argv[5] = NULL;
 229
 230        return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
 231}
 232
 233static int revert_or_cherry_pick(int argc, const char **argv)
 234{
 235        unsigned char head[20];
 236        struct commit *base, *next;
 237        int i;
 238        char *oneline, *reencoded_message = NULL;
 239        const char *message, *encoding;
 240
 241        git_config(git_default_config);
 242        me = action == REVERT ? "revert" : "cherry-pick";
 243        setenv(GIT_REFLOG_ACTION, me, 0);
 244        parse_options(argc, argv);
 245
 246        /* this is copied from the shell script, but it's never triggered... */
 247        if (action == REVERT && replay)
 248                die("revert is incompatible with replay");
 249
 250        if (no_commit) {
 251                /*
 252                 * We do not intend to commit immediately.  We just want to
 253                 * merge the differences in.
 254                 */
 255                if (write_tree(head, 0, NULL))
 256                        die ("Your index file is unmerged.");
 257        } else {
 258                struct wt_status s;
 259
 260                if (get_sha1("HEAD", head))
 261                        die ("You do not have a valid HEAD");
 262                wt_status_prepare(&s);
 263                if (s.commitable || s.workdir_dirty)
 264                        die ("Dirty index: cannot %s", me);
 265                discard_cache();
 266        }
 267
 268        if (!commit->parents)
 269                die ("Cannot %s a root commit", me);
 270        if (commit->parents->next)
 271                die ("Cannot %s a multi-parent commit.", me);
 272        if (!(message = commit->buffer))
 273                die ("Cannot get commit message for %s",
 274                                sha1_to_hex(commit->object.sha1));
 275
 276        /*
 277         * "commit" is an existing commit.  We would want to apply
 278         * the difference it introduces since its first parent "prev"
 279         * on top of the current HEAD if we are cherry-pick.  Or the
 280         * reverse of it if we are revert.
 281         */
 282
 283        msg_fd = hold_lock_file_for_update(&msg_file, ".msg", 1);
 284
 285        encoding = get_encoding(message);
 286        if (!encoding)
 287                encoding = "utf-8";
 288        if (!git_commit_encoding)
 289                git_commit_encoding = "utf-8";
 290        if ((reencoded_message = reencode_string(message,
 291                                        git_commit_encoding, encoding)))
 292                message = reencoded_message;
 293
 294        oneline = get_oneline(message);
 295
 296        if (action == REVERT) {
 297                char *oneline_body = strchr(oneline, ' ');
 298
 299                base = commit;
 300                next = commit->parents->item;
 301                add_to_msg("Revert \"");
 302                add_to_msg(oneline_body + 1);
 303                add_to_msg("\"\n\nThis reverts commit ");
 304                add_to_msg(sha1_to_hex(commit->object.sha1));
 305                add_to_msg(".\n");
 306        } else {
 307                base = commit->parents->item;
 308                next = commit;
 309                set_author_ident_env(message);
 310                add_message_to_msg(message);
 311                if (!replay) {
 312                        add_to_msg("(cherry picked from commit ");
 313                        add_to_msg(sha1_to_hex(commit->object.sha1));
 314                        add_to_msg(")\n");
 315                }
 316        }
 317        if (needed_deref) {
 318                add_to_msg("(original 'git ");
 319                add_to_msg(me);
 320                add_to_msg("' arguments: ");
 321                for (i = 0; i < argc; i++) {
 322                        if (i)
 323                                add_to_msg(" ");
 324                        add_to_msg(argv[i]);
 325                }
 326                add_to_msg(")\n");
 327        }
 328
 329        if (merge_recursive(sha1_to_hex(base->object.sha1),
 330                                sha1_to_hex(head), "HEAD",
 331                                sha1_to_hex(next->object.sha1), oneline) ||
 332                        write_tree(head, 0, NULL)) {
 333                const char *target = git_path("MERGE_MSG");
 334                add_to_msg("\nConflicts:\n\n");
 335                read_cache();
 336                for (i = 0; i < active_nr;) {
 337                        struct cache_entry *ce = active_cache[i++];
 338                        if (ce_stage(ce)) {
 339                                add_to_msg("\t");
 340                                add_to_msg(ce->name);
 341                                add_to_msg("\n");
 342                                while (i < active_nr && !strcmp(ce->name,
 343                                                active_cache[i]->name))
 344                                        i++;
 345                        }
 346                }
 347                if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
 348                        die ("Error wrapping up .msg");
 349                unlink(target);
 350                if (rename(".msg", target))
 351                        die ("Could not move .msg to %s", target);
 352                fprintf(stderr, "Automatic %s failed.  "
 353                        "After resolving the conflicts,\n"
 354                        "mark the corrected paths with 'git-add <paths>'\n"
 355                        "and commit the result.\n", me);
 356                if (action == CHERRY_PICK) {
 357                        fprintf(stderr, "When commiting, use the option "
 358                                "'-c %s' to retain authorship and message.\n",
 359                                find_unique_abbrev(commit->object.sha1,
 360                                        DEFAULT_ABBREV));
 361                }
 362                exit(1);
 363        }
 364        if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
 365                die ("Error wrapping up .msg");
 366        fprintf(stderr, "Finished one %s.\n", me);
 367
 368        /*
 369         *
 370         * If we are cherry-pick, and if the merge did not result in
 371         * hand-editing, we will hit this commit and inherit the original
 372         * author date and name.
 373         * If we are revert, or if our cherry-pick results in a hand merge,
 374         * we had better say that the current user is responsible for that.
 375         */
 376
 377        if (!no_commit) {
 378                if (edit)
 379                        return execl_git_cmd("commit", "-n", "-F", ".msg",
 380                                "-e", NULL);
 381                else
 382                        return execl_git_cmd("commit", "-n", "-F", ".msg",
 383                                NULL);
 384        }
 385        if (reencoded_message)
 386                free(reencoded_message);
 387
 388        return 0;
 389}
 390
 391int cmd_revert(int argc, const char **argv, const char *prefix)
 392{
 393        if (isatty(0))
 394                edit = 1;
 395        action = REVERT;
 396        return revert_or_cherry_pick(argc, argv);
 397}
 398
 399int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 400{
 401        replay = 1;
 402        action = CHERRY_PICK;
 403        return revert_or_cherry_pick(argc, argv);
 404}