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
, void *context
)
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
, NULL
);
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];
90 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
92 newfd
= hold_locked_index(lock_file
, 1);
96 read_tree_some(source_tree
, pathspec
);
98 for (pos
= 0; pathspec
[pos
]; pos
++)
100 ps_matched
= xcalloc(1, pos
);
102 for (pos
= 0; pos
< active_nr
; pos
++) {
103 struct cache_entry
*ce
= active_cache
[pos
];
104 pathspec_match(pathspec
, ps_matched
, ce
->name
, 0);
107 if (report_path_error(ps_matched
, pathspec
, 0))
110 /* Now we are committed to check them out */
111 memset(&state
, 0, sizeof(state
));
113 state
.refresh_cache
= 1;
114 for (pos
= 0; pos
< active_nr
; pos
++) {
115 struct cache_entry
*ce
= active_cache
[pos
];
116 if (pathspec_match(pathspec
, NULL
, ce
->name
, 0)) {
117 errs
|= checkout_entry(ce
, &state
, NULL
);
121 if (write_cache(newfd
, active_cache
, active_nr
) ||
122 commit_locked_index(lock_file
))
123 die("unable to write new index file");
125 resolve_ref("HEAD", rev
, 0, &flag
);
126 head
= lookup_commit_reference_gently(rev
, 1);
128 errs
|= post_checkout_hook(head
, head
, 0);
132 static void show_local_changes(struct object
*head
)
135 /* I think we want full paths, even if we're in a subdirectory. */
136 init_revisions(&rev
, NULL
);
138 rev
.diffopt
.output_format
|= DIFF_FORMAT_NAME_STATUS
;
139 add_pending_object(&rev
, head
, NULL
);
140 run_diff_index(&rev
, 0);
143 static void describe_detached_head(char *msg
, struct commit
*commit
)
147 parse_commit(commit
);
148 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, &sb
, 0, NULL
, NULL
, 0, 0);
149 fprintf(stderr
, "%s %s... %s\n", msg
,
150 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
), sb
.buf
);
154 struct checkout_opts
{
160 const char *new_branch
;
162 enum branch_track track
;
165 static int reset_tree(struct tree
*tree
, struct checkout_opts
*o
, int worktree
)
167 struct unpack_trees_options opts
;
168 struct tree_desc tree_desc
;
170 memset(&opts
, 0, sizeof(opts
));
172 opts
.update
= worktree
;
173 opts
.skip_unmerged
= !worktree
;
176 opts
.fn
= oneway_merge
;
177 opts
.verbose_update
= !o
->quiet
;
178 opts
.src_index
= &the_index
;
179 opts
.dst_index
= &the_index
;
181 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
182 switch (unpack_trees(1, &tree_desc
, &opts
)) {
184 o
->writeout_error
= 1;
186 * We return 0 nevertheless, as the index is all right
187 * and more importantly we have made best efforts to
188 * update paths in the work tree, and we cannot revert
199 const char *name
; /* The short name used */
200 const char *path
; /* The full name of a real branch */
201 struct commit
*commit
; /* The named commit */
204 static void setup_branch_path(struct branch_info
*branch
)
207 strbuf_init(&buf
, 0);
208 strbuf_addstr(&buf
, "refs/heads/");
209 strbuf_addstr(&buf
, branch
->name
);
210 branch
->path
= strbuf_detach(&buf
, NULL
);
213 static int merge_working_tree(struct checkout_opts
*opts
,
214 struct branch_info
*old
, struct branch_info
*new)
217 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
218 int newfd
= hold_locked_index(lock_file
, 1);
222 ret
= reset_tree(new->commit
->tree
, opts
, 1);
226 struct tree_desc trees
[2];
228 struct unpack_trees_options topts
;
230 memset(&topts
, 0, sizeof(topts
));
232 topts
.src_index
= &the_index
;
233 topts
.dst_index
= &the_index
;
235 topts
.msgs
.not_uptodate_file
= "You have local changes to '%s'; cannot switch branches.";
237 refresh_cache(REFRESH_QUIET
);
239 if (unmerged_cache()) {
240 error("you need to resolve your current index first");
244 /* 2-way merge to the new branch */
247 topts
.gently
= opts
->merge
;
248 topts
.verbose_update
= !opts
->quiet
;
249 topts
.fn
= twoway_merge
;
250 topts
.dir
= xcalloc(1, sizeof(*topts
.dir
));
251 topts
.dir
->show_ignored
= 1;
252 topts
.dir
->exclude_per_dir
= ".gitignore";
253 tree
= parse_tree_indirect(old
->commit
->object
.sha1
);
254 init_tree_desc(&trees
[0], tree
->buffer
, tree
->size
);
255 tree
= parse_tree_indirect(new->commit
->object
.sha1
);
256 init_tree_desc(&trees
[1], tree
->buffer
, tree
->size
);
258 ret
= unpack_trees(2, trees
, &topts
);
261 * Unpack couldn't do a trivial merge; either
262 * give up or do a real merge, depending on
263 * whether the merge flag was used.
267 struct merge_options o
;
270 parse_commit(old
->commit
);
272 /* Do more real merge */
275 * We update the index fully, then write the
276 * tree from the index, then merge the new
277 * branch with the current tree, with the old
278 * branch as the base. Then we reset the index
279 * (but not the working tree) to the new
280 * branch, leaving the working tree as the
281 * merged version, but skipping unmerged
282 * entries in the index.
285 add_files_to_cache(NULL
, NULL
, 0);
286 init_merge_options(&o
);
288 work
= write_tree_from_memory(&o
);
290 ret
= reset_tree(new->commit
->tree
, opts
, 1);
293 o
.branch1
= new->name
;
295 merge_trees(&o
, new->commit
->tree
, work
,
296 old
->commit
->tree
, &result
);
297 ret
= reset_tree(new->commit
->tree
, opts
, 0);
303 if (write_cache(newfd
, active_cache
, active_nr
) ||
304 commit_locked_index(lock_file
))
305 die("unable to write new index file");
308 show_local_changes(&new->commit
->object
);
313 static void report_tracking(struct branch_info
*new)
315 struct strbuf sb
= STRBUF_INIT
;
316 struct branch
*branch
= branch_get(new->name
);
318 if (!format_tracking_info(branch
, &sb
))
320 fputs(sb
.buf
, stdout
);
324 static void update_refs_for_switch(struct checkout_opts
*opts
,
325 struct branch_info
*old
,
326 struct branch_info
*new)
329 const char *old_desc
;
330 if (opts
->new_branch
) {
331 create_branch(old
->name
, opts
->new_branch
, new->name
, 0,
332 opts
->new_branch_log
, opts
->track
);
333 new->name
= opts
->new_branch
;
334 setup_branch_path(new);
337 strbuf_init(&msg
, 0);
338 old_desc
= old
->name
;
340 old_desc
= sha1_to_hex(old
->commit
->object
.sha1
);
341 strbuf_addf(&msg
, "checkout: moving from %s to %s",
342 old_desc
, new->name
);
345 create_symref("HEAD", new->path
, msg
.buf
);
347 if (old
->path
&& !strcmp(new->path
, old
->path
))
348 fprintf(stderr
, "Already on \"%s\"\n",
351 fprintf(stderr
, "Switched to%s branch \"%s\"\n",
352 opts
->new_branch
? " a new" : "",
355 } else if (strcmp(new->name
, "HEAD")) {
356 update_ref(msg
.buf
, "HEAD", new->commit
->object
.sha1
, NULL
,
357 REF_NODEREF
, DIE_ON_ERR
);
360 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
);
361 describe_detached_head("HEAD is now at", new->commit
);
364 remove_branch_state();
365 strbuf_release(&msg
);
366 if (!opts
->quiet
&& (new->path
|| !strcmp(new->name
, "HEAD")))
367 report_tracking(new);
370 static int switch_branches(struct checkout_opts
*opts
, struct branch_info
*new)
373 struct branch_info old
;
374 unsigned char rev
[20];
376 memset(&old
, 0, sizeof(old
));
377 old
.path
= resolve_ref("HEAD", rev
, 0, &flag
);
378 old
.commit
= lookup_commit_reference_gently(rev
, 1);
379 if (!(flag
& REF_ISSYMREF
))
382 if (old
.path
&& !prefixcmp(old
.path
, "refs/heads/"))
383 old
.name
= old
.path
+ strlen("refs/heads/");
387 new->commit
= old
.commit
;
389 die("You are on a branch yet to be born");
390 parse_commit(new->commit
);
394 * If the new thing isn't a branch and isn't HEAD and we're
395 * not starting a new branch, and we want messages, and we
396 * weren't on a branch, and we're moving to a new commit,
397 * describe the old commit.
399 if (!new->path
&& strcmp(new->name
, "HEAD") && !opts
->new_branch
&&
400 !opts
->quiet
&& !old
.path
&& new->commit
!= old
.commit
)
401 describe_detached_head("Previous HEAD position was", old
.commit
);
405 fprintf(stderr
, "warning: You appear to be on a branch yet to be born.\n");
406 fprintf(stderr
, "warning: Forcing checkout of %s.\n", new->name
);
411 ret
= merge_working_tree(opts
, &old
, new);
415 update_refs_for_switch(opts
, &old
, new);
417 ret
= post_checkout_hook(old
.commit
, new->commit
, 1);
418 return ret
|| opts
->writeout_error
;
421 int cmd_checkout(int argc
, const char **argv
, const char *prefix
)
423 struct checkout_opts opts
;
424 unsigned char rev
[20];
426 struct branch_info
new;
427 struct tree
*source_tree
= NULL
;
428 struct option options
[] = {
429 OPT__QUIET(&opts
.quiet
),
430 OPT_STRING('b', NULL
, &opts
.new_branch
, "new branch", "branch"),
431 OPT_BOOLEAN('l', NULL
, &opts
.new_branch_log
, "log for new branch"),
432 OPT_SET_INT('t', "track", &opts
.track
, "track",
433 BRANCH_TRACK_EXPLICIT
),
434 OPT_BOOLEAN('f', NULL
, &opts
.force
, "force"),
435 OPT_BOOLEAN('m', NULL
, &opts
.merge
, "merge"),
440 memset(&opts
, 0, sizeof(opts
));
441 memset(&new, 0, sizeof(new));
443 git_config(git_default_config
, NULL
);
445 opts
.track
= BRANCH_TRACK_UNSPECIFIED
;
447 argc
= parse_options(argc
, argv
, options
, checkout_usage
,
448 PARSE_OPT_KEEP_DASHDASH
);
450 /* --track without -b should DWIM */
451 if (0 < opts
.track
&& !opts
.new_branch
) {
452 const char *argv0
= argv
[0];
453 if (!argc
|| !strcmp(argv0
, "--"))
454 die ("--track needs a branch name");
455 if (!prefixcmp(argv0
, "refs/"))
457 if (!prefixcmp(argv0
, "remotes/"))
459 argv0
= strchr(argv0
, '/');
460 if (!argv0
|| !argv0
[1])
461 die ("Missing branch name; try -b");
462 opts
.new_branch
= argv0
+ 1;
465 if (opts
.track
== BRANCH_TRACK_UNSPECIFIED
)
466 opts
.track
= git_branch_track
;
468 if (opts
.force
&& opts
.merge
)
469 die("git checkout: -f and -m are incompatible");
472 * case 1: git checkout <ref> -- [<paths>]
474 * <ref> must be a valid tree, everything after the '--' must be
477 * case 2: git checkout -- [<paths>]
479 * everything after the '--' must be paths.
481 * case 3: git checkout <something> [<paths>]
483 * With no paths, if <something> is a commit, that is to
484 * switch to the branch or detach HEAD at it.
486 * Otherwise <something> shall not be ambiguous.
487 * - If it's *only* a reference, treat it like case (1).
488 * - If it's only a path, treat it like case (2).
493 if (!strcmp(argv
[0], "--")) { /* case (2) */
500 has_dash_dash
= (argc
> 1) && !strcmp(argv
[1], "--");
502 if (get_sha1(arg
, rev
)) {
503 if (has_dash_dash
) /* case (1) */
504 die("invalid reference: %s", arg
);
505 goto no_reference
; /* case (3 -> 2) */
508 /* we can't end up being in (2) anymore, eat the argument */
512 if ((new.commit
= lookup_commit_reference_gently(rev
, 1))) {
514 setup_branch_path(&new);
515 if (resolve_ref(new.path
, rev
, 1, NULL
))
516 new.commit
= lookup_commit_reference(rev
);
519 parse_commit(new.commit
);
520 source_tree
= new.commit
->tree
;
522 source_tree
= parse_tree_indirect(rev
);
524 if (!source_tree
) /* case (1): want a tree */
525 die("reference is not a tree: %s", arg
);
526 if (!has_dash_dash
) {/* case (3 -> 1) */
528 * Do not complain the most common case
529 * git checkout branch
530 * even if there happen to be a file called 'branch';
531 * it would be extremely annoying.
534 verify_non_filename(NULL
, arg
);
544 const char **pathspec
= get_pathspec(prefix
, argv
);
547 die("invalid path specification");
550 if (opts
.new_branch
|| opts
.force
|| opts
.merge
) {
552 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]);
554 die("git checkout: updating paths is incompatible with switching branches/forcing");
558 return checkout_paths(source_tree
, pathspec
);
561 if (new.name
&& !new.commit
) {
562 die("Cannot switch branch to a non-commit.");
565 return switch_branches(&opts
, &new);