checkout: show progress when checkout takes long time while switching branches
[git/git.git] / builtin-checkout.c
CommitLineData
782c2d65
DB
1#include "cache.h"
2#include "builtin.h"
3#include "parse-options.h"
4#include "refs.h"
5#include "commit.h"
6#include "tree.h"
7#include "tree-walk.h"
8#include "unpack-trees.h"
9#include "dir.h"
10#include "run-command.h"
11#include "merge-recursive.h"
12#include "branch.h"
13#include "diff.h"
14#include "revision.h"
79a1e6b4 15#include "remote.h"
782c2d65
DB
16
17static const char * const checkout_usage[] = {
18 "git checkout [options] <branch>",
19 "git checkout [options] [<branch>] -- <file>...",
20 NULL,
21};
22
23static int post_checkout_hook(struct commit *old, struct commit *new,
24 int changed)
25{
26 struct child_process proc;
27 const char *name = git_path("hooks/post-checkout");
28 const char *argv[5];
29
30 if (access(name, X_OK) < 0)
31 return 0;
32
33 memset(&proc, 0, sizeof(proc));
34 argv[0] = name;
35 argv[1] = xstrdup(sha1_to_hex(old->object.sha1));
36 argv[2] = xstrdup(sha1_to_hex(new->object.sha1));
37 argv[3] = changed ? "1" : "0";
38 argv[4] = NULL;
39 proc.argv = argv;
40 proc.no_stdin = 1;
41 proc.stdout_to_stderr = 1;
42 return run_command(&proc);
43}
44
45static int update_some(const unsigned char *sha1, const char *base, int baselen,
46 const char *pathname, unsigned mode, int stage)
47{
48 int len;
49 struct cache_entry *ce;
50
51 if (S_ISGITLINK(mode))
52 return 0;
53
54 if (S_ISDIR(mode))
55 return READ_TREE_RECURSIVE;
56
57 len = baselen + strlen(pathname);
58 ce = xcalloc(1, cache_entry_size(len));
59 hashcpy(ce->sha1, sha1);
60 memcpy(ce->name, base, baselen);
61 memcpy(ce->name + baselen, pathname, len - baselen);
62 ce->ce_flags = create_ce_flags(len, 0);
63 ce->ce_mode = create_ce_mode(mode);
64 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
65 return 0;
66}
67
68static int read_tree_some(struct tree *tree, const char **pathspec)
69{
70 int newfd;
71 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
72 newfd = hold_locked_index(lock_file, 1);
73 read_cache();
74
75 read_tree_recursive(tree, "", 0, 0, pathspec, update_some);
76
77 if (write_cache(newfd, active_cache, active_nr) ||
78 commit_locked_index(lock_file))
79 die("unable to write new index file");
80
81 /* update the index with the given tree's info
82 * for all args, expanding wildcards, and exit
83 * with any non-zero return code.
84 */
85 return 0;
86}
87
88static int checkout_paths(const char **pathspec)
89{
90 int pos;
91 struct checkout state;
92 static char *ps_matched;
93 unsigned char rev[20];
94 int flag;
95 struct commit *head;
96
97 for (pos = 0; pathspec[pos]; pos++)
98 ;
99 ps_matched = xcalloc(1, pos);
100
101 for (pos = 0; pos < active_nr; pos++) {
102 struct cache_entry *ce = active_cache[pos];
103 pathspec_match(pathspec, ps_matched, ce->name, 0);
104 }
105
106 if (report_path_error(ps_matched, pathspec, 0))
107 return 1;
108
109 memset(&state, 0, sizeof(state));
110 state.force = 1;
111 state.refresh_cache = 1;
112 for (pos = 0; pos < active_nr; pos++) {
113 struct cache_entry *ce = active_cache[pos];
114 if (pathspec_match(pathspec, NULL, ce->name, 0)) {
115 checkout_entry(ce, &state, NULL);
116 }
117 }
118
119 resolve_ref("HEAD", rev, 0, &flag);
120 head = lookup_commit_reference_gently(rev, 1);
121
122 return post_checkout_hook(head, head, 0);
123}
124
125static void show_local_changes(struct object *head)
126{
127 struct rev_info rev;
128 /* I think we want full paths, even if we're in a subdirectory. */
129 init_revisions(&rev, NULL);
130 rev.abbrev = 0;
131 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;
132 add_pending_object(&rev, head, NULL);
133 run_diff_index(&rev, 0);
134}
135
136static void describe_detached_head(char *msg, struct commit *commit)
137{
138 struct strbuf sb;
139 strbuf_init(&sb, 0);
140 parse_commit(commit);
141 pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, "", "", 0, 0);
142 fprintf(stderr, "%s %s... %s\n", msg,
143 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
144 strbuf_release(&sb);
145}
146
147static int reset_to_new(struct tree *tree, int quiet)
148{
149 struct unpack_trees_options opts;
150 struct tree_desc tree_desc;
151 memset(&opts, 0, sizeof(opts));
152 opts.head_idx = -1;
153 opts.update = 1;
154 opts.reset = 1;
155 opts.merge = 1;
156 opts.fn = oneway_merge;
157 opts.verbose_update = !quiet;
158 parse_tree(tree);
159 init_tree_desc(&tree_desc, tree->buffer, tree->size);
160 if (unpack_trees(1, &tree_desc, &opts))
161 return 128;
162 return 0;
163}
164
165static void reset_clean_to_new(struct tree *tree, int quiet)
166{
167 struct unpack_trees_options opts;
168 struct tree_desc tree_desc;
169 memset(&opts, 0, sizeof(opts));
170 opts.head_idx = -1;
171 opts.skip_unmerged = 1;
172 opts.reset = 1;
173 opts.merge = 1;
174 opts.fn = oneway_merge;
175 opts.verbose_update = !quiet;
176 parse_tree(tree);
177 init_tree_desc(&tree_desc, tree->buffer, tree->size);
178 if (unpack_trees(1, &tree_desc, &opts))
179 exit(128);
180}
181
182struct checkout_opts {
183 int quiet;
184 int merge;
185 int force;
186
187 char *new_branch;
188 int new_branch_log;
189 int track;
190};
191
192struct branch_info {
193 const char *name; /* The short name used */
194 const char *path; /* The full name of a real branch */
195 struct commit *commit; /* The named commit */
196};
197
198static void setup_branch_path(struct branch_info *branch)
199{
200 struct strbuf buf;
201 strbuf_init(&buf, 0);
202 strbuf_addstr(&buf, "refs/heads/");
203 strbuf_addstr(&buf, branch->name);
204 branch->path = strbuf_detach(&buf, NULL);
205}
206
207static int merge_working_tree(struct checkout_opts *opts,
75ea38df 208 struct branch_info *old, struct branch_info *new)
782c2d65
DB
209{
210 int ret;
211 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
212 int newfd = hold_locked_index(lock_file, 1);
213 read_cache();
214
215 if (opts->force) {
216 ret = reset_to_new(new->commit->tree, opts->quiet);
217 if (ret)
218 return ret;
219 } else {
220 struct tree_desc trees[2];
221 struct tree *tree;
222 struct unpack_trees_options topts;
223 memset(&topts, 0, sizeof(topts));
224 topts.head_idx = -1;
225
226 refresh_cache(REFRESH_QUIET);
227
228 if (unmerged_cache()) {
229 ret = opts->merge ? -1 :
230 error("you need to resolve your current index first");
231 } else {
232 topts.update = 1;
233 topts.merge = 1;
234 topts.gently = opts->merge;
52229a29 235 topts.verbose_update = !opts->quiet;
782c2d65
DB
236 topts.fn = twoway_merge;
237 topts.dir = xcalloc(1, sizeof(*topts.dir));
238 topts.dir->show_ignored = 1;
239 topts.dir->exclude_per_dir = ".gitignore";
782c2d65
DB
240 tree = parse_tree_indirect(old->commit->object.sha1);
241 init_tree_desc(&trees[0], tree->buffer, tree->size);
242 tree = parse_tree_indirect(new->commit->object.sha1);
243 init_tree_desc(&trees[1], tree->buffer, tree->size);
244 ret = unpack_trees(2, trees, &topts);
245 }
246 if (ret) {
247 /*
248 * Unpack couldn't do a trivial merge; either
249 * give up or do a real merge, depending on
250 * whether the merge flag was used.
251 */
252 struct tree *result;
253 struct tree *work;
254 if (!opts->merge)
255 return 1;
256 parse_commit(old->commit);
257
258 /* Do more real merge */
259
260 /*
261 * We update the index fully, then write the
262 * tree from the index, then merge the new
263 * branch with the current tree, with the old
264 * branch as the base. Then we reset the index
265 * (but not the working tree) to the new
266 * branch, leaving the working tree as the
267 * merged version, but skipping unmerged
268 * entries in the index.
269 */
270
271 add_files_to_cache(0, NULL, NULL);
272 work = write_tree_from_memory();
273
274 ret = reset_to_new(new->commit->tree, opts->quiet);
275 if (ret)
276 return ret;
277 merge_trees(new->commit->tree, work, old->commit->tree,
278 new->name, "local", &result);
279 reset_clean_to_new(new->commit->tree, opts->quiet);
280 }
281 }
282
283 if (write_cache(newfd, active_cache, active_nr) ||
284 commit_locked_index(lock_file))
285 die("unable to write new index file");
286
287 if (!opts->force)
288 show_local_changes(&new->commit->object);
289
290 return 0;
291}
292
b56fca07 293static void report_tracking(struct branch_info *new, struct checkout_opts *opts)
79a1e6b4
JH
294{
295 /*
296 * We have switched to a new branch; is it building on
297 * top of another branch, and if so does that other branch
298 * have changes we do not have yet?
299 */
300 char *base;
301 unsigned char sha1[20];
302 struct commit *ours, *theirs;
79a1e6b4 303 char symmetric[84];
b0030db3
JH
304 struct rev_info revs;
305 const char *rev_argv[10];
306 int rev_argc;
307 int num_ours, num_theirs;
308 const char *remote_msg;
b56fca07 309 struct branch *branch = branch_get(new->name);
79a1e6b4 310
b0030db3
JH
311 /*
312 * Nothing to report unless we are marked to build on top of
313 * somebody else.
314 */
b56fca07 315 if (!branch || !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
79a1e6b4 316 return;
79a1e6b4 317
79a1e6b4 318 /*
b0030db3
JH
319 * If what we used to build on no longer exists, there is
320 * nothing to report.
79a1e6b4 321 */
b0030db3 322 base = branch->merge[0]->dst;
79a1e6b4
JH
323 if (!resolve_ref(base, sha1, 1, NULL))
324 return;
79a1e6b4 325
b0030db3
JH
326 theirs = lookup_commit(sha1);
327 ours = new->commit;
79a1e6b4
JH
328 if (!hashcmp(sha1, ours->object.sha1))
329 return; /* we are the same */
330
b0030db3
JH
331 /* Run "rev-list --left-right ours...theirs" internally... */
332 rev_argc = 0;
333 rev_argv[rev_argc++] = NULL;
334 rev_argv[rev_argc++] = "--left-right";
335 rev_argv[rev_argc++] = symmetric;
336 rev_argv[rev_argc++] = "--";
337 rev_argv[rev_argc] = NULL;
338
339 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
340 strcpy(symmetric + 40, "...");
341 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
342
343 init_revisions(&revs, NULL);
344 setup_revisions(rev_argc, rev_argv, &revs, NULL);
345 prepare_revision_walk(&revs);
346
347 /* ... and count the commits on each side. */
348 num_ours = 0;
349 num_theirs = 0;
350 while (1) {
351 struct commit *c = get_revision(&revs);
352 if (!c)
353 break;
354 if (c->object.flags & SYMMETRIC_LEFT)
355 num_ours++;
356 else
357 num_theirs++;
79a1e6b4 358 }
79a1e6b4 359
b0030db3
JH
360 if (!prefixcmp(base, "refs/remotes/")) {
361 remote_msg = " remote";
79a1e6b4 362 base += strlen("refs/remotes/");
b0030db3
JH
363 } else {
364 remote_msg = "";
79a1e6b4 365 }
79a1e6b4 366
b0030db3
JH
367 if (!num_theirs)
368 printf("Your branch is ahead of the tracked%s branch '%s' "
369 "by %d commit%s.\n",
370 remote_msg, base,
371 num_ours, (num_ours == 1) ? "" : "s");
372 else if (!num_ours)
b56fca07 373 printf("Your branch is behind the tracked%s branch '%s' "
b0030db3
JH
374 "by %d commit%s,\n"
375 "and can be fast-forwarded.\n",
376 remote_msg, base,
377 num_theirs, (num_theirs == 1) ? "" : "s");
378 else
379 printf("Your branch and the tracked%s branch '%s' "
380 "have diverged,\nand respectively "
381 "have %d and %d different commit(s) each.\n",
382 remote_msg, base,
383 num_ours, num_theirs);
384}
79a1e6b4 385
782c2d65
DB
386static void update_refs_for_switch(struct checkout_opts *opts,
387 struct branch_info *old,
388 struct branch_info *new)
389{
390 struct strbuf msg;
391 const char *old_desc;
392 if (opts->new_branch) {
393 create_branch(old->name, opts->new_branch, new->name, 0,
394 opts->new_branch_log, opts->track);
395 new->name = opts->new_branch;
396 setup_branch_path(new);
397 }
398
399 strbuf_init(&msg, 0);
400 old_desc = old->name;
401 if (!old_desc)
402 old_desc = sha1_to_hex(old->commit->object.sha1);
403 strbuf_addf(&msg, "checkout: moving from %s to %s",
404 old_desc, new->name);
405
406 if (new->path) {
407 create_symref("HEAD", new->path, msg.buf);
408 if (!opts->quiet) {
409 if (old->path && !strcmp(new->path, old->path))
410 fprintf(stderr, "Already on \"%s\"\n",
411 new->name);
412 else
413 fprintf(stderr, "Switched to%s branch \"%s\"\n",
414 opts->new_branch ? " a new" : "",
415 new->name);
416 }
417 } else if (strcmp(new->name, "HEAD")) {
418 update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
419 REF_NODEREF, DIE_ON_ERR);
420 if (!opts->quiet) {
421 if (old->path)
422 fprintf(stderr, "Note: moving to \"%s\" which isn't a local branch\nIf you want to create a new branch from this checkout, you may do so\n(now or later) by using -b with the checkout command again. Example:\n git checkout -b <new_branch_name>\n", new->name);
423 describe_detached_head("HEAD is now at", new->commit);
424 }
425 }
426 remove_branch_state();
427 strbuf_release(&msg);
b0030db3 428 if (!opts->quiet && (new->path || !strcmp(new->name, "HEAD")))
b56fca07 429 report_tracking(new, opts);
782c2d65
DB
430}
431
75ea38df 432static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
782c2d65
DB
433{
434 int ret = 0;
435 struct branch_info old;
436 unsigned char rev[20];
437 int flag;
438 memset(&old, 0, sizeof(old));
439 old.path = resolve_ref("HEAD", rev, 0, &flag);
440 old.commit = lookup_commit_reference_gently(rev, 1);
441 if (!(flag & REF_ISSYMREF))
442 old.path = NULL;
443
444 if (old.path && !prefixcmp(old.path, "refs/heads/"))
445 old.name = old.path + strlen("refs/heads/");
446
447 if (!new->name) {
448 new->name = "HEAD";
449 new->commit = old.commit;
450 if (!new->commit)
451 die("You are on a branch yet to be born");
452 parse_commit(new->commit);
453 }
454
455 /*
456 * If the new thing isn't a branch and isn't HEAD and we're
457 * not starting a new branch, and we want messages, and we
458 * weren't on a branch, and we're moving to a new commit,
459 * describe the old commit.
460 */
461 if (!new->path && strcmp(new->name, "HEAD") && !opts->new_branch &&
462 !opts->quiet && !old.path && new->commit != old.commit)
463 describe_detached_head("Previous HEAD position was", old.commit);
464
465 if (!old.commit) {
466 if (!opts->quiet) {
467 fprintf(stderr, "warning: You appear to be on a branch yet to be born.\n");
468 fprintf(stderr, "warning: Forcing checkout of %s.\n", new->name);
469 }
470 opts->force = 1;
471 }
472
75ea38df 473 ret = merge_working_tree(opts, &old, new);
782c2d65
DB
474 if (ret)
475 return ret;
476
477 update_refs_for_switch(opts, &old, new);
478
479 return post_checkout_hook(old.commit, new->commit, 1);
480}
481
482static int branch_track = 0;
483
484static int git_checkout_config(const char *var, const char *value)
485{
486 if (!strcmp(var, "branch.autosetupmerge"))
487 branch_track = git_config_bool(var, value);
488
489 return git_default_config(var, value);
490}
491
492int cmd_checkout(int argc, const char **argv, const char *prefix)
493{
494 struct checkout_opts opts;
495 unsigned char rev[20];
496 const char *arg;
497 struct branch_info new;
498 struct tree *source_tree = NULL;
499 struct option options[] = {
500 OPT__QUIET(&opts.quiet),
501 OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
502 OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
503 OPT_BOOLEAN( 0 , "track", &opts.track, "track"),
504 OPT_BOOLEAN('f', NULL, &opts.force, "force"),
505 OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
b249b552 506 OPT_END(),
782c2d65
DB
507 };
508
509 memset(&opts, 0, sizeof(opts));
510 memset(&new, 0, sizeof(new));
511
512 git_config(git_checkout_config);
513
514 opts.track = branch_track;
515
516 argc = parse_options(argc, argv, options, checkout_usage, 0);
517 if (argc) {
518 arg = argv[0];
519 if (get_sha1(arg, rev))
520 ;
521 else if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
522 new.name = arg;
523 setup_branch_path(&new);
524 if (resolve_ref(new.path, rev, 1, NULL))
525 new.commit = lookup_commit_reference(rev);
526 else
527 new.path = NULL;
528 parse_commit(new.commit);
529 source_tree = new.commit->tree;
530 argv++;
531 argc--;
532 } else if ((source_tree = parse_tree_indirect(rev))) {
533 argv++;
534 argc--;
535 }
536 }
537
538 if (argc && !strcmp(argv[0], "--")) {
539 argv++;
540 argc--;
541 }
542
543 if (!opts.new_branch && (opts.track != branch_track))
544 die("git checkout: --track and --no-track require -b");
545
546 if (opts.force && opts.merge)
547 die("git checkout: -f and -m are incompatible");
548
549 if (argc) {
550 const char **pathspec = get_pathspec(prefix, argv);
551 /* Checkout paths */
552 if (opts.new_branch || opts.force || opts.merge) {
553 if (argc == 1) {
554 die("git checkout: updating paths is incompatible with switching branches/forcing\nDid you intend to checkout '%s' which can not be resolved as commit?", argv[0]);
555 } else {
556 die("git checkout: updating paths is incompatible with switching branches/forcing");
557 }
558 }
559
560 if (source_tree)
561 read_tree_some(source_tree, pathspec);
562 else
563 read_cache();
564 return checkout_paths(pathspec);
565 }
566
567 if (new.name && !new.commit) {
568 die("Cannot switch branch to a non-commit.");
569 }
570
75ea38df 571 return switch_branches(&opts, &new);
782c2d65 572}