rename: warn user when we have turned off rename detection
[alt-git.git] / builtin-checkout.c
blobb0cd78856418319ed46b2e0cf372deffc050bb80
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"
15 #include "remote.h"
17 static const char * const checkout_usage[] = {
18 "git checkout [options] <branch>",
19 "git checkout [options] [<branch>] -- <file>...",
20 NULL,
23 static int post_checkout_hook(struct commit *old, struct commit *new,
24 int changed)
26 struct child_process proc;
27 const char *name = git_path("hooks/post-checkout");
28 const char *argv[5];
30 if (access(name, X_OK) < 0)
31 return 0;
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);
45 static int update_some(const unsigned char *sha1, const char *base, int baselen,
46 const char *pathname, unsigned mode, int stage)
48 int len;
49 struct cache_entry *ce;
51 if (S_ISGITLINK(mode))
52 return 0;
54 if (S_ISDIR(mode))
55 return READ_TREE_RECURSIVE;
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;
68 static int read_tree_some(struct tree *tree, const char **pathspec)
70 read_tree_recursive(tree, "", 0, 0, pathspec, update_some);
72 /* update the index with the given tree's info
73 * for all args, expanding wildcards, and exit
74 * with any non-zero return code.
76 return 0;
79 static int checkout_paths(struct tree *source_tree, const char **pathspec)
81 int pos;
82 struct checkout state;
83 static char *ps_matched;
84 unsigned char rev[20];
85 int flag;
86 struct commit *head;
88 int newfd;
89 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
91 newfd = hold_locked_index(lock_file, 1);
92 read_cache();
94 if (source_tree)
95 read_tree_some(source_tree, pathspec);
97 for (pos = 0; pathspec[pos]; pos++)
99 ps_matched = xcalloc(1, pos);
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);
106 if (report_path_error(ps_matched, pathspec, 0))
107 return 1;
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);
119 if (write_cache(newfd, active_cache, active_nr) ||
120 commit_locked_index(lock_file))
121 die("unable to write new index file");
123 resolve_ref("HEAD", rev, 0, &flag);
124 head = lookup_commit_reference_gently(rev, 1);
126 return post_checkout_hook(head, head, 0);
129 static void show_local_changes(struct object *head)
131 struct rev_info rev;
132 /* I think we want full paths, even if we're in a subdirectory. */
133 init_revisions(&rev, NULL);
134 rev.abbrev = 0;
135 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;
136 add_pending_object(&rev, head, NULL);
137 run_diff_index(&rev, 0);
140 static void describe_detached_head(char *msg, struct commit *commit)
142 struct strbuf sb;
143 strbuf_init(&sb, 0);
144 parse_commit(commit);
145 pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, "", "", 0, 0);
146 fprintf(stderr, "%s %s... %s\n", msg,
147 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
148 strbuf_release(&sb);
151 static int reset_to_new(struct tree *tree, int quiet)
153 struct unpack_trees_options opts;
154 struct tree_desc tree_desc;
155 memset(&opts, 0, sizeof(opts));
156 opts.head_idx = -1;
157 opts.update = 1;
158 opts.reset = 1;
159 opts.merge = 1;
160 opts.fn = oneway_merge;
161 opts.verbose_update = !quiet;
162 parse_tree(tree);
163 init_tree_desc(&tree_desc, tree->buffer, tree->size);
164 if (unpack_trees(1, &tree_desc, &opts))
165 return 128;
166 return 0;
169 static void reset_clean_to_new(struct tree *tree, int quiet)
171 struct unpack_trees_options opts;
172 struct tree_desc tree_desc;
173 memset(&opts, 0, sizeof(opts));
174 opts.head_idx = -1;
175 opts.skip_unmerged = 1;
176 opts.reset = 1;
177 opts.merge = 1;
178 opts.fn = oneway_merge;
179 opts.verbose_update = !quiet;
180 parse_tree(tree);
181 init_tree_desc(&tree_desc, tree->buffer, tree->size);
182 if (unpack_trees(1, &tree_desc, &opts))
183 exit(128);
186 struct checkout_opts {
187 int quiet;
188 int merge;
189 int force;
191 char *new_branch;
192 int new_branch_log;
193 enum branch_track track;
196 struct branch_info {
197 const char *name; /* The short name used */
198 const char *path; /* The full name of a real branch */
199 struct commit *commit; /* The named commit */
202 static void setup_branch_path(struct branch_info *branch)
204 struct strbuf buf;
205 strbuf_init(&buf, 0);
206 strbuf_addstr(&buf, "refs/heads/");
207 strbuf_addstr(&buf, branch->name);
208 branch->path = strbuf_detach(&buf, NULL);
211 static int merge_working_tree(struct checkout_opts *opts,
212 struct branch_info *old, struct branch_info *new)
214 int ret;
215 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
216 int newfd = hold_locked_index(lock_file, 1);
217 read_cache();
219 if (opts->force) {
220 ret = reset_to_new(new->commit->tree, opts->quiet);
221 if (ret)
222 return ret;
223 } else {
224 struct tree_desc trees[2];
225 struct tree *tree;
226 struct unpack_trees_options topts;
227 memset(&topts, 0, sizeof(topts));
228 topts.head_idx = -1;
230 refresh_cache(REFRESH_QUIET);
232 if (unmerged_cache()) {
233 error("you need to resolve your current index first");
234 return 1;
237 /* 2-way merge to the new branch */
238 topts.update = 1;
239 topts.merge = 1;
240 topts.gently = opts->merge;
241 topts.verbose_update = !opts->quiet;
242 topts.fn = twoway_merge;
243 topts.dir = xcalloc(1, sizeof(*topts.dir));
244 topts.dir->show_ignored = 1;
245 topts.dir->exclude_per_dir = ".gitignore";
246 tree = parse_tree_indirect(old->commit->object.sha1);
247 init_tree_desc(&trees[0], tree->buffer, tree->size);
248 tree = parse_tree_indirect(new->commit->object.sha1);
249 init_tree_desc(&trees[1], tree->buffer, tree->size);
251 if (unpack_trees(2, trees, &topts)) {
253 * Unpack couldn't do a trivial merge; either
254 * give up or do a real merge, depending on
255 * whether the merge flag was used.
257 struct tree *result;
258 struct tree *work;
259 if (!opts->merge)
260 return 1;
261 parse_commit(old->commit);
263 /* Do more real merge */
266 * We update the index fully, then write the
267 * tree from the index, then merge the new
268 * branch with the current tree, with the old
269 * branch as the base. Then we reset the index
270 * (but not the working tree) to the new
271 * branch, leaving the working tree as the
272 * merged version, but skipping unmerged
273 * entries in the index.
276 add_files_to_cache(0, NULL, NULL);
277 work = write_tree_from_memory();
279 ret = reset_to_new(new->commit->tree, opts->quiet);
280 if (ret)
281 return ret;
282 merge_trees(new->commit->tree, work, old->commit->tree,
283 new->name, "local", &result);
284 reset_clean_to_new(new->commit->tree, opts->quiet);
288 if (write_cache(newfd, active_cache, active_nr) ||
289 commit_locked_index(lock_file))
290 die("unable to write new index file");
292 if (!opts->force)
293 show_local_changes(&new->commit->object);
295 return 0;
298 static void report_tracking(struct branch_info *new, struct checkout_opts *opts)
301 * We have switched to a new branch; is it building on
302 * top of another branch, and if so does that other branch
303 * have changes we do not have yet?
305 char *base;
306 unsigned char sha1[20];
307 struct commit *ours, *theirs;
308 char symmetric[84];
309 struct rev_info revs;
310 const char *rev_argv[10];
311 int rev_argc;
312 int num_ours, num_theirs;
313 const char *remote_msg;
314 struct branch *branch = branch_get(new->name);
317 * Nothing to report unless we are marked to build on top of
318 * somebody else.
320 if (!branch || !branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
321 return;
324 * If what we used to build on no longer exists, there is
325 * nothing to report.
327 base = branch->merge[0]->dst;
328 if (!resolve_ref(base, sha1, 1, NULL))
329 return;
331 theirs = lookup_commit(sha1);
332 ours = new->commit;
333 if (!hashcmp(sha1, ours->object.sha1))
334 return; /* we are the same */
336 /* Run "rev-list --left-right ours...theirs" internally... */
337 rev_argc = 0;
338 rev_argv[rev_argc++] = NULL;
339 rev_argv[rev_argc++] = "--left-right";
340 rev_argv[rev_argc++] = symmetric;
341 rev_argv[rev_argc++] = "--";
342 rev_argv[rev_argc] = NULL;
344 strcpy(symmetric, sha1_to_hex(ours->object.sha1));
345 strcpy(symmetric + 40, "...");
346 strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1));
348 init_revisions(&revs, NULL);
349 setup_revisions(rev_argc, rev_argv, &revs, NULL);
350 prepare_revision_walk(&revs);
352 /* ... and count the commits on each side. */
353 num_ours = 0;
354 num_theirs = 0;
355 while (1) {
356 struct commit *c = get_revision(&revs);
357 if (!c)
358 break;
359 if (c->object.flags & SYMMETRIC_LEFT)
360 num_ours++;
361 else
362 num_theirs++;
365 if (!prefixcmp(base, "refs/remotes/")) {
366 remote_msg = " remote";
367 base += strlen("refs/remotes/");
368 } else {
369 remote_msg = "";
372 if (!num_theirs)
373 printf("Your branch is ahead of the tracked%s branch '%s' "
374 "by %d commit%s.\n",
375 remote_msg, base,
376 num_ours, (num_ours == 1) ? "" : "s");
377 else if (!num_ours)
378 printf("Your branch is behind the tracked%s branch '%s' "
379 "by %d commit%s,\n"
380 "and can be fast-forwarded.\n",
381 remote_msg, base,
382 num_theirs, (num_theirs == 1) ? "" : "s");
383 else
384 printf("Your branch and the tracked%s branch '%s' "
385 "have diverged,\nand respectively "
386 "have %d and %d different commit(s) each.\n",
387 remote_msg, base,
388 num_ours, num_theirs);
391 static void update_refs_for_switch(struct checkout_opts *opts,
392 struct branch_info *old,
393 struct branch_info *new)
395 struct strbuf msg;
396 const char *old_desc;
397 if (opts->new_branch) {
398 create_branch(old->name, opts->new_branch, new->name, 0,
399 opts->new_branch_log, opts->track);
400 new->name = opts->new_branch;
401 setup_branch_path(new);
404 strbuf_init(&msg, 0);
405 old_desc = old->name;
406 if (!old_desc)
407 old_desc = sha1_to_hex(old->commit->object.sha1);
408 strbuf_addf(&msg, "checkout: moving from %s to %s",
409 old_desc, new->name);
411 if (new->path) {
412 create_symref("HEAD", new->path, msg.buf);
413 if (!opts->quiet) {
414 if (old->path && !strcmp(new->path, old->path))
415 fprintf(stderr, "Already on \"%s\"\n",
416 new->name);
417 else
418 fprintf(stderr, "Switched to%s branch \"%s\"\n",
419 opts->new_branch ? " a new" : "",
420 new->name);
422 } else if (strcmp(new->name, "HEAD")) {
423 update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
424 REF_NODEREF, DIE_ON_ERR);
425 if (!opts->quiet) {
426 if (old->path)
427 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);
428 describe_detached_head("HEAD is now at", new->commit);
431 remove_branch_state();
432 strbuf_release(&msg);
433 if (!opts->quiet && (new->path || !strcmp(new->name, "HEAD")))
434 report_tracking(new, opts);
437 static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
439 int ret = 0;
440 struct branch_info old;
441 unsigned char rev[20];
442 int flag;
443 memset(&old, 0, sizeof(old));
444 old.path = resolve_ref("HEAD", rev, 0, &flag);
445 old.commit = lookup_commit_reference_gently(rev, 1);
446 if (!(flag & REF_ISSYMREF))
447 old.path = NULL;
449 if (old.path && !prefixcmp(old.path, "refs/heads/"))
450 old.name = old.path + strlen("refs/heads/");
452 if (!new->name) {
453 new->name = "HEAD";
454 new->commit = old.commit;
455 if (!new->commit)
456 die("You are on a branch yet to be born");
457 parse_commit(new->commit);
461 * If the new thing isn't a branch and isn't HEAD and we're
462 * not starting a new branch, and we want messages, and we
463 * weren't on a branch, and we're moving to a new commit,
464 * describe the old commit.
466 if (!new->path && strcmp(new->name, "HEAD") && !opts->new_branch &&
467 !opts->quiet && !old.path && new->commit != old.commit)
468 describe_detached_head("Previous HEAD position was", old.commit);
470 if (!old.commit) {
471 if (!opts->quiet) {
472 fprintf(stderr, "warning: You appear to be on a branch yet to be born.\n");
473 fprintf(stderr, "warning: Forcing checkout of %s.\n", new->name);
475 opts->force = 1;
478 ret = merge_working_tree(opts, &old, new);
479 if (ret)
480 return ret;
482 update_refs_for_switch(opts, &old, new);
484 return post_checkout_hook(old.commit, new->commit, 1);
487 static int git_checkout_config(const char *var, const char *value)
489 return git_default_config(var, value);
492 int cmd_checkout(int argc, const char **argv, const char *prefix)
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_SET_INT( 0 , "track", &opts.track, "track",
504 BRANCH_TRACK_EXPLICIT),
505 OPT_BOOLEAN('f', NULL, &opts.force, "force"),
506 OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
507 OPT_END(),
510 memset(&opts, 0, sizeof(opts));
511 memset(&new, 0, sizeof(new));
513 git_config(git_checkout_config);
515 opts.track = git_branch_track;
517 argc = parse_options(argc, argv, options, checkout_usage, 0);
518 if (argc) {
519 arg = argv[0];
520 if (get_sha1(arg, rev))
522 else if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
523 new.name = arg;
524 setup_branch_path(&new);
525 if (resolve_ref(new.path, rev, 1, NULL))
526 new.commit = lookup_commit_reference(rev);
527 else
528 new.path = NULL;
529 parse_commit(new.commit);
530 source_tree = new.commit->tree;
531 argv++;
532 argc--;
533 } else if ((source_tree = parse_tree_indirect(rev))) {
534 argv++;
535 argc--;
539 if (argc && !strcmp(argv[0], "--")) {
540 argv++;
541 argc--;
544 if (!opts.new_branch && (opts.track != git_branch_track))
545 die("git checkout: --track and --no-track require -b");
547 if (opts.force && opts.merge)
548 die("git checkout: -f and -m are incompatible");
550 if (argc) {
551 const char **pathspec = get_pathspec(prefix, argv);
553 if (!pathspec)
554 die("invalid path specification");
556 /* Checkout paths */
557 if (opts.new_branch || opts.force || opts.merge) {
558 if (argc == 1) {
559 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]);
560 } else {
561 die("git checkout: updating paths is incompatible with switching branches/forcing");
565 return checkout_paths(source_tree, pathspec);
568 if (new.name && !new.commit) {
569 die("Cannot switch branch to a non-commit.");
572 return switch_branches(&opts, &new);