reset: don't allow "git reset -- $pathspec" in bare repo
[gitweb.git] / builtin / reset.c
index 915cc9f86f855d517abaa55d31c502899fb77220..664fad9fc586cf0276e4bf8e1315d2eae8ceb911 100644 (file)
@@ -119,34 +119,23 @@ static void print_new_head_line(struct commit *commit)
 
 static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
 {
-       int result;
-
        if (!index_lock) {
                index_lock = xcalloc(1, sizeof(struct lock_file));
                fd = hold_locked_index(index_lock, 1);
        }
 
-       if (read_cache() < 0)
-               return error(_("Could not read index"));
-
-       result = refresh_index(&the_index, (flags), NULL, NULL,
-                              _("Unstaged changes after reset:")) ? 1 : 0;
+       refresh_index(&the_index, (flags), NULL, NULL,
+                     _("Unstaged changes after reset:"));
        if (write_cache(fd, active_cache, active_nr) ||
                        commit_locked_index(index_lock))
                return error ("Could not refresh index");
-       return result;
+       return 0;
 }
 
 static void update_index_from_diff(struct diff_queue_struct *q,
                struct diff_options *opt, void *data)
 {
        int i;
-       int *discard_flag = data;
-
-       /* do_diff_cache() mangled the index */
-       discard_cache();
-       *discard_flag = 1;
-       read_cache();
 
        for (i = 0; i < q->nr; i++) {
                struct diff_filespec *one = q->queue[i]->one;
@@ -164,32 +153,19 @@ static void update_index_from_diff(struct diff_queue_struct *q,
        }
 }
 
-static int interactive_reset(const char *revision, const char **argv,
-                            const char *prefix)
-{
-       const char **pathspec = NULL;
-
-       if (*argv)
-               pathspec = get_pathspec(prefix, argv);
-
-       return run_add_interactive(revision, "--patch=reset", pathspec);
-}
-
-static int read_from_tree(const char *prefix, const char **argv,
-               unsigned char *tree_sha1, int refresh_flags)
+static int read_from_tree(const char **pathspec, unsigned char *tree_sha1,
+                         int refresh_flags)
 {
        struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
-       int index_fd, index_was_discarded = 0;
+       int index_fd;
        struct diff_options opt;
 
        memset(&opt, 0, sizeof(opt));
-       diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
+       diff_tree_setup_paths(pathspec, &opt);
        opt.output_format = DIFF_FORMAT_CALLBACK;
        opt.format_callback = update_index_from_diff;
-       opt.format_callback_data = &index_was_discarded;
 
        index_fd = hold_locked_index(lock, 1);
-       index_was_discarded = 0;
        read_cache();
        if (do_diff_cache(tree_sha1, &opt))
                return 1;
@@ -197,9 +173,6 @@ static int read_from_tree(const char *prefix, const char **argv,
        diff_flush(&opt);
        diff_tree_release_paths(&opt);
 
-       if (!index_was_discarded)
-               /* The index is still clobbered from do_diff_cache() */
-               discard_cache();
        return update_index_refresh(index_fd, lock, refresh_flags);
 }
 
@@ -232,6 +205,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
        const char *rev = "HEAD";
        unsigned char sha1[20], *orig = NULL, sha1_orig[20],
                                *old_orig = NULL, sha1_old_orig[20];
+       const char **pathspec = NULL;
        struct commit *commit;
        struct strbuf msg = STRBUF_INIT;
        const struct option options[] = {
@@ -303,23 +277,24 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                die(_("Could not parse object '%s'."), rev);
        hashcpy(sha1, commit->object.sha1);
 
+       if (i < argc)
+               pathspec = get_pathspec(prefix, argv + i);
+
        if (patch_mode) {
                if (reset_type != NONE)
                        die(_("--patch is incompatible with --{hard,mixed,soft}"));
-               return interactive_reset(rev, argv + i, prefix);
+               return run_add_interactive(rev, "--patch=reset", pathspec);
        }
 
        /* git reset tree [--] paths... can be used to
         * load chosen paths from the tree into the index without
         * affecting the working tree nor HEAD. */
-       if (i < argc) {
+       if (pathspec) {
                if (reset_type == MIXED)
                        warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
                else if (reset_type != NONE)
                        die(_("Cannot do %s reset with paths."),
                                        _(reset_type_names[reset_type]));
-               return read_from_tree(prefix, argv + i, sha1,
-                               quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
        }
        if (reset_type == NONE)
                reset_type = MIXED; /* by default */
@@ -331,6 +306,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                die(_("%s reset is not allowed in a bare repository"),
                    _(reset_type_names[reset_type]));
 
+       if (pathspec)
+               return read_from_tree(pathspec, sha1,
+                               quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
+
        /* Soft reset does not touch the index file nor the working tree
         * at all, but requires them in a good order.  Other resets reset
         * the index file to the tree object we are switching to. */