3 #include "parse-options.h"
8 #include "unpack-trees.h"
10 #include "run-command.h"
11 #include "merge-recursive.h"
17 static const char * const checkout_usage
[] = {
18 "git checkout [options] <branch>",
19 "git checkout [options] [<branch>] -- <file>...",
23 static int post_checkout_hook(struct commit
*old
, struct commit
*new,
26 struct child_process proc
;
27 const char *name
= git_path("hooks/post-checkout");
30 if (access(name
, X_OK
) < 0)
33 memset(&proc
, 0, sizeof(proc
));
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";
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
)
49 struct cache_entry
*ce
;
51 if (S_ISGITLINK(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
);
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.
79 static int checkout_paths(struct tree
*source_tree
, const char **pathspec
)
82 struct checkout state
;
83 static char *ps_matched
;
84 unsigned char rev
[20];
89 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
91 newfd
= hold_locked_index(lock_file
, 1);
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))
109 memset(&state
, 0, sizeof(state
));
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
)
132 /* I think we want full paths, even if we're in a subdirectory. */
133 init_revisions(&rev
, NULL
);
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
)
144 parse_commit(commit
);
145 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, &sb
, 0, NULL
, NULL
, 0, 0);
146 fprintf(stderr
, "%s %s... %s\n", msg
,
147 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
), sb
.buf
);
151 static int reset_to_new(struct tree
*tree
, int quiet
)
153 struct unpack_trees_options opts
;
154 struct tree_desc tree_desc
;
156 memset(&opts
, 0, sizeof(opts
));
161 opts
.fn
= oneway_merge
;
162 opts
.verbose_update
= !quiet
;
163 opts
.src_index
= &the_index
;
164 opts
.dst_index
= &the_index
;
166 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
167 if (unpack_trees(1, &tree_desc
, &opts
))
172 static void reset_clean_to_new(struct tree
*tree
, int quiet
)
174 struct unpack_trees_options opts
;
175 struct tree_desc tree_desc
;
177 memset(&opts
, 0, sizeof(opts
));
179 opts
.skip_unmerged
= 1;
182 opts
.fn
= oneway_merge
;
183 opts
.verbose_update
= !quiet
;
184 opts
.src_index
= &the_index
;
185 opts
.dst_index
= &the_index
;
187 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
188 if (unpack_trees(1, &tree_desc
, &opts
))
192 struct checkout_opts
{
199 enum branch_track track
;
203 const char *name
; /* The short name used */
204 const char *path
; /* The full name of a real branch */
205 struct commit
*commit
; /* The named commit */
208 static void setup_branch_path(struct branch_info
*branch
)
211 strbuf_init(&buf
, 0);
212 strbuf_addstr(&buf
, "refs/heads/");
213 strbuf_addstr(&buf
, branch
->name
);
214 branch
->path
= strbuf_detach(&buf
, NULL
);
217 static int merge_working_tree(struct checkout_opts
*opts
,
218 struct branch_info
*old
, struct branch_info
*new)
221 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
222 int newfd
= hold_locked_index(lock_file
, 1);
226 ret
= reset_to_new(new->commit
->tree
, opts
->quiet
);
230 struct tree_desc trees
[2];
232 struct unpack_trees_options topts
;
234 memset(&topts
, 0, sizeof(topts
));
236 topts
.src_index
= &the_index
;
237 topts
.dst_index
= &the_index
;
239 topts
.msgs
.not_uptodate_file
= "You have local changes to '%s'; cannot switch branches.";
241 refresh_cache(REFRESH_QUIET
);
243 if (unmerged_cache()) {
244 error("you need to resolve your current index first");
248 /* 2-way merge to the new branch */
251 topts
.gently
= opts
->merge
;
252 topts
.verbose_update
= !opts
->quiet
;
253 topts
.fn
= twoway_merge
;
254 topts
.dir
= xcalloc(1, sizeof(*topts
.dir
));
255 topts
.dir
->show_ignored
= 1;
256 topts
.dir
->exclude_per_dir
= ".gitignore";
257 tree
= parse_tree_indirect(old
->commit
->object
.sha1
);
258 init_tree_desc(&trees
[0], tree
->buffer
, tree
->size
);
259 tree
= parse_tree_indirect(new->commit
->object
.sha1
);
260 init_tree_desc(&trees
[1], tree
->buffer
, tree
->size
);
262 if (unpack_trees(2, trees
, &topts
)) {
264 * Unpack couldn't do a trivial merge; either
265 * give up or do a real merge, depending on
266 * whether the merge flag was used.
272 parse_commit(old
->commit
);
274 /* Do more real merge */
277 * We update the index fully, then write the
278 * tree from the index, then merge the new
279 * branch with the current tree, with the old
280 * branch as the base. Then we reset the index
281 * (but not the working tree) to the new
282 * branch, leaving the working tree as the
283 * merged version, but skipping unmerged
284 * entries in the index.
287 add_files_to_cache(NULL
, NULL
, 0);
288 work
= write_tree_from_memory();
290 ret
= reset_to_new(new->commit
->tree
, opts
->quiet
);
293 merge_trees(new->commit
->tree
, work
, old
->commit
->tree
,
294 new->name
, "local", &result
);
295 reset_clean_to_new(new->commit
->tree
, opts
->quiet
);
299 if (write_cache(newfd
, active_cache
, active_nr
) ||
300 commit_locked_index(lock_file
))
301 die("unable to write new index file");
304 show_local_changes(&new->commit
->object
);
309 static void report_tracking(struct branch_info
*new, struct checkout_opts
*opts
)
312 * We have switched to a new branch; is it building on
313 * top of another branch, and if so does that other branch
314 * have changes we do not have yet?
317 unsigned char sha1
[20];
318 struct commit
*ours
, *theirs
;
320 struct rev_info revs
;
321 const char *rev_argv
[10];
323 int num_ours
, num_theirs
;
324 const char *remote_msg
;
325 struct branch
*branch
= branch_get(new->name
);
328 * Nothing to report unless we are marked to build on top of
331 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
335 * If what we used to build on no longer exists, there is
338 base
= branch
->merge
[0]->dst
;
339 if (!resolve_ref(base
, sha1
, 1, NULL
))
342 theirs
= lookup_commit(sha1
);
344 if (!hashcmp(sha1
, ours
->object
.sha1
))
345 return; /* we are the same */
347 /* Run "rev-list --left-right ours...theirs" internally... */
349 rev_argv
[rev_argc
++] = NULL
;
350 rev_argv
[rev_argc
++] = "--left-right";
351 rev_argv
[rev_argc
++] = symmetric
;
352 rev_argv
[rev_argc
++] = "--";
353 rev_argv
[rev_argc
] = NULL
;
355 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
356 strcpy(symmetric
+ 40, "...");
357 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
359 init_revisions(&revs
, NULL
);
360 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
361 prepare_revision_walk(&revs
);
363 /* ... and count the commits on each side. */
367 struct commit
*c
= get_revision(&revs
);
370 if (c
->object
.flags
& SYMMETRIC_LEFT
)
376 if (!prefixcmp(base
, "refs/remotes/")) {
377 remote_msg
= " remote";
378 base
+= strlen("refs/remotes/");
384 printf("Your branch is ahead of the tracked%s branch '%s' "
387 num_ours
, (num_ours
== 1) ? "" : "s");
389 printf("Your branch is behind the tracked%s branch '%s' "
391 "and can be fast-forwarded.\n",
393 num_theirs
, (num_theirs
== 1) ? "" : "s");
395 printf("Your branch and the tracked%s branch '%s' "
396 "have diverged,\nand respectively "
397 "have %d and %d different commit(s) each.\n",
399 num_ours
, num_theirs
);
402 static void update_refs_for_switch(struct checkout_opts
*opts
,
403 struct branch_info
*old
,
404 struct branch_info
*new)
407 const char *old_desc
;
408 if (opts
->new_branch
) {
409 create_branch(old
->name
, opts
->new_branch
, new->name
, 0,
410 opts
->new_branch_log
, opts
->track
);
411 new->name
= opts
->new_branch
;
412 setup_branch_path(new);
415 strbuf_init(&msg
, 0);
416 old_desc
= old
->name
;
418 old_desc
= sha1_to_hex(old
->commit
->object
.sha1
);
419 strbuf_addf(&msg
, "checkout: moving from %s to %s",
420 old_desc
, new->name
);
423 create_symref("HEAD", new->path
, msg
.buf
);
425 if (old
->path
&& !strcmp(new->path
, old
->path
))
426 fprintf(stderr
, "Already on \"%s\"\n",
429 fprintf(stderr
, "Switched to%s branch \"%s\"\n",
430 opts
->new_branch
? " a new" : "",
433 } else if (strcmp(new->name
, "HEAD")) {
434 update_ref(msg
.buf
, "HEAD", new->commit
->object
.sha1
, NULL
,
435 REF_NODEREF
, DIE_ON_ERR
);
438 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
);
439 describe_detached_head("HEAD is now at", new->commit
);
442 remove_branch_state();
443 strbuf_release(&msg
);
444 if (!opts
->quiet
&& (new->path
|| !strcmp(new->name
, "HEAD")))
445 report_tracking(new, opts
);
448 static int switch_branches(struct checkout_opts
*opts
, struct branch_info
*new)
451 struct branch_info old
;
452 unsigned char rev
[20];
454 memset(&old
, 0, sizeof(old
));
455 old
.path
= resolve_ref("HEAD", rev
, 0, &flag
);
456 old
.commit
= lookup_commit_reference_gently(rev
, 1);
457 if (!(flag
& REF_ISSYMREF
))
460 if (old
.path
&& !prefixcmp(old
.path
, "refs/heads/"))
461 old
.name
= old
.path
+ strlen("refs/heads/");
465 new->commit
= old
.commit
;
467 die("You are on a branch yet to be born");
468 parse_commit(new->commit
);
472 * If the new thing isn't a branch and isn't HEAD and we're
473 * not starting a new branch, and we want messages, and we
474 * weren't on a branch, and we're moving to a new commit,
475 * describe the old commit.
477 if (!new->path
&& strcmp(new->name
, "HEAD") && !opts
->new_branch
&&
478 !opts
->quiet
&& !old
.path
&& new->commit
!= old
.commit
)
479 describe_detached_head("Previous HEAD position was", old
.commit
);
483 fprintf(stderr
, "warning: You appear to be on a branch yet to be born.\n");
484 fprintf(stderr
, "warning: Forcing checkout of %s.\n", new->name
);
489 ret
= merge_working_tree(opts
, &old
, new);
493 update_refs_for_switch(opts
, &old
, new);
495 return post_checkout_hook(old
.commit
, new->commit
, 1);
498 int cmd_checkout(int argc
, const char **argv
, const char *prefix
)
500 struct checkout_opts opts
;
501 unsigned char rev
[20];
503 struct branch_info
new;
504 struct tree
*source_tree
= NULL
;
505 struct option options
[] = {
506 OPT__QUIET(&opts
.quiet
),
507 OPT_STRING('b', NULL
, &opts
.new_branch
, "new branch", "branch"),
508 OPT_BOOLEAN('l', NULL
, &opts
.new_branch_log
, "log for new branch"),
509 OPT_SET_INT('t', "track", &opts
.track
, "track",
510 BRANCH_TRACK_EXPLICIT
),
511 OPT_BOOLEAN('f', NULL
, &opts
.force
, "force"),
512 OPT_BOOLEAN('m', NULL
, &opts
.merge
, "merge"),
516 memset(&opts
, 0, sizeof(opts
));
517 memset(&new, 0, sizeof(new));
519 git_config(git_default_config
);
521 opts
.track
= git_branch_track
;
523 argc
= parse_options(argc
, argv
, options
, checkout_usage
, 0);
526 if (get_sha1(arg
, rev
))
528 else if ((new.commit
= lookup_commit_reference_gently(rev
, 1))) {
530 setup_branch_path(&new);
531 if (resolve_ref(new.path
, rev
, 1, NULL
))
532 new.commit
= lookup_commit_reference(rev
);
535 parse_commit(new.commit
);
536 source_tree
= new.commit
->tree
;
539 } else if ((source_tree
= parse_tree_indirect(rev
))) {
545 if (argc
&& !strcmp(argv
[0], "--")) {
550 if (!opts
.new_branch
&& (opts
.track
!= git_branch_track
))
551 die("git checkout: --track and --no-track require -b");
553 if (opts
.force
&& opts
.merge
)
554 die("git checkout: -f and -m are incompatible");
557 const char **pathspec
= get_pathspec(prefix
, argv
);
560 die("invalid path specification");
563 if (opts
.new_branch
|| opts
.force
|| opts
.merge
) {
565 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]);
567 die("git checkout: updating paths is incompatible with switching branches/forcing");
571 return checkout_paths(source_tree
, pathspec
);
574 if (new.name
&& !new.commit
) {
575 die("Cannot switch branch to a non-commit.");
578 return switch_branches(&opts
, &new);