builtin-reset.con commit Merge branch 'jc/revert-merge' (02273fd)
   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 "cache.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
  20static const char builtin_reset_usage[] =
  21"git-reset [--mixed | --soft | --hard]  [<commit-ish>] [ [--] <paths>...]";
  22
  23static char *args_to_str(const char **argv)
  24{
  25        char *buf = NULL;
  26        unsigned long len, space = 0, nr = 0;
  27
  28        for (; *argv; argv++) {
  29                len = strlen(*argv);
  30                ALLOC_GROW(buf, nr + 1 + len, space);
  31                if (nr)
  32                        buf[nr++] = ' ';
  33                memcpy(buf + nr, *argv, len);
  34                nr += len;
  35        }
  36        ALLOC_GROW(buf, nr + 1, space);
  37        buf[nr] = '\0';
  38
  39        return buf;
  40}
  41
  42static inline int is_merge(void)
  43{
  44        return !access(git_path("MERGE_HEAD"), F_OK);
  45}
  46
  47static int unmerged_files(void)
  48{
  49        char b;
  50        ssize_t len;
  51        struct child_process cmd;
  52        const char *argv_ls_files[] = {"ls-files", "--unmerged", NULL};
  53
  54        memset(&cmd, 0, sizeof(cmd));
  55        cmd.argv = argv_ls_files;
  56        cmd.git_cmd = 1;
  57        cmd.out = -1;
  58
  59        if (start_command(&cmd))
  60                die("Could not run sub-command: git ls-files");
  61
  62        len = xread(cmd.out, &b, 1);
  63        if (len < 0)
  64                die("Could not read output from git ls-files: %s",
  65                                                strerror(errno));
  66        finish_command(&cmd);
  67
  68        return len;
  69}
  70
  71static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
  72{
  73        int i = 0;
  74        const char *args[6];
  75
  76        args[i++] = "read-tree";
  77        args[i++] = "-v";
  78        args[i++] = "--reset";
  79        if (is_hard_reset)
  80                args[i++] = "-u";
  81        args[i++] = sha1_to_hex(sha1);
  82        args[i] = NULL;
  83
  84        return run_command_v_opt(args, RUN_GIT_CMD);
  85}
  86
  87static void print_new_head_line(struct commit *commit)
  88{
  89        const char *hex, *dots = "...", *body;
  90
  91        hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
  92        if (!hex) {
  93                hex = sha1_to_hex(commit->object.sha1);
  94                dots = "";
  95        }
  96        printf("HEAD is now at %s%s", hex, dots);
  97        body = strstr(commit->buffer, "\n\n");
  98        if (body) {
  99                const char *eol;
 100                size_t len;
 101                body += 2;
 102                eol = strchr(body, '\n');
 103                len = eol ? eol - body : strlen(body);
 104                printf(" %.*s\n", (int) len, body);
 105        }
 106        else
 107                printf("\n");
 108}
 109
 110static int update_index_refresh(void)
 111{
 112        const char *argv_update_index[] = {"update-index", "--refresh", NULL};
 113        return run_command_v_opt(argv_update_index, RUN_GIT_CMD);
 114}
 115
 116struct update_cb_data {
 117        int index_fd;
 118        struct lock_file *lock;
 119        int exit_code;
 120};
 121
 122static void update_index_from_diff(struct diff_queue_struct *q,
 123                struct diff_options *opt, void *data)
 124{
 125        int i;
 126        struct update_cb_data *cb = data;
 127
 128        /* do_diff_cache() mangled the index */
 129        discard_cache();
 130        read_cache();
 131
 132        for (i = 0; i < q->nr; i++) {
 133                struct diff_filespec *one = q->queue[i]->one;
 134                if (one->mode) {
 135                        struct cache_entry *ce;
 136                        ce = make_cache_entry(one->mode, one->sha1, one->path,
 137                                0, 0);
 138                        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
 139                                ADD_CACHE_OK_TO_REPLACE);
 140                } else
 141                        remove_file_from_cache(one->path);
 142        }
 143
 144        cb->exit_code = write_cache(cb->index_fd, active_cache, active_nr) ||
 145                close(cb->index_fd) ||
 146                commit_locked_index(cb->lock);
 147}
 148
 149static int read_from_tree(const char *prefix, const char **argv,
 150                unsigned char *tree_sha1)
 151{
 152        struct diff_options opt;
 153        struct update_cb_data cb;
 154
 155        memset(&opt, 0, sizeof(opt));
 156        diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
 157        opt.output_format = DIFF_FORMAT_CALLBACK;
 158        opt.format_callback = update_index_from_diff;
 159        opt.format_callback_data = &cb;
 160
 161        cb.lock = xcalloc(1, sizeof(struct lock_file));
 162        cb.index_fd = hold_locked_index(cb.lock, 1);
 163        cb.exit_code = 0;
 164        read_cache();
 165        if (do_diff_cache(tree_sha1, &opt))
 166                return 1;
 167        diffcore_std(&opt);
 168        diff_flush(&opt);
 169
 170        return cb.exit_code;
 171}
 172
 173static void prepend_reflog_action(const char *action, char *buf, size_t size)
 174{
 175        const char *sep = ": ";
 176        const char *rla = getenv("GIT_REFLOG_ACTION");
 177        if (!rla)
 178                rla = sep = "";
 179        if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
 180                warning("Reflog action message too long: %.*s...", 50, buf);
 181}
 182
 183enum reset_type { MIXED, SOFT, HARD, NONE };
 184static const char *reset_type_names[] = { "mixed", "soft", "hard", NULL };
 185
 186int cmd_reset(int argc, const char **argv, const char *prefix)
 187{
 188        int i = 1, reset_type = NONE, update_ref_status = 0;
 189        const char *rev = "HEAD";
 190        unsigned char sha1[20], *orig = NULL, sha1_orig[20],
 191                                *old_orig = NULL, sha1_old_orig[20];
 192        struct commit *commit;
 193        char *reflog_action, msg[1024];
 194
 195        git_config(git_default_config);
 196
 197        reflog_action = args_to_str(argv);
 198        setenv("GIT_REFLOG_ACTION", reflog_action, 0);
 199
 200        if (i < argc) {
 201                if (!strcmp(argv[i], "--mixed")) {
 202                        reset_type = MIXED;
 203                        i++;
 204                }
 205                else if (!strcmp(argv[i], "--soft")) {
 206                        reset_type = SOFT;
 207                        i++;
 208                }
 209                else if (!strcmp(argv[i], "--hard")) {
 210                        reset_type = HARD;
 211                        i++;
 212                }
 213        }
 214
 215        if (i < argc && argv[i][0] != '-')
 216                rev = argv[i++];
 217
 218        if (get_sha1(rev, sha1))
 219                die("Failed to resolve '%s' as a valid ref.", rev);
 220
 221        commit = lookup_commit_reference(sha1);
 222        if (!commit)
 223                die("Could not parse object '%s'.", rev);
 224        hashcpy(sha1, commit->object.sha1);
 225
 226        if (i < argc && !strcmp(argv[i], "--"))
 227                i++;
 228        else if (i < argc && argv[i][0] == '-')
 229                usage(builtin_reset_usage);
 230
 231        /* git reset tree [--] paths... can be used to
 232         * load chosen paths from the tree into the index without
 233         * affecting the working tree nor HEAD. */
 234        if (i < argc) {
 235                if (reset_type == MIXED)
 236                        warning("--mixed option is deprecated with paths.");
 237                else if (reset_type != NONE)
 238                        die("Cannot do %s reset with paths.",
 239                                        reset_type_names[reset_type]);
 240                if (read_from_tree(prefix, argv + i, sha1))
 241                        return 1;
 242                return update_index_refresh() ? 1 : 0;
 243        }
 244        if (reset_type == NONE)
 245                reset_type = MIXED; /* by default */
 246
 247        /* Soft reset does not touch the index file nor the working tree
 248         * at all, but requires them in a good order.  Other resets reset
 249         * the index file to the tree object we are switching to. */
 250        if (reset_type == SOFT) {
 251                if (is_merge() || unmerged_files())
 252                        die("Cannot do a soft reset in the middle of a merge.");
 253        }
 254        else if (reset_index_file(sha1, (reset_type == HARD)))
 255                die("Could not reset index file to revision '%s'.", rev);
 256
 257        /* Any resets update HEAD to the head being switched to,
 258         * saving the previous head in ORIG_HEAD before. */
 259        if (!get_sha1("ORIG_HEAD", sha1_old_orig))
 260                old_orig = sha1_old_orig;
 261        if (!get_sha1("HEAD", sha1_orig)) {
 262                orig = sha1_orig;
 263                prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
 264                update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
 265        }
 266        else if (old_orig)
 267                delete_ref("ORIG_HEAD", old_orig);
 268        prepend_reflog_action("updating HEAD", msg, sizeof(msg));
 269        update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
 270
 271        switch (reset_type) {
 272        case HARD:
 273                if (!update_ref_status)
 274                        print_new_head_line(commit);
 275                break;
 276        case SOFT: /* Nothing else to do. */
 277                break;
 278        case MIXED: /* Report what has not been updated. */
 279                update_index_refresh();
 280                break;
 281        }
 282
 283        unlink(git_path("MERGE_HEAD"));
 284        unlink(git_path("rr-cache/MERGE_RR"));
 285        unlink(git_path("MERGE_MSG"));
 286        unlink(git_path("SQUASH_MSG"));
 287
 288        free(reflog_action);
 289
 290        return update_ref_status;
 291}