Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 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" |
Daniel Barkalow | c369e7b | 2008-02-07 11:40:16 -0500 | [diff] [blame] | 19 | #include "branch.h" |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 20 | #include "parse-options.h" |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 21 | #include "unpack-trees.h" |
| 22 | #include "cache-tree.h" |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 23 | |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 24 | static const char * const git_reset_usage[] = { |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 25 | "git reset [--mixed | --soft | --hard | --merge] [-q] [<commit>]", |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 26 | "git reset [--mixed] <commit> [--] <paths>...", |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 27 | NULL |
| 28 | }; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 29 | |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 30 | enum reset_type { MIXED, SOFT, HARD, MERGE, NONE }; |
| 31 | static const char *reset_type_names[] = { "mixed", "soft", "hard", "merge", NULL }; |
| 32 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 33 | static char *args_to_str(const char **argv) |
| 34 | { |
| 35 | char *buf = NULL; |
| 36 | unsigned long len, space = 0, nr = 0; |
| 37 | |
| 38 | for (; *argv; argv++) { |
| 39 | len = strlen(*argv); |
| 40 | ALLOC_GROW(buf, nr + 1 + len, space); |
| 41 | if (nr) |
| 42 | buf[nr++] = ' '; |
| 43 | memcpy(buf + nr, *argv, len); |
| 44 | nr += len; |
| 45 | } |
| 46 | ALLOC_GROW(buf, nr + 1, space); |
| 47 | buf[nr] = '\0'; |
| 48 | |
| 49 | return buf; |
| 50 | } |
| 51 | |
| 52 | static inline int is_merge(void) |
| 53 | { |
| 54 | return !access(git_path("MERGE_HEAD"), F_OK); |
| 55 | } |
| 56 | |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 57 | static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 58 | { |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 59 | int nr = 1; |
| 60 | int newfd; |
| 61 | struct tree_desc desc[2]; |
| 62 | struct unpack_trees_options opts; |
| 63 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 64 | |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 65 | memset(&opts, 0, sizeof(opts)); |
| 66 | opts.head_idx = 1; |
| 67 | opts.src_index = &the_index; |
| 68 | opts.dst_index = &the_index; |
| 69 | opts.fn = oneway_merge; |
| 70 | opts.merge = 1; |
Jamis Buck | 5aa965a | 2008-05-31 18:10:58 -0700 | [diff] [blame] | 71 | if (!quiet) |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 72 | opts.verbose_update = 1; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 73 | switch (reset_type) { |
| 74 | case MERGE: |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 75 | opts.update = 1; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 76 | break; |
| 77 | case HARD: |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 78 | opts.update = 1; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 79 | /* fallthrough */ |
| 80 | default: |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 81 | opts.reset = 1; |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 82 | } |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 83 | |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 84 | newfd = hold_locked_index(lock, 1); |
| 85 | |
| 86 | read_cache_unmerged(); |
| 87 | |
| 88 | if (!fill_tree_descriptor(desc + nr - 1, sha1)) |
| 89 | return error("Failed to find tree of %s.", sha1_to_hex(sha1)); |
| 90 | if (unpack_trees(nr, desc, &opts)) |
| 91 | return -1; |
| 92 | if (write_cache(newfd, active_cache, active_nr) || |
| 93 | commit_locked_index(lock)) |
| 94 | return error("Could not write new index file."); |
| 95 | |
| 96 | return 0; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void print_new_head_line(struct commit *commit) |
| 100 | { |
Junio C Hamano | 2efb3b0 | 2008-03-01 23:43:32 -0800 | [diff] [blame] | 101 | const char *hex, *body; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 102 | |
| 103 | hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); |
Junio C Hamano | 2efb3b0 | 2008-03-01 23:43:32 -0800 | [diff] [blame] | 104 | printf("HEAD is now at %s", hex); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 105 | body = strstr(commit->buffer, "\n\n"); |
| 106 | if (body) { |
| 107 | const char *eol; |
| 108 | size_t len; |
| 109 | body += 2; |
| 110 | eol = strchr(body, '\n'); |
| 111 | len = eol ? eol - body : strlen(body); |
| 112 | printf(" %.*s\n", (int) len, body); |
| 113 | } |
| 114 | else |
| 115 | printf("\n"); |
| 116 | } |
| 117 | |
Stephan Beyer | b0320ea | 2008-07-25 22:49:08 +0200 | [diff] [blame] | 118 | static int update_index_refresh(int fd, struct lock_file *index_lock, int flags) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 119 | { |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 120 | int result; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 121 | |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 122 | if (!index_lock) { |
| 123 | index_lock = xcalloc(1, sizeof(struct lock_file)); |
| 124 | fd = hold_locked_index(index_lock, 1); |
| 125 | } |
| 126 | |
| 127 | if (read_cache() < 0) |
| 128 | return error("Could not read index"); |
Stephan Beyer | b0320ea | 2008-07-25 22:49:08 +0200 | [diff] [blame] | 129 | |
Matthieu Moy | 3deffc5 | 2009-08-21 10:57:59 +0200 | [diff] [blame] | 130 | result = refresh_index(&the_index, (flags), NULL, NULL, |
| 131 | "Unstaged changes after reset:") ? 1 : 0; |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 132 | if (write_cache(fd, active_cache, active_nr) || |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 133 | commit_locked_index(index_lock)) |
| 134 | return error ("Could not refresh index"); |
| 135 | return result; |
| 136 | } |
Johannes Schindelin | 2e7a978 | 2007-11-03 13:12:17 +0000 | [diff] [blame] | 137 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 138 | static void update_index_from_diff(struct diff_queue_struct *q, |
| 139 | struct diff_options *opt, void *data) |
| 140 | { |
| 141 | int i; |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 142 | int *discard_flag = data; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 143 | |
| 144 | /* do_diff_cache() mangled the index */ |
| 145 | discard_cache(); |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 146 | *discard_flag = 1; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 147 | read_cache(); |
| 148 | |
| 149 | for (i = 0; i < q->nr; i++) { |
| 150 | struct diff_filespec *one = q->queue[i]->one; |
| 151 | if (one->mode) { |
| 152 | struct cache_entry *ce; |
| 153 | ce = make_cache_entry(one->mode, one->sha1, one->path, |
| 154 | 0, 0); |
Dmitry Potapov | d09e2cd | 2008-10-05 06:14:40 +0400 | [diff] [blame] | 155 | if (!ce) |
| 156 | die("make_cache_entry failed for path '%s'", |
| 157 | one->path); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 158 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | |
| 159 | ADD_CACHE_OK_TO_REPLACE); |
| 160 | } else |
| 161 | remove_file_from_cache(one->path); |
| 162 | } |
| 163 | } |
| 164 | |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 165 | static int interactive_reset(const char *revision, const char **argv, |
| 166 | const char *prefix) |
| 167 | { |
| 168 | const char **pathspec = NULL; |
| 169 | |
| 170 | if (*argv) |
| 171 | pathspec = get_pathspec(prefix, argv); |
| 172 | |
| 173 | return run_add_interactive(revision, "--patch=reset", pathspec); |
| 174 | } |
| 175 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 176 | static int read_from_tree(const char *prefix, const char **argv, |
Stephan Beyer | b0320ea | 2008-07-25 22:49:08 +0200 | [diff] [blame] | 177 | unsigned char *tree_sha1, int refresh_flags) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 178 | { |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 179 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
| 180 | int index_fd, index_was_discarded = 0; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 181 | struct diff_options opt; |
| 182 | |
| 183 | memset(&opt, 0, sizeof(opt)); |
| 184 | diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt); |
| 185 | opt.output_format = DIFF_FORMAT_CALLBACK; |
| 186 | opt.format_callback = update_index_from_diff; |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 187 | opt.format_callback_data = &index_was_discarded; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 188 | |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 189 | index_fd = hold_locked_index(lock, 1); |
| 190 | index_was_discarded = 0; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 191 | read_cache(); |
| 192 | if (do_diff_cache(tree_sha1, &opt)) |
| 193 | return 1; |
| 194 | diffcore_std(&opt); |
| 195 | diff_flush(&opt); |
Mike Hommey | 03b69c7 | 2007-12-11 22:59:55 +0100 | [diff] [blame] | 196 | diff_tree_release_paths(&opt); |
Johannes Schindelin | 2e7a978 | 2007-11-03 13:12:17 +0000 | [diff] [blame] | 197 | |
Johannes Schindelin | 620a6cd | 2007-11-03 15:21:21 +0000 | [diff] [blame] | 198 | if (!index_was_discarded) |
| 199 | /* The index is still clobbered from do_diff_cache() */ |
| 200 | discard_cache(); |
Stephan Beyer | b0320ea | 2008-07-25 22:49:08 +0200 | [diff] [blame] | 201 | return update_index_refresh(index_fd, lock, refresh_flags); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void prepend_reflog_action(const char *action, char *buf, size_t size) |
| 205 | { |
| 206 | const char *sep = ": "; |
| 207 | const char *rla = getenv("GIT_REFLOG_ACTION"); |
| 208 | if (!rla) |
| 209 | rla = sep = ""; |
| 210 | if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size) |
| 211 | warning("Reflog action message too long: %.*s...", 50, buf); |
| 212 | } |
| 213 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 214 | int cmd_reset(int argc, const char **argv, const char *prefix) |
| 215 | { |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 216 | int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0; |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 217 | int patch_mode = 0; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 218 | const char *rev = "HEAD"; |
| 219 | unsigned char sha1[20], *orig = NULL, sha1_orig[20], |
| 220 | *old_orig = NULL, sha1_old_orig[20]; |
| 221 | struct commit *commit; |
| 222 | char *reflog_action, msg[1024]; |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 223 | const struct option options[] = { |
Felipe Contreras | 5d2dcc4 | 2009-12-02 23:28:40 +0200 | [diff] [blame] | 224 | OPT__QUIET(&quiet), |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 225 | OPT_SET_INT(0, "mixed", &reset_type, |
| 226 | "reset HEAD and index", MIXED), |
| 227 | OPT_SET_INT(0, "soft", &reset_type, "reset only HEAD", SOFT), |
| 228 | OPT_SET_INT(0, "hard", &reset_type, |
| 229 | "reset HEAD, index and working tree", HARD), |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 230 | OPT_SET_INT(0, "merge", &reset_type, |
| 231 | "reset HEAD, index and working tree", MERGE), |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 232 | OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"), |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 233 | OPT_END() |
| 234 | }; |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 235 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 236 | git_config(git_default_config, NULL); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 237 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 238 | argc = parse_options(argc, argv, prefix, options, git_reset_usage, |
Carlos Rica | 5eee6b2 | 2008-03-04 23:11:34 +0100 | [diff] [blame] | 239 | PARSE_OPT_KEEP_DASHDASH); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 240 | reflog_action = args_to_str(argv); |
| 241 | setenv("GIT_REFLOG_ACTION", reflog_action, 0); |
| 242 | |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 243 | /* |
| 244 | * Possible arguments are: |
| 245 | * |
| 246 | * git reset [-opts] <rev> <paths>... |
| 247 | * git reset [-opts] <rev> -- <paths>... |
| 248 | * git reset [-opts] -- <paths>... |
| 249 | * git reset [-opts] <paths>... |
| 250 | * |
| 251 | * At this point, argv[i] points immediately after [-opts]. |
| 252 | */ |
| 253 | |
| 254 | if (i < argc) { |
| 255 | if (!strcmp(argv[i], "--")) { |
| 256 | i++; /* reset to HEAD, possibly with paths */ |
| 257 | } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) { |
| 258 | rev = argv[i]; |
| 259 | i += 2; |
| 260 | } |
| 261 | /* |
| 262 | * Otherwise, argv[i] could be either <rev> or <paths> and |
Mike Ralphson | 3ea3c21 | 2009-04-17 19:13:30 +0100 | [diff] [blame] | 263 | * has to be unambiguous. |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 264 | */ |
| 265 | else if (!get_sha1(argv[i], sha1)) { |
| 266 | /* |
| 267 | * Ok, argv[i] looks like a rev; it should not |
| 268 | * be a filename. |
| 269 | */ |
| 270 | verify_non_filename(prefix, argv[i]); |
| 271 | rev = argv[i++]; |
| 272 | } else { |
| 273 | /* Otherwise we treat this as a filename */ |
| 274 | verify_filename(prefix, argv[i]); |
| 275 | } |
| 276 | } |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 277 | |
| 278 | if (get_sha1(rev, sha1)) |
| 279 | die("Failed to resolve '%s' as a valid ref.", rev); |
| 280 | |
| 281 | commit = lookup_commit_reference(sha1); |
| 282 | if (!commit) |
| 283 | die("Could not parse object '%s'.", rev); |
| 284 | hashcpy(sha1, commit->object.sha1); |
| 285 | |
Thomas Rast | d002ef4 | 2009-08-15 13:48:31 +0200 | [diff] [blame] | 286 | if (patch_mode) { |
| 287 | if (reset_type != NONE) |
| 288 | die("--patch is incompatible with --{hard,mixed,soft}"); |
| 289 | return interactive_reset(rev, argv + i, prefix); |
| 290 | } |
| 291 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 292 | /* git reset tree [--] paths... can be used to |
| 293 | * load chosen paths from the tree into the index without |
| 294 | * affecting the working tree nor HEAD. */ |
| 295 | if (i < argc) { |
| 296 | if (reset_type == MIXED) |
| 297 | warning("--mixed option is deprecated with paths."); |
| 298 | else if (reset_type != NONE) |
| 299 | die("Cannot do %s reset with paths.", |
| 300 | reset_type_names[reset_type]); |
Stephan Beyer | b0320ea | 2008-07-25 22:49:08 +0200 | [diff] [blame] | 301 | return read_from_tree(prefix, argv + i, sha1, |
Matthieu Moy | 43673fd | 2009-08-21 10:57:58 +0200 | [diff] [blame] | 302 | quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 303 | } |
| 304 | if (reset_type == NONE) |
| 305 | reset_type = MIXED; /* by default */ |
| 306 | |
Jeff King | cd0f0f6 | 2009-12-30 03:47:03 -0500 | [diff] [blame] | 307 | if (reset_type == HARD || reset_type == MERGE) |
| 308 | setup_work_tree(); |
Jeff King | 49b9362 | 2007-12-31 02:13:52 -0500 | [diff] [blame] | 309 | |
Christian Couder | 2b06b0a | 2009-12-30 06:54:44 +0100 | [diff] [blame] | 310 | if (reset_type == MIXED && is_bare_repository()) |
| 311 | die("%s reset is not allowed in a bare repository", |
| 312 | reset_type_names[reset_type]); |
| 313 | |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 314 | /* Soft reset does not touch the index file nor the working tree |
| 315 | * at all, but requires them in a good order. Other resets reset |
| 316 | * the index file to the tree object we are switching to. */ |
| 317 | if (reset_type == SOFT) { |
Daniel Barkalow | 94a5728 | 2008-02-07 11:40:13 -0500 | [diff] [blame] | 318 | if (is_merge() || read_cache() < 0 || unmerged_cache()) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 319 | die("Cannot do a soft reset in the middle of a merge."); |
| 320 | } |
Linus Torvalds | 9e8ecea | 2008-12-01 09:30:31 -0800 | [diff] [blame] | 321 | else if (reset_index_file(sha1, reset_type, quiet)) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 322 | die("Could not reset index file to revision '%s'.", rev); |
| 323 | |
| 324 | /* Any resets update HEAD to the head being switched to, |
| 325 | * saving the previous head in ORIG_HEAD before. */ |
| 326 | if (!get_sha1("ORIG_HEAD", sha1_old_orig)) |
| 327 | old_orig = sha1_old_orig; |
| 328 | if (!get_sha1("HEAD", sha1_orig)) { |
| 329 | orig = sha1_orig; |
| 330 | prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg)); |
| 331 | update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR); |
| 332 | } |
| 333 | else if (old_orig) |
Miklos Vajna | eca35a2 | 2008-10-26 03:33:56 +0100 | [diff] [blame] | 334 | delete_ref("ORIG_HEAD", old_orig, 0); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 335 | prepend_reflog_action("updating HEAD", msg, sizeof(msg)); |
| 336 | update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR); |
| 337 | |
| 338 | switch (reset_type) { |
| 339 | case HARD: |
Gerrit Pape | 521b53e | 2007-11-04 09:37:20 +0000 | [diff] [blame] | 340 | if (!update_ref_status && !quiet) |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 341 | print_new_head_line(commit); |
| 342 | break; |
| 343 | case SOFT: /* Nothing else to do. */ |
| 344 | break; |
| 345 | case MIXED: /* Report what has not been updated. */ |
Stephan Beyer | b0320ea | 2008-07-25 22:49:08 +0200 | [diff] [blame] | 346 | update_index_refresh(0, NULL, |
Matthieu Moy | 43673fd | 2009-08-21 10:57:58 +0200 | [diff] [blame] | 347 | quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | |
Daniel Barkalow | c369e7b | 2008-02-07 11:40:16 -0500 | [diff] [blame] | 351 | remove_branch_state(); |
Carlos Rica | 0e5a7fa | 2007-09-11 05:19:34 +0200 | [diff] [blame] | 352 | |
| 353 | free(reflog_action); |
| 354 | |
| 355 | return update_ref_status; |
| 356 | } |