3 #include "parse-options.h"
8 #include "unpack-trees.h"
10 #include "run-command.h"
11 #include "merge-recursive.h"
16 static const char * const checkout_usage
[] = {
17 "git checkout [options] <branch>",
18 "git checkout [options] [<branch>] -- <file>...",
22 static int post_checkout_hook(struct commit
*old
, struct commit
*new,
25 struct child_process proc
;
26 const char *name
= git_path("hooks/post-checkout");
29 if (access(name
, X_OK
) < 0)
32 memset(&proc
, 0, sizeof(proc
));
34 argv
[1] = xstrdup(sha1_to_hex(old
->object
.sha1
));
35 argv
[2] = xstrdup(sha1_to_hex(new->object
.sha1
));
36 argv
[3] = changed
? "1" : "0";
40 proc
.stdout_to_stderr
= 1;
41 return run_command(&proc
);
44 static int update_some(const unsigned char *sha1
, const char *base
, int baselen
,
45 const char *pathname
, unsigned mode
, int stage
)
48 struct cache_entry
*ce
;
50 if (S_ISGITLINK(mode
))
54 return READ_TREE_RECURSIVE
;
56 len
= baselen
+ strlen(pathname
);
57 ce
= xcalloc(1, cache_entry_size(len
));
58 hashcpy(ce
->sha1
, sha1
);
59 memcpy(ce
->name
, base
, baselen
);
60 memcpy(ce
->name
+ baselen
, pathname
, len
- baselen
);
61 ce
->ce_flags
= create_ce_flags(len
, 0);
62 ce
->ce_mode
= create_ce_mode(mode
);
63 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
67 static int read_tree_some(struct tree
*tree
, const char **pathspec
)
70 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
71 newfd
= hold_locked_index(lock_file
, 1);
74 read_tree_recursive(tree
, "", 0, 0, pathspec
, update_some
);
76 if (write_cache(newfd
, active_cache
, active_nr
) ||
77 commit_locked_index(lock_file
))
78 die("unable to write new index file");
80 /* update the index with the given tree's info
81 * for all args, expanding wildcards, and exit
82 * with any non-zero return code.
87 static int checkout_paths(const char **pathspec
)
90 struct checkout state
;
91 static char *ps_matched
;
92 unsigned char rev
[20];
96 for (pos
= 0; pathspec
[pos
]; pos
++)
98 ps_matched
= xcalloc(1, pos
);
100 for (pos
= 0; pos
< active_nr
; pos
++) {
101 struct cache_entry
*ce
= active_cache
[pos
];
102 pathspec_match(pathspec
, ps_matched
, ce
->name
, 0);
105 if (report_path_error(ps_matched
, pathspec
, 0))
108 memset(&state
, 0, sizeof(state
));
110 state
.refresh_cache
= 1;
111 for (pos
= 0; pos
< active_nr
; pos
++) {
112 struct cache_entry
*ce
= active_cache
[pos
];
113 if (pathspec_match(pathspec
, NULL
, ce
->name
, 0)) {
114 checkout_entry(ce
, &state
, NULL
);
118 resolve_ref("HEAD", rev
, 0, &flag
);
119 head
= lookup_commit_reference_gently(rev
, 1);
121 return post_checkout_hook(head
, head
, 0);
124 static void show_local_changes(struct object
*head
)
127 /* I think we want full paths, even if we're in a subdirectory. */
128 init_revisions(&rev
, NULL
);
130 rev
.diffopt
.output_format
|= DIFF_FORMAT_NAME_STATUS
;
131 add_pending_object(&rev
, head
, NULL
);
132 run_diff_index(&rev
, 0);
135 static void describe_detached_head(char *msg
, struct commit
*commit
)
139 parse_commit(commit
);
140 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, &sb
, 0, "", "", 0, 0);
141 fprintf(stderr
, "%s %s... %s\n", msg
,
142 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
), sb
.buf
);
146 static int reset_to_new(struct tree
*tree
, int quiet
)
148 struct unpack_trees_options opts
;
149 struct tree_desc tree_desc
;
150 memset(&opts
, 0, sizeof(opts
));
155 opts
.fn
= oneway_merge
;
156 opts
.verbose_update
= !quiet
;
158 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
159 if (unpack_trees(1, &tree_desc
, &opts
))
164 static void reset_clean_to_new(struct tree
*tree
, int quiet
)
166 struct unpack_trees_options opts
;
167 struct tree_desc tree_desc
;
168 memset(&opts
, 0, sizeof(opts
));
170 opts
.skip_unmerged
= 1;
173 opts
.fn
= oneway_merge
;
174 opts
.verbose_update
= !quiet
;
176 init_tree_desc(&tree_desc
, tree
->buffer
, tree
->size
);
177 if (unpack_trees(1, &tree_desc
, &opts
))
181 struct checkout_opts
{
192 const char *name
; /* The short name used */
193 const char *path
; /* The full name of a real branch */
194 struct commit
*commit
; /* The named commit */
197 static void setup_branch_path(struct branch_info
*branch
)
200 strbuf_init(&buf
, 0);
201 strbuf_addstr(&buf
, "refs/heads/");
202 strbuf_addstr(&buf
, branch
->name
);
203 branch
->path
= strbuf_detach(&buf
, NULL
);
206 static int merge_working_tree(struct checkout_opts
*opts
,
207 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 ret
= opts
->merge
? -1 :
230 error("you need to resolve your current index first");
234 topts
.gently
= opts
->merge
;
235 topts
.fn
= twoway_merge
;
236 topts
.dir
= xcalloc(1, sizeof(*topts
.dir
));
237 topts
.dir
->show_ignored
= 1;
238 topts
.dir
->exclude_per_dir
= ".gitignore";
239 topts
.prefix
= prefix
;
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
);
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.
256 parse_commit(old
->commit
);
258 /* Do more real merge */
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.
271 add_files_to_cache(0, NULL
, NULL
);
272 work
= write_tree_from_memory();
274 ret
= reset_to_new(new->commit
->tree
, opts
->quiet
);
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
);
283 if (write_cache(newfd
, active_cache
, active_nr
) ||
284 commit_locked_index(lock_file
))
285 die("unable to write new index file");
288 show_local_changes(&new->commit
->object
);
293 static void update_refs_for_switch(struct checkout_opts
*opts
,
294 struct branch_info
*old
,
295 struct branch_info
*new)
298 const char *old_desc
;
299 if (opts
->new_branch
) {
300 create_branch(old
->name
, opts
->new_branch
, new->name
, 0,
301 opts
->new_branch_log
, opts
->track
);
302 new->name
= opts
->new_branch
;
303 setup_branch_path(new);
306 strbuf_init(&msg
, 0);
307 old_desc
= old
->name
;
309 old_desc
= sha1_to_hex(old
->commit
->object
.sha1
);
310 strbuf_addf(&msg
, "checkout: moving from %s to %s",
311 old_desc
, new->name
);
314 create_symref("HEAD", new->path
, msg
.buf
);
316 if (old
->path
&& !strcmp(new->path
, old
->path
))
317 fprintf(stderr
, "Already on \"%s\"\n",
320 fprintf(stderr
, "Switched to%s branch \"%s\"\n",
321 opts
->new_branch
? " a new" : "",
324 } else if (strcmp(new->name
, "HEAD")) {
325 update_ref(msg
.buf
, "HEAD", new->commit
->object
.sha1
, NULL
,
326 REF_NODEREF
, DIE_ON_ERR
);
329 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
);
330 describe_detached_head("HEAD is now at", new->commit
);
333 remove_branch_state();
334 strbuf_release(&msg
);
337 static int switch_branches(struct checkout_opts
*opts
,
338 struct branch_info
*new, const char *prefix
)
341 struct branch_info old
;
342 unsigned char rev
[20];
344 memset(&old
, 0, sizeof(old
));
345 old
.path
= xstrdup(resolve_ref("HEAD", rev
, 0, &flag
));
346 old
.commit
= lookup_commit_reference_gently(rev
, 1);
347 if (!(flag
& REF_ISSYMREF
)) {
348 free((char *)old
.path
);
352 if (old
.path
&& !prefixcmp(old
.path
, "refs/heads/"))
353 old
.name
= old
.path
+ strlen("refs/heads/");
357 new->commit
= old
.commit
;
359 die("You are on a branch yet to be born");
360 parse_commit(new->commit
);
364 * If the new thing isn't a branch and isn't HEAD and we're
365 * not starting a new branch, and we want messages, and we
366 * weren't on a branch, and we're moving to a new commit,
367 * describe the old commit.
369 if (!new->path
&& strcmp(new->name
, "HEAD") && !opts
->new_branch
&&
370 !opts
->quiet
&& !old
.path
&& new->commit
!= old
.commit
)
371 describe_detached_head("Previous HEAD position was", old
.commit
);
375 fprintf(stderr
, "warning: You appear to be on a branch yet to be born.\n");
376 fprintf(stderr
, "warning: Forcing checkout of %s.\n", new->name
);
381 ret
= merge_working_tree(opts
, &old
, new, prefix
);
385 update_refs_for_switch(opts
, &old
, new);
386 free((char *)old
.path
);
387 return post_checkout_hook(old
.commit
, new->commit
, 1);
390 static int branch_track
= 0;
392 static int git_checkout_config(const char *var
, const char *value
)
394 if (!strcmp(var
, "branch.autosetupmerge"))
395 branch_track
= git_config_bool(var
, value
);
397 return git_default_config(var
, value
);
400 int cmd_checkout(int argc
, const char **argv
, const char *prefix
)
402 struct checkout_opts opts
;
403 unsigned char rev
[20];
405 struct branch_info
new;
406 struct tree
*source_tree
= NULL
;
407 struct option options
[] = {
408 OPT__QUIET(&opts
.quiet
),
409 OPT_STRING('b', NULL
, &opts
.new_branch
, "new branch", "branch"),
410 OPT_BOOLEAN('l', NULL
, &opts
.new_branch_log
, "log for new branch"),
411 OPT_BOOLEAN( 0 , "track", &opts
.track
, "track"),
412 OPT_BOOLEAN('f', NULL
, &opts
.force
, "force"),
413 OPT_BOOLEAN('m', NULL
, &opts
.merge
, "merge"),
416 memset(&opts
, 0, sizeof(opts
));
417 memset(&new, 0, sizeof(new));
419 git_config(git_checkout_config
);
421 opts
.track
= branch_track
;
423 argc
= parse_options(argc
, argv
, options
, checkout_usage
, 0);
426 if (get_sha1(arg
, rev
))
428 else if ((new.commit
= lookup_commit_reference_gently(rev
, 1))) {
430 setup_branch_path(&new);
431 if (resolve_ref(new.path
, rev
, 1, NULL
))
432 new.commit
= lookup_commit_reference(rev
);
435 parse_commit(new.commit
);
436 source_tree
= new.commit
->tree
;
439 } else if ((source_tree
= parse_tree_indirect(rev
))) {
445 if (argc
&& !strcmp(argv
[0], "--")) {
450 if (!opts
.new_branch
&& (opts
.track
!= branch_track
))
451 die("git checkout: --track and --no-track require -b");
453 if (opts
.force
&& opts
.merge
)
454 die("git checkout: -f and -m are incompatible");
457 const char **pathspec
= get_pathspec(prefix
, argv
);
459 if (opts
.new_branch
|| opts
.force
|| opts
.merge
) {
461 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]);
463 die("git checkout: updating paths is incompatible with switching branches/forcing");
468 read_tree_some(source_tree
, pathspec
);
471 return checkout_paths(pathspec
);
474 if (new.name
&& !new.commit
) {
475 die("Cannot switch branch to a non-commit.");
478 return switch_branches(&opts
, &new, prefix
);