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
)
71 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
72 newfd
= hold_locked_index(lock_file
, 1);
75 read_tree_recursive(tree
, "", 0, 0, pathspec
, update_some
);
77 if (write_cache(newfd
, active_cache
, active_nr
) ||
78 commit_locked_index(lock_file
))
79 die("unable to write new index file");
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.
88 static int checkout_paths(const char **pathspec
)
91 struct checkout state
;
92 static char *ps_matched
;
93 unsigned char rev
[20];
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 resolve_ref("HEAD", rev
, 0, &flag
);
120 head
= lookup_commit_reference_gently(rev
, 1);
122 return post_checkout_hook(head
, head
, 0);
125 static void show_local_changes(struct object
*head
)
128 /* I think we want full paths, even if we're in a subdirectory. */
129 init_revisions(&rev
, NULL
);
131 rev
.diffopt
.output_format
|= DIFF_FORMAT_NAME_STATUS
;
132 add_pending_object(&rev
, head
, NULL
);
133 run_diff_index(&rev
, 0);
136 static void describe_detached_head(char *msg
, struct commit
*commit
)
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
);
147 static int reset_to_new(struct tree
*tree
, int quiet
)
149 struct unpack_trees_options opts
;
150 struct tree_desc tree_desc
;
151 memset(&opts
, 0, sizeof(opts
));
156 opts
.fn
= oneway_merge
;
157 opts
.verbose_update
= !quiet
;
159 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
160 if (unpack_trees(1, &tree_desc
, &opts
))
165 static void reset_clean_to_new(struct tree
*tree
, int quiet
)
167 struct unpack_trees_options opts
;
168 struct tree_desc tree_desc
;
169 memset(&opts
, 0, sizeof(opts
));
171 opts
.skip_unmerged
= 1;
174 opts
.fn
= oneway_merge
;
175 opts
.verbose_update
= !quiet
;
177 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
178 if (unpack_trees(1, &tree_desc
, &opts
))
182 struct checkout_opts
{
189 enum branch_track track
;
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 */
198 static void setup_branch_path(struct branch_info
*branch
)
201 strbuf_init(&buf
, 0);
202 strbuf_addstr(&buf
, "refs/heads/");
203 strbuf_addstr(&buf
, branch
->name
);
204 branch
->path
= strbuf_detach(&buf
, NULL
);
207 static int merge_working_tree(struct checkout_opts
*opts
,
208 struct branch_info
*old
, struct branch_info
*new)
211 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
212 int newfd
= hold_locked_index(lock_file
, 1);
216 ret
= reset_to_new(new->commit
->tree
, opts
->quiet
);
220 struct tree_desc trees
[2];
222 struct unpack_trees_options topts
;
223 memset(&topts
, 0, sizeof(topts
));
226 refresh_cache(REFRESH_QUIET
);
228 if (unmerged_cache()) {
229 error("you need to resolve your current index first");
233 /* 2-way merge to the new branch */
236 topts
.gently
= opts
->merge
;
237 topts
.verbose_update
= !opts
->quiet
;
238 topts
.fn
= twoway_merge
;
239 topts
.dir
= xcalloc(1, sizeof(*topts
.dir
));
240 topts
.dir
->show_ignored
= 1;
241 topts
.dir
->exclude_per_dir
= ".gitignore";
242 tree
= parse_tree_indirect(old
->commit
->object
.sha1
);
243 init_tree_desc(&trees
[0], tree
->buffer
, tree
->size
);
244 tree
= parse_tree_indirect(new->commit
->object
.sha1
);
245 init_tree_desc(&trees
[1], tree
->buffer
, tree
->size
);
247 if (unpack_trees(2, trees
, &topts
)) {
249 * Unpack couldn't do a trivial merge; either
250 * give up or do a real merge, depending on
251 * whether the merge flag was used.
257 parse_commit(old
->commit
);
259 /* Do more real merge */
262 * We update the index fully, then write the
263 * tree from the index, then merge the new
264 * branch with the current tree, with the old
265 * branch as the base. Then we reset the index
266 * (but not the working tree) to the new
267 * branch, leaving the working tree as the
268 * merged version, but skipping unmerged
269 * entries in the index.
272 add_files_to_cache(0, NULL
, NULL
);
273 work
= write_tree_from_memory();
275 ret
= reset_to_new(new->commit
->tree
, opts
->quiet
);
278 merge_trees(new->commit
->tree
, work
, old
->commit
->tree
,
279 new->name
, "local", &result
);
280 reset_clean_to_new(new->commit
->tree
, opts
->quiet
);
284 if (write_cache(newfd
, active_cache
, active_nr
) ||
285 commit_locked_index(lock_file
))
286 die("unable to write new index file");
289 show_local_changes(&new->commit
->object
);
294 static void report_tracking(struct branch_info
*new, struct checkout_opts
*opts
)
297 * We have switched to a new branch; is it building on
298 * top of another branch, and if so does that other branch
299 * have changes we do not have yet?
302 unsigned char sha1
[20];
303 struct commit
*ours
, *theirs
;
305 struct rev_info revs
;
306 const char *rev_argv
[10];
308 int num_ours
, num_theirs
;
309 const char *remote_msg
;
310 struct branch
*branch
= branch_get(new->name
);
313 * Nothing to report unless we are marked to build on top of
316 if (!branch
|| !branch
->merge
|| !branch
->merge
[0] || !branch
->merge
[0]->dst
)
320 * If what we used to build on no longer exists, there is
323 base
= branch
->merge
[0]->dst
;
324 if (!resolve_ref(base
, sha1
, 1, NULL
))
327 theirs
= lookup_commit(sha1
);
329 if (!hashcmp(sha1
, ours
->object
.sha1
))
330 return; /* we are the same */
332 /* Run "rev-list --left-right ours...theirs" internally... */
334 rev_argv
[rev_argc
++] = NULL
;
335 rev_argv
[rev_argc
++] = "--left-right";
336 rev_argv
[rev_argc
++] = symmetric
;
337 rev_argv
[rev_argc
++] = "--";
338 rev_argv
[rev_argc
] = NULL
;
340 strcpy(symmetric
, sha1_to_hex(ours
->object
.sha1
));
341 strcpy(symmetric
+ 40, "...");
342 strcpy(symmetric
+ 43, sha1_to_hex(theirs
->object
.sha1
));
344 init_revisions(&revs
, NULL
);
345 setup_revisions(rev_argc
, rev_argv
, &revs
, NULL
);
346 prepare_revision_walk(&revs
);
348 /* ... and count the commits on each side. */
352 struct commit
*c
= get_revision(&revs
);
355 if (c
->object
.flags
& SYMMETRIC_LEFT
)
361 if (!prefixcmp(base
, "refs/remotes/")) {
362 remote_msg
= " remote";
363 base
+= strlen("refs/remotes/");
369 printf("Your branch is ahead of the tracked%s branch '%s' "
372 num_ours
, (num_ours
== 1) ? "" : "s");
374 printf("Your branch is behind the tracked%s branch '%s' "
376 "and can be fast-forwarded.\n",
378 num_theirs
, (num_theirs
== 1) ? "" : "s");
380 printf("Your branch and the tracked%s branch '%s' "
381 "have diverged,\nand respectively "
382 "have %d and %d different commit(s) each.\n",
384 num_ours
, num_theirs
);
387 static void update_refs_for_switch(struct checkout_opts
*opts
,
388 struct branch_info
*old
,
389 struct branch_info
*new)
392 const char *old_desc
;
393 if (opts
->new_branch
) {
394 create_branch(old
->name
, opts
->new_branch
, new->name
, 0,
395 opts
->new_branch_log
, opts
->track
);
396 new->name
= opts
->new_branch
;
397 setup_branch_path(new);
400 strbuf_init(&msg
, 0);
401 old_desc
= old
->name
;
403 old_desc
= sha1_to_hex(old
->commit
->object
.sha1
);
404 strbuf_addf(&msg
, "checkout: moving from %s to %s",
405 old_desc
, new->name
);
408 create_symref("HEAD", new->path
, msg
.buf
);
410 if (old
->path
&& !strcmp(new->path
, old
->path
))
411 fprintf(stderr
, "Already on \"%s\"\n",
414 fprintf(stderr
, "Switched to%s branch \"%s\"\n",
415 opts
->new_branch
? " a new" : "",
418 } else if (strcmp(new->name
, "HEAD")) {
419 update_ref(msg
.buf
, "HEAD", new->commit
->object
.sha1
, NULL
,
420 REF_NODEREF
, DIE_ON_ERR
);
423 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
);
424 describe_detached_head("HEAD is now at", new->commit
);
427 remove_branch_state();
428 strbuf_release(&msg
);
429 if (!opts
->quiet
&& (new->path
|| !strcmp(new->name
, "HEAD")))
430 report_tracking(new, opts
);
433 static int switch_branches(struct checkout_opts
*opts
, struct branch_info
*new)
436 struct branch_info old
;
437 unsigned char rev
[20];
439 memset(&old
, 0, sizeof(old
));
440 old
.path
= resolve_ref("HEAD", rev
, 0, &flag
);
441 old
.commit
= lookup_commit_reference_gently(rev
, 1);
442 if (!(flag
& REF_ISSYMREF
))
445 if (old
.path
&& !prefixcmp(old
.path
, "refs/heads/"))
446 old
.name
= old
.path
+ strlen("refs/heads/");
450 new->commit
= old
.commit
;
452 die("You are on a branch yet to be born");
453 parse_commit(new->commit
);
457 * If the new thing isn't a branch and isn't HEAD and we're
458 * not starting a new branch, and we want messages, and we
459 * weren't on a branch, and we're moving to a new commit,
460 * describe the old commit.
462 if (!new->path
&& strcmp(new->name
, "HEAD") && !opts
->new_branch
&&
463 !opts
->quiet
&& !old
.path
&& new->commit
!= old
.commit
)
464 describe_detached_head("Previous HEAD position was", old
.commit
);
468 fprintf(stderr
, "warning: You appear to be on a branch yet to be born.\n");
469 fprintf(stderr
, "warning: Forcing checkout of %s.\n", new->name
);
474 ret
= merge_working_tree(opts
, &old
, new);
478 update_refs_for_switch(opts
, &old
, new);
480 return post_checkout_hook(old
.commit
, new->commit
, 1);
483 static int git_checkout_config(const char *var
, const char *value
)
485 return git_default_config(var
, value
);
488 int cmd_checkout(int argc
, const char **argv
, const char *prefix
)
490 struct checkout_opts opts
;
491 unsigned char rev
[20];
493 struct branch_info
new;
494 struct tree
*source_tree
= NULL
;
495 struct option options
[] = {
496 OPT__QUIET(&opts
.quiet
),
497 OPT_STRING('b', NULL
, &opts
.new_branch
, "new branch", "branch"),
498 OPT_BOOLEAN('l', NULL
, &opts
.new_branch_log
, "log for new branch"),
499 OPT_SET_INT( 0 , "track", &opts
.track
, "track",
500 BRANCH_TRACK_EXPLICIT
),
501 OPT_BOOLEAN('f', NULL
, &opts
.force
, "force"),
502 OPT_BOOLEAN('m', NULL
, &opts
.merge
, "merge"),
506 memset(&opts
, 0, sizeof(opts
));
507 memset(&new, 0, sizeof(new));
509 git_config(git_checkout_config
);
511 opts
.track
= git_branch_track
;
513 argc
= parse_options(argc
, argv
, options
, checkout_usage
, 0);
516 if (get_sha1(arg
, rev
))
518 else if ((new.commit
= lookup_commit_reference_gently(rev
, 1))) {
520 setup_branch_path(&new);
521 if (resolve_ref(new.path
, rev
, 1, NULL
))
522 new.commit
= lookup_commit_reference(rev
);
525 parse_commit(new.commit
);
526 source_tree
= new.commit
->tree
;
529 } else if ((source_tree
= parse_tree_indirect(rev
))) {
535 if (argc
&& !strcmp(argv
[0], "--")) {
540 if (!opts
.new_branch
&& (opts
.track
!= git_branch_track
))
541 die("git checkout: --track and --no-track require -b");
543 if (opts
.force
&& opts
.merge
)
544 die("git checkout: -f and -m are incompatible");
547 const char **pathspec
= get_pathspec(prefix
, argv
);
550 die("invalid path specification");
553 if (opts
.new_branch
|| opts
.force
|| opts
.merge
) {
555 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]);
557 die("git checkout: updating paths is incompatible with switching branches/forcing");
562 read_tree_some(source_tree
, pathspec
);
565 return checkout_paths(pathspec
);
568 if (new.name
&& !new.commit
) {
569 die("Cannot switch branch to a non-commit.");
572 return switch_branches(&opts
, &new);