]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-checkout.c
Merge branch 'maint' into ph/checkout
[thirdparty/git.git] / builtin-checkout.c
CommitLineData
782c2d65
DB
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"
79a1e6b4 15#include "remote.h"
782c2d65
DB
16
17static const char * const checkout_usage[] = {
18 "git checkout [options] <branch>",
19 "git checkout [options] [<branch>] -- <file>...",
20 NULL,
21};
22
23static int post_checkout_hook(struct commit *old, struct commit *new,
24 int changed)
25{
26 struct child_process proc;
27 const char *name = git_path("hooks/post-checkout");
28 const char *argv[5];
29
30 if (access(name, X_OK) < 0)
31 return 0;
32
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);
43}
44
45static int update_some(const unsigned char *sha1, const char *base, int baselen,
671f0707 46 const char *pathname, unsigned mode, int stage, void *context)
782c2d65
DB
47{
48 int len;
49 struct cache_entry *ce;
50
51 if (S_ISGITLINK(mode))
52 return 0;
53
54 if (S_ISDIR(mode))
55 return READ_TREE_RECURSIVE;
56
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;
66}
67
68static int read_tree_some(struct tree *tree, const char **pathspec)
69{
671f0707 70 read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
782c2d65 71
782c2d65
DB
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.
75 */
76 return 0;
77}
78
75336878 79static int checkout_paths(struct tree *source_tree, const char **pathspec)
782c2d65
DB
80{
81 int pos;
82 struct checkout state;
83 static char *ps_matched;
84 unsigned char rev[20];
85 int flag;
86 struct commit *head;
d2b3691b 87 int errs = 0;
782c2d65 88
75336878
DB
89 int newfd;
90 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
91
92 newfd = hold_locked_index(lock_file, 1);
93 read_cache();
94
95 if (source_tree)
96 read_tree_some(source_tree, pathspec);
97
782c2d65
DB
98 for (pos = 0; pathspec[pos]; pos++)
99 ;
100 ps_matched = xcalloc(1, pos);
101
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);
105 }
106
107 if (report_path_error(ps_matched, pathspec, 0))
108 return 1;
109
d2b3691b 110 /* Now we are committed to check them out */
782c2d65
DB
111 memset(&state, 0, sizeof(state));
112 state.force = 1;
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)) {
d2b3691b 117 errs |= checkout_entry(ce, &state, NULL);
782c2d65
DB
118 }
119 }
120
75336878
DB
121 if (write_cache(newfd, active_cache, active_nr) ||
122 commit_locked_index(lock_file))
123 die("unable to write new index file");
124
782c2d65
DB
125 resolve_ref("HEAD", rev, 0, &flag);
126 head = lookup_commit_reference_gently(rev, 1);
127
d2b3691b
JH
128 errs |= post_checkout_hook(head, head, 0);
129 return errs;
782c2d65
DB
130}
131
132static void show_local_changes(struct object *head)
133{
134 struct rev_info rev;
135 /* I think we want full paths, even if we're in a subdirectory. */
136 init_revisions(&rev, NULL);
137 rev.abbrev = 0;
138 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;
139 add_pending_object(&rev, head, NULL);
140 run_diff_index(&rev, 0);
141}
142
143static void describe_detached_head(char *msg, struct commit *commit)
144{
145 struct strbuf sb;
146 strbuf_init(&sb, 0);
147 parse_commit(commit);
27886318 148 pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, 0, NULL, NULL, 0, 0);
782c2d65
DB
149 fprintf(stderr, "%s %s... %s\n", msg,
150 find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
151 strbuf_release(&sb);
152}
153
6286a08d
JH
154struct checkout_opts {
155 int quiet;
156 int merge;
157 int force;
291d823e 158 int writeout_error;
bc052d7f 159
6286a08d
JH
160 char *new_branch;
161 int new_branch_log;
162 enum branch_track track;
163};
782c2d65 164
6286a08d 165static int reset_tree(struct tree *tree, struct checkout_opts *o, int worktree)
782c2d65
DB
166{
167 struct unpack_trees_options opts;
168 struct tree_desc tree_desc;
bc052d7f 169
782c2d65
DB
170 memset(&opts, 0, sizeof(opts));
171 opts.head_idx = -1;
6286a08d
JH
172 opts.update = worktree;
173 opts.skip_unmerged = !worktree;
782c2d65
DB
174 opts.reset = 1;
175 opts.merge = 1;
176 opts.fn = oneway_merge;
6286a08d 177 opts.verbose_update = !o->quiet;
34110cd4
LT
178 opts.src_index = &the_index;
179 opts.dst_index = &the_index;
782c2d65
DB
180 parse_tree(tree);
181 init_tree_desc(&tree_desc, tree->buffer, tree->size);
291d823e
JH
182 switch (unpack_trees(1, &tree_desc, &opts)) {
183 case -2:
184 o->writeout_error = 1;
185 /*
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
189 * them.
190 */
191 case 0:
192 return 0;
193 default:
84a5750b 194 return 128;
291d823e 195 }
782c2d65
DB
196}
197
782c2d65
DB
198struct branch_info {
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 */
202};
203
204static void setup_branch_path(struct branch_info *branch)
205{
206 struct strbuf buf;
207 strbuf_init(&buf, 0);
208 strbuf_addstr(&buf, "refs/heads/");
209 strbuf_addstr(&buf, branch->name);
210 branch->path = strbuf_detach(&buf, NULL);
211}
212
213static int merge_working_tree(struct checkout_opts *opts,
75ea38df 214 struct branch_info *old, struct branch_info *new)
782c2d65
DB
215{
216 int ret;
217 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
218 int newfd = hold_locked_index(lock_file, 1);
219 read_cache();
220
221 if (opts->force) {
6286a08d 222 ret = reset_tree(new->commit->tree, opts, 1);
782c2d65
DB
223 if (ret)
224 return ret;
225 } else {
226 struct tree_desc trees[2];
227 struct tree *tree;
228 struct unpack_trees_options topts;
bc052d7f 229
782c2d65
DB
230 memset(&topts, 0, sizeof(topts));
231 topts.head_idx = -1;
34110cd4
LT
232 topts.src_index = &the_index;
233 topts.dst_index = &the_index;
782c2d65 234
8ccba008
JH
235 topts.msgs.not_uptodate_file = "You have local changes to '%s'; cannot switch branches.";
236
782c2d65
DB
237 refresh_cache(REFRESH_QUIET);
238
239 if (unmerged_cache()) {
04c9e11f
JH
240 error("you need to resolve your current index first");
241 return 1;
782c2d65 242 }
04c9e11f
JH
243
244 /* 2-way merge to the new branch */
245 topts.update = 1;
246 topts.merge = 1;
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);
257
291d823e
JH
258 ret = unpack_trees(2, trees, &topts);
259 if (ret == -1) {
782c2d65
DB
260 /*
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.
264 */
265 struct tree *result;
266 struct tree *work;
267 if (!opts->merge)
268 return 1;
269 parse_commit(old->commit);
270
271 /* Do more real merge */
272
273 /*
274 * We update the index fully, then write the
275 * tree from the index, then merge the new
276 * branch with the current tree, with the old
277 * branch as the base. Then we reset the index
278 * (but not the working tree) to the new
279 * branch, leaving the working tree as the
280 * merged version, but skipping unmerged
281 * entries in the index.
282 */
283
7ae02a30 284 add_files_to_cache(NULL, NULL, 0);
782c2d65
DB
285 work = write_tree_from_memory();
286
6286a08d 287 ret = reset_tree(new->commit->tree, opts, 1);
782c2d65
DB
288 if (ret)
289 return ret;
290 merge_trees(new->commit->tree, work, old->commit->tree,
291 new->name, "local", &result);
6286a08d 292 ret = reset_tree(new->commit->tree, opts, 0);
84a5750b
JH
293 if (ret)
294 return ret;
782c2d65
DB
295 }
296 }
297
298 if (write_cache(newfd, active_cache, active_nr) ||
299 commit_locked_index(lock_file))
300 die("unable to write new index file");
301
302 if (!opts->force)
303 show_local_changes(&new->commit->object);
304
305 return 0;
306}
307
6d21bf96 308static void report_tracking(struct branch_info *new)
79a1e6b4 309{
6d21bf96 310 struct strbuf sb = STRBUF_INIT;
b56fca07 311 struct branch *branch = branch_get(new->name);
79a1e6b4 312
6d21bf96 313 if (!format_tracking_info(branch, &sb))
79a1e6b4 314 return;
6d21bf96
JH
315 fputs(sb.buf, stdout);
316 strbuf_release(&sb);
b0030db3 317}
79a1e6b4 318
782c2d65
DB
319static void update_refs_for_switch(struct checkout_opts *opts,
320 struct branch_info *old,
321 struct branch_info *new)
322{
323 struct strbuf msg;
324 const char *old_desc;
325 if (opts->new_branch) {
326 create_branch(old->name, opts->new_branch, new->name, 0,
327 opts->new_branch_log, opts->track);
328 new->name = opts->new_branch;
329 setup_branch_path(new);
330 }
331
332 strbuf_init(&msg, 0);
333 old_desc = old->name;
334 if (!old_desc)
335 old_desc = sha1_to_hex(old->commit->object.sha1);
336 strbuf_addf(&msg, "checkout: moving from %s to %s",
337 old_desc, new->name);
338
339 if (new->path) {
340 create_symref("HEAD", new->path, msg.buf);
341 if (!opts->quiet) {
342 if (old->path && !strcmp(new->path, old->path))
343 fprintf(stderr, "Already on \"%s\"\n",
344 new->name);
345 else
346 fprintf(stderr, "Switched to%s branch \"%s\"\n",
347 opts->new_branch ? " a new" : "",
348 new->name);
349 }
350 } else if (strcmp(new->name, "HEAD")) {
351 update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
352 REF_NODEREF, DIE_ON_ERR);
353 if (!opts->quiet) {
354 if (old->path)
355 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);
356 describe_detached_head("HEAD is now at", new->commit);
357 }
358 }
359 remove_branch_state();
360 strbuf_release(&msg);
b0030db3 361 if (!opts->quiet && (new->path || !strcmp(new->name, "HEAD")))
6d21bf96 362 report_tracking(new);
782c2d65
DB
363}
364
75ea38df 365static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
782c2d65
DB
366{
367 int ret = 0;
368 struct branch_info old;
369 unsigned char rev[20];
370 int flag;
371 memset(&old, 0, sizeof(old));
372 old.path = resolve_ref("HEAD", rev, 0, &flag);
373 old.commit = lookup_commit_reference_gently(rev, 1);
374 if (!(flag & REF_ISSYMREF))
375 old.path = NULL;
376
377 if (old.path && !prefixcmp(old.path, "refs/heads/"))
378 old.name = old.path + strlen("refs/heads/");
379
380 if (!new->name) {
381 new->name = "HEAD";
382 new->commit = old.commit;
383 if (!new->commit)
384 die("You are on a branch yet to be born");
385 parse_commit(new->commit);
386 }
387
388 /*
389 * If the new thing isn't a branch and isn't HEAD and we're
390 * not starting a new branch, and we want messages, and we
391 * weren't on a branch, and we're moving to a new commit,
392 * describe the old commit.
393 */
394 if (!new->path && strcmp(new->name, "HEAD") && !opts->new_branch &&
395 !opts->quiet && !old.path && new->commit != old.commit)
396 describe_detached_head("Previous HEAD position was", old.commit);
397
398 if (!old.commit) {
399 if (!opts->quiet) {
400 fprintf(stderr, "warning: You appear to be on a branch yet to be born.\n");
401 fprintf(stderr, "warning: Forcing checkout of %s.\n", new->name);
402 }
403 opts->force = 1;
404 }
405
75ea38df 406 ret = merge_working_tree(opts, &old, new);
782c2d65
DB
407 if (ret)
408 return ret;
409
410 update_refs_for_switch(opts, &old, new);
411
291d823e
JH
412 ret = post_checkout_hook(old.commit, new->commit, 1);
413 return ret || opts->writeout_error;
782c2d65
DB
414}
415
782c2d65
DB
416int cmd_checkout(int argc, const char **argv, const char *prefix)
417{
418 struct checkout_opts opts;
419 unsigned char rev[20];
420 const char *arg;
421 struct branch_info new;
422 struct tree *source_tree = NULL;
423 struct option options[] = {
424 OPT__QUIET(&opts.quiet),
425 OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
426 OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
498a6e7e 427 OPT_SET_INT('t', "track", &opts.track, "track",
9ed36cfa 428 BRANCH_TRACK_EXPLICIT),
782c2d65
DB
429 OPT_BOOLEAN('f', NULL, &opts.force, "force"),
430 OPT_BOOLEAN('m', NULL, &opts.merge, "merge"),
b249b552 431 OPT_END(),
782c2d65
DB
432 };
433
434 memset(&opts, 0, sizeof(opts));
435 memset(&new, 0, sizeof(new));
436
ef90d6d4 437 git_config(git_default_config, NULL);
782c2d65 438
9ed36cfa 439 opts.track = git_branch_track;
782c2d65 440
f5242ebf
PH
441 argc = parse_options(argc, argv, options, checkout_usage,
442 PARSE_OPT_KEEP_DASHDASH);
782c2d65
DB
443 if (argc) {
444 arg = argv[0];
445 if (get_sha1(arg, rev))
446 ;
447 else if ((new.commit = lookup_commit_reference_gently(rev, 1))) {
448 new.name = arg;
449 setup_branch_path(&new);
450 if (resolve_ref(new.path, rev, 1, NULL))
451 new.commit = lookup_commit_reference(rev);
452 else
453 new.path = NULL;
454 parse_commit(new.commit);
455 source_tree = new.commit->tree;
456 argv++;
457 argc--;
458 } else if ((source_tree = parse_tree_indirect(rev))) {
459 argv++;
460 argc--;
461 }
462 }
463
464 if (argc && !strcmp(argv[0], "--")) {
465 argv++;
466 argc--;
467 }
468
9ed36cfa 469 if (!opts.new_branch && (opts.track != git_branch_track))
782c2d65
DB
470 die("git checkout: --track and --no-track require -b");
471
472 if (opts.force && opts.merge)
473 die("git checkout: -f and -m are incompatible");
474
475 if (argc) {
476 const char **pathspec = get_pathspec(prefix, argv);
301e42ed
AR
477
478 if (!pathspec)
479 die("invalid path specification");
480
782c2d65
DB
481 /* Checkout paths */
482 if (opts.new_branch || opts.force || opts.merge) {
483 if (argc == 1) {
484 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]);
485 } else {
486 die("git checkout: updating paths is incompatible with switching branches/forcing");
487 }
488 }
489
75336878 490 return checkout_paths(source_tree, pathspec);
782c2d65
DB
491 }
492
493 if (new.name && !new.commit) {
494 die("Cannot switch branch to a non-commit.");
495 }
496
75ea38df 497 return switch_branches(&opts, &new);
782c2d65 498}