builtin / reset.con commit Merge branch 'tf/gitweb-extra-breadcrumbs' (784bdd6)
   1/*
   2 * "git reset" builtin command
   3 *
   4 * Copyright (c) 2007 Carlos Rica
   5 *
   6 * Based on git-reset.sh, which is
   7 *
   8 * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
   9 */
  10#include "builtin.h"
  11#include "tag.h"
  12#include "object.h"
  13#include "commit.h"
  14#include "run-command.h"
  15#include "refs.h"
  16#include "diff.h"
  17#include "diffcore.h"
  18#include "tree.h"
  19#include "branch.h"
  20#include "parse-options.h"
  21#include "unpack-trees.h"
  22#include "cache-tree.h"
  23
  24static const char * const git_reset_usage[] = {
  25        N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
  26        N_("git reset [-q] <tree-ish> [--] <paths>..."),
  27        N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
  28        NULL
  29};
  30
  31enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
  32static const char *reset_type_names[] = {
  33        N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
  34};
  35
  36static inline int is_merge(void)
  37{
  38        return !access(git_path("MERGE_HEAD"), F_OK);
  39}
  40
  41static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
  42{
  43        int nr = 1;
  44        struct tree_desc desc[2];
  45        struct tree *tree;
  46        struct unpack_trees_options opts;
  47
  48        memset(&opts, 0, sizeof(opts));
  49        opts.head_idx = 1;
  50        opts.src_index = &the_index;
  51        opts.dst_index = &the_index;
  52        opts.fn = oneway_merge;
  53        opts.merge = 1;
  54        if (!quiet)
  55                opts.verbose_update = 1;
  56        switch (reset_type) {
  57        case KEEP:
  58        case MERGE:
  59                opts.update = 1;
  60                break;
  61        case HARD:
  62                opts.update = 1;
  63                /* fallthrough */
  64        default:
  65                opts.reset = 1;
  66        }
  67
  68        read_cache_unmerged();
  69
  70        if (reset_type == KEEP) {
  71                unsigned char head_sha1[20];
  72                if (get_sha1("HEAD", head_sha1))
  73                        return error(_("You do not have a valid HEAD."));
  74                if (!fill_tree_descriptor(desc, head_sha1))
  75                        return error(_("Failed to find tree of HEAD."));
  76                nr++;
  77                opts.fn = twoway_merge;
  78        }
  79
  80        if (!fill_tree_descriptor(desc + nr - 1, sha1))
  81                return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
  82        if (unpack_trees(nr, desc, &opts))
  83                return -1;
  84
  85        if (reset_type == MIXED || reset_type == HARD) {
  86                tree = parse_tree_indirect(sha1);
  87                prime_cache_tree(&active_cache_tree, tree);
  88        }
  89
  90        return 0;
  91}
  92
  93static void print_new_head_line(struct commit *commit)
  94{
  95        const char *hex, *body;
  96        char *msg;
  97
  98        hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
  99        printf(_("HEAD is now at %s"), hex);
 100        msg = logmsg_reencode(commit, NULL, get_log_output_encoding());
 101        body = strstr(msg, "\n\n");
 102        if (body) {
 103                const char *eol;
 104                size_t len;
 105                body += 2;
 106                eol = strchr(body, '\n');
 107                len = eol ? eol - body : strlen(body);
 108                printf(" %.*s\n", (int) len, body);
 109        }
 110        else
 111                printf("\n");
 112        logmsg_free(msg, commit);
 113}
 114
 115static void update_index_from_diff(struct diff_queue_struct *q,
 116                struct diff_options *opt, void *data)
 117{
 118        int i;
 119
 120        for (i = 0; i < q->nr; i++) {
 121                struct diff_filespec *one = q->queue[i]->one;
 122                if (one->mode && !is_null_sha1(one->sha1)) {
 123                        struct cache_entry *ce;
 124                        ce = make_cache_entry(one->mode, one->sha1, one->path,
 125                                0, 0);
 126                        if (!ce)
 127                                die(_("make_cache_entry failed for path '%s'"),
 128                                    one->path);
 129                        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
 130                                ADD_CACHE_OK_TO_REPLACE);
 131                } else
 132                        remove_file_from_cache(one->path);
 133        }
 134}
 135
 136static int read_from_tree(const char **pathspec, unsigned char *tree_sha1)
 137{
 138        struct diff_options opt;
 139
 140        memset(&opt, 0, sizeof(opt));
 141        diff_tree_setup_paths(pathspec, &opt);
 142        opt.output_format = DIFF_FORMAT_CALLBACK;
 143        opt.format_callback = update_index_from_diff;
 144
 145        read_cache();
 146        if (do_diff_cache(tree_sha1, &opt))
 147                return 1;
 148        diffcore_std(&opt);
 149        diff_flush(&opt);
 150        diff_tree_release_paths(&opt);
 151
 152        return 0;
 153}
 154
 155static void set_reflog_message(struct strbuf *sb, const char *action,
 156                               const char *rev)
 157{
 158        const char *rla = getenv("GIT_REFLOG_ACTION");
 159
 160        strbuf_reset(sb);
 161        if (rla)
 162                strbuf_addf(sb, "%s: %s", rla, action);
 163        else if (rev)
 164                strbuf_addf(sb, "reset: moving to %s", rev);
 165        else
 166                strbuf_addf(sb, "reset: %s", action);
 167}
 168
 169static void die_if_unmerged_cache(int reset_type)
 170{
 171        if (is_merge() || read_cache() < 0 || unmerged_cache())
 172                die(_("Cannot do a %s reset in the middle of a merge."),
 173                    _(reset_type_names[reset_type]));
 174
 175}
 176
 177static const char **parse_args(const char **argv, const char *prefix, const char **rev_ret)
 178{
 179        const char *rev = "HEAD";
 180        unsigned char unused[20];
 181        /*
 182         * Possible arguments are:
 183         *
 184         * git reset [-opts] [<rev>]
 185         * git reset [-opts] <tree> [<paths>...]
 186         * git reset [-opts] <tree> -- [<paths>...]
 187         * git reset [-opts] -- [<paths>...]
 188         * git reset [-opts] <paths>...
 189         *
 190         * At this point, argv points immediately after [-opts].
 191         */
 192
 193        if (argv[0]) {
 194                if (!strcmp(argv[0], "--")) {
 195                        argv++; /* reset to HEAD, possibly with paths */
 196                } else if (argv[1] && !strcmp(argv[1], "--")) {
 197                        rev = argv[0];
 198                        argv += 2;
 199                }
 200                /*
 201                 * Otherwise, argv[0] could be either <rev> or <paths> and
 202                 * has to be unambiguous. If there is a single argument, it
 203                 * can not be a tree
 204                 */
 205                else if ((!argv[1] && !get_sha1_committish(argv[0], unused)) ||
 206                         (argv[1] && !get_sha1_treeish(argv[0], unused))) {
 207                        /*
 208                         * Ok, argv[0] looks like a commit/tree; it should not
 209                         * be a filename.
 210                         */
 211                        verify_non_filename(prefix, argv[0]);
 212                        rev = *argv++;
 213                } else {
 214                        /* Otherwise we treat this as a filename */
 215                        verify_filename(prefix, argv[0], 1);
 216                }
 217        }
 218        *rev_ret = rev;
 219        return argv[0] ? get_pathspec(prefix, argv) : NULL;
 220}
 221
 222static int update_refs(const char *rev, const unsigned char *sha1)
 223{
 224        int update_ref_status;
 225        struct strbuf msg = STRBUF_INIT;
 226        unsigned char *orig = NULL, sha1_orig[20],
 227                *old_orig = NULL, sha1_old_orig[20];
 228
 229        if (!get_sha1("ORIG_HEAD", sha1_old_orig))
 230                old_orig = sha1_old_orig;
 231        if (!get_sha1("HEAD", sha1_orig)) {
 232                orig = sha1_orig;
 233                set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
 234                update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
 235        } else if (old_orig)
 236                delete_ref("ORIG_HEAD", old_orig, 0);
 237        set_reflog_message(&msg, "updating HEAD", rev);
 238        update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
 239        strbuf_release(&msg);
 240        return update_ref_status;
 241}
 242
 243int cmd_reset(int argc, const char **argv, const char *prefix)
 244{
 245        int reset_type = NONE, update_ref_status = 0, quiet = 0;
 246        int patch_mode = 0, unborn;
 247        const char *rev;
 248        unsigned char sha1[20];
 249        const char **pathspec = NULL;
 250        const struct option options[] = {
 251                OPT__QUIET(&quiet, N_("be quiet, only report errors")),
 252                OPT_SET_INT(0, "mixed", &reset_type,
 253                                                N_("reset HEAD and index"), MIXED),
 254                OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
 255                OPT_SET_INT(0, "hard", &reset_type,
 256                                N_("reset HEAD, index and working tree"), HARD),
 257                OPT_SET_INT(0, "merge", &reset_type,
 258                                N_("reset HEAD, index and working tree"), MERGE),
 259                OPT_SET_INT(0, "keep", &reset_type,
 260                                N_("reset HEAD but keep local changes"), KEEP),
 261                OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")),
 262                OPT_END()
 263        };
 264
 265        git_config(git_default_config, NULL);
 266
 267        argc = parse_options(argc, argv, prefix, options, git_reset_usage,
 268                                                PARSE_OPT_KEEP_DASHDASH);
 269        pathspec = parse_args(argv, prefix, &rev);
 270
 271        unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1);
 272        if (unborn) {
 273                /* reset on unborn branch: treat as reset to empty tree */
 274                hashcpy(sha1, EMPTY_TREE_SHA1_BIN);
 275        } else if (!pathspec) {
 276                struct commit *commit;
 277                if (get_sha1_committish(rev, sha1))
 278                        die(_("Failed to resolve '%s' as a valid revision."), rev);
 279                commit = lookup_commit_reference(sha1);
 280                if (!commit)
 281                        die(_("Could not parse object '%s'."), rev);
 282                hashcpy(sha1, commit->object.sha1);
 283        } else {
 284                struct tree *tree;
 285                if (get_sha1_treeish(rev, sha1))
 286                        die(_("Failed to resolve '%s' as a valid tree."), rev);
 287                tree = parse_tree_indirect(sha1);
 288                if (!tree)
 289                        die(_("Could not parse object '%s'."), rev);
 290                hashcpy(sha1, tree->object.sha1);
 291        }
 292
 293        if (patch_mode) {
 294                if (reset_type != NONE)
 295                        die(_("--patch is incompatible with --{hard,mixed,soft}"));
 296                return run_add_interactive(sha1_to_hex(sha1), "--patch=reset", pathspec);
 297        }
 298
 299        /* git reset tree [--] paths... can be used to
 300         * load chosen paths from the tree into the index without
 301         * affecting the working tree nor HEAD. */
 302        if (pathspec) {
 303                if (reset_type == MIXED)
 304                        warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
 305                else if (reset_type != NONE)
 306                        die(_("Cannot do %s reset with paths."),
 307                                        _(reset_type_names[reset_type]));
 308        }
 309        if (reset_type == NONE)
 310                reset_type = MIXED; /* by default */
 311
 312        if (reset_type != SOFT && reset_type != MIXED)
 313                setup_work_tree();
 314
 315        if (reset_type == MIXED && is_bare_repository())
 316                die(_("%s reset is not allowed in a bare repository"),
 317                    _(reset_type_names[reset_type]));
 318
 319        /* Soft reset does not touch the index file nor the working tree
 320         * at all, but requires them in a good order.  Other resets reset
 321         * the index file to the tree object we are switching to. */
 322        if (reset_type == SOFT || reset_type == KEEP)
 323                die_if_unmerged_cache(reset_type);
 324
 325        if (reset_type != SOFT) {
 326                struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 327                int newfd = hold_locked_index(lock, 1);
 328                if (reset_type == MIXED) {
 329                        if (read_from_tree(pathspec, sha1))
 330                                return 1;
 331                } else {
 332                        int err = reset_index(sha1, reset_type, quiet);
 333                        if (reset_type == KEEP && !err)
 334                                err = reset_index(sha1, MIXED, quiet);
 335                        if (err)
 336                                die(_("Could not reset index file to revision '%s'."), rev);
 337                }
 338
 339                if (reset_type == MIXED) { /* Report what has not been updated. */
 340                        int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
 341                        refresh_index(&the_index, flags, NULL, NULL,
 342                                      _("Unstaged changes after reset:"));
 343                }
 344
 345                if (write_cache(newfd, active_cache, active_nr) ||
 346                    commit_locked_index(lock))
 347                        die(_("Could not write new index file."));
 348        }
 349
 350        if (!pathspec && !unborn) {
 351                /* Any resets without paths update HEAD to the head being
 352                 * switched to, saving the previous head in ORIG_HEAD before. */
 353                update_ref_status = update_refs(rev, sha1);
 354
 355                if (reset_type == HARD && !update_ref_status && !quiet)
 356                        print_new_head_line(lookup_commit_reference(sha1));
 357        }
 358        if (!pathspec)
 359                remove_branch_state();
 360
 361        return update_ref_status;
 362}