]> git.ipfire.org Git - thirdparty/git.git/blame - branch.c
Merge branch 'js/use-builtin-add-i'
[thirdparty/git.git] / branch.c
CommitLineData
303d1d0b 1#include "git-compat-util.h"
e496c003 2#include "cache.h"
b2141fc1 3#include "config.h"
e496c003
DB
4#include "branch.h"
5#include "refs.h"
ec0cb496 6#include "refspec.h"
e496c003 7#include "remote.h"
b07d9bfd 8#include "sequencer.h"
e496c003 9#include "commit.h"
ac6c561b 10#include "worktree.h"
961b130d
GC
11#include "submodule-config.h"
12#include "run-command.h"
e496c003
DB
13
14struct tracking {
0ad4a5ff 15 struct refspec_item spec;
d3115660 16 struct string_list *srcs;
e496c003
DB
17 const char *remote;
18 int matches;
19};
20
e4921d87
TK
21struct find_tracked_branch_cb {
22 struct tracking *tracking;
23 struct string_list ambiguous_remotes;
24};
25
e496c003
DB
26static int find_tracked_branch(struct remote *remote, void *priv)
27{
e4921d87
TK
28 struct find_tracked_branch_cb *ftb = priv;
29 struct tracking *tracking = ftb->tracking;
e496c003
DB
30
31 if (!remote_find_tracking(remote, &tracking->spec)) {
e4921d87
TK
32 switch (++tracking->matches) {
33 case 1:
d3115660 34 string_list_append(tracking->srcs, tracking->spec.src);
e496c003 35 tracking->remote = remote->name;
e4921d87
TK
36 break;
37 case 2:
38 /* there are at least two remotes; backfill the first one */
39 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
40 /* fall through */
41 default:
42 string_list_append(&ftb->ambiguous_remotes, remote->name);
e496c003 43 free(tracking->spec.src);
d3115660 44 string_list_clear(tracking->srcs, 0);
e4921d87 45 break;
e496c003 46 }
bdaf1dfa 47 /* remote_find_tracking() searches by src if present */
e496c003
DB
48 tracking->spec.src = NULL;
49 }
e496c003
DB
50 return 0;
51}
52
a9f2c136 53static int should_setup_rebase(const char *origin)
c998ae9b
DS
54{
55 switch (autorebase) {
56 case AUTOREBASE_NEVER:
57 return 0;
58 case AUTOREBASE_LOCAL:
a9f2c136 59 return origin == NULL;
c998ae9b 60 case AUTOREBASE_REMOTE:
a9f2c136 61 return origin != NULL;
c998ae9b
DS
62 case AUTOREBASE_ALWAYS:
63 return 1;
64 }
65 return 0;
66}
67
a3f40ec4
JS
68/**
69 * Install upstream tracking configuration for a branch; specifically, add
70 * `branch.<name>.remote` and `branch.<name>.merge` entries.
71 *
72 * `flag` contains integer flags for options; currently only
73 * BRANCH_CONFIG_VERBOSE is checked.
74 *
75 * `local` is the name of the branch whose configuration we're installing.
76 *
77 * `origin` is the name of the remote owning the upstream branches. NULL means
78 * the upstream branches are local to this repo.
79 *
80 * `remotes` is a list of refs that are upstream of local
81 */
82static int install_branch_config_multiple_remotes(int flag, const char *local,
83 const char *origin, struct string_list *remotes)
a9f2c136 84{
cf4fff57 85 const char *shortname = NULL;
a9f2c136 86 struct strbuf key = STRBUF_INIT;
a3f40ec4 87 struct string_list_item *item;
a9f2c136
JH
88 int rebasing = should_setup_rebase(origin);
89
a3f40ec4
JS
90 if (!remotes->nr)
91 BUG("must provide at least one remote for branch config");
92 if (rebasing && remotes->nr > 1)
93 die(_("cannot inherit upstream tracking configuration of "
94 "multiple refs when rebasing is requested"));
95
96 /*
97 * If the new branch is trying to track itself, something has gone
98 * wrong. Warn the user and don't proceed any further.
99 */
100 if (!origin)
101 for_each_string_list_item(item, remotes)
102 if (skip_prefix(item->string, "refs/heads/", &shortname)
103 && !strcmp(local, shortname)) {
0669bdf4 104 warning(_("not setting branch '%s' as its own upstream"),
a3f40ec4
JS
105 local);
106 return 0;
107 }
85e2233f 108
a9f2c136 109 strbuf_addf(&key, "branch.%s.remote", local);
30598ad0 110 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
27852b2c 111 goto out_err;
a9f2c136
JH
112
113 strbuf_reset(&key);
114 strbuf_addf(&key, "branch.%s.merge", local);
a3f40ec4
JS
115 /*
116 * We want to overwrite any existing config with all the branches in
117 * "remotes". Override any existing config, then write our branches. If
118 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
119 * we've written so far.
120 */
121 if (git_config_set_gently(key.buf, NULL) < 0)
27852b2c 122 goto out_err;
a3f40ec4
JS
123 for_each_string_list_item(item, remotes)
124 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
125 goto out_err;
a9f2c136
JH
126
127 if (rebasing) {
128 strbuf_reset(&key);
129 strbuf_addf(&key, "branch.%s.rebase", local);
30598ad0 130 if (git_config_set_gently(key.buf, "true") < 0)
27852b2c 131 goto out_err;
a9f2c136 132 }
d53a3503 133 strbuf_release(&key);
a9f2c136 134
72f60083 135 if (flag & BRANCH_CONFIG_VERBOSE) {
a3f40ec4
JS
136 struct strbuf tmp_ref_name = STRBUF_INIT;
137 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
138
139 for_each_string_list_item(item, remotes) {
140 shortname = item->string;
141 skip_prefix(shortname, "refs/heads/", &shortname);
142 if (origin) {
143 strbuf_addf(&tmp_ref_name, "%s/%s",
144 origin, shortname);
145 string_list_append_nodup(
146 &friendly_ref_names,
147 strbuf_detach(&tmp_ref_name, NULL));
148 } else {
149 string_list_append(
150 &friendly_ref_names, shortname);
151 }
152 }
153
154 if (remotes->nr == 1) {
155 /*
156 * Rebasing is only allowed in the case of a single
157 * upstream branch.
158 */
159 printf_ln(rebasing ?
160 _("branch '%s' set up to track '%s' by rebasing.") :
161 _("branch '%s' set up to track '%s'."),
162 local, friendly_ref_names.items[0].string);
9fe0cf3a 163 } else {
a3f40ec4
JS
164 printf_ln(_("branch '%s' set up to track:"), local);
165 for_each_string_list_item(item, &friendly_ref_names)
166 printf_ln(" %s", item->string);
9fe0cf3a 167 }
a3f40ec4
JS
168
169 string_list_clear(&friendly_ref_names, 0);
72f60083 170 }
27852b2c
PS
171
172 return 0;
173
174out_err:
175 strbuf_release(&key);
7435e7e2 176 error(_("unable to write upstream branch configuration"));
27852b2c 177
a3f40ec4
JS
178 advise(_("\nAfter fixing the error cause you may try to fix up\n"
179 "the remote tracking information by invoking:"));
180 if (remotes->nr == 1)
181 advise(" git branch --set-upstream-to=%s%s%s",
182 origin ? origin : "",
183 origin ? "/" : "",
184 remotes->items[0].string);
185 else {
186 advise(" git config --add branch.\"%s\".remote %s",
187 local, origin ? origin : ".");
188 for_each_string_list_item(item, remotes)
189 advise(" git config --add branch.\"%s\".merge %s",
190 local, item->string);
191 }
27852b2c
PS
192
193 return -1;
a9f2c136
JH
194}
195
a3f40ec4
JS
196int install_branch_config(int flag, const char *local, const char *origin,
197 const char *remote)
198{
199 int ret;
200 struct string_list remotes = STRING_LIST_INIT_DUP;
201
202 string_list_append(&remotes, remote);
203 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
204 string_list_clear(&remotes, 0);
205 return ret;
206}
207
d3115660
JS
208static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
209{
210 const char *bare_ref;
211 struct branch *branch;
212 int i;
213
214 bare_ref = orig_ref;
215 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
216
217 branch = branch_get(bare_ref);
218 if (!branch->remote_name) {
219 warning(_("asked to inherit tracking from '%s', but no remote is set"),
220 bare_ref);
221 return -1;
222 }
223
224 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
225 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
226 bare_ref);
227 return -1;
228 }
229
230 tracking->remote = xstrdup(branch->remote_name);
231 for (i = 0; i < branch->merge_nr; i++)
232 string_list_append(tracking->srcs, branch->merge_name[i]);
233 return 0;
234}
235
e496c003 236/*
e89f151d
GC
237 * Used internally to set the branch.<new_ref>.{remote,merge} config
238 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
239 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
240 * will not be expanded to "refs/remotes/origin/main", so it is not safe
241 * for 'orig_ref' to be raw user input.
e496c003 242 */
27852b2c
PS
243static void setup_tracking(const char *new_ref, const char *orig_ref,
244 enum branch_track track, int quiet)
e496c003 245{
e496c003 246 struct tracking tracking;
d3115660 247 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
f9a482e6 248 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
e4921d87
TK
249 struct find_tracked_branch_cb ftb_cb = {
250 .tracking = &tracking,
251 .ambiguous_remotes = STRING_LIST_INIT_DUP,
252 };
e496c003 253
75388bf5
GC
254 if (!track)
255 BUG("asked to set up tracking, but tracking is disallowed");
256
e496c003
DB
257 memset(&tracking, 0, sizeof(tracking));
258 tracking.spec.dst = (char *)orig_ref;
d3115660
JS
259 tracking.srcs = &tracking_srcs;
260 if (track != BRANCH_TRACK_INHERIT)
e4921d87 261 for_each_remote(find_tracked_branch, &ftb_cb);
d3115660 262 else if (inherit_tracking(&tracking, orig_ref))
679e3693 263 goto cleanup;
e496c003 264
9ed36cfa
JS
265 if (!tracking.matches)
266 switch (track) {
bdaf1dfa 267 /* If ref is not remote, still use local */
9ed36cfa
JS
268 case BRANCH_TRACK_ALWAYS:
269 case BRANCH_TRACK_EXPLICIT:
4fc50066 270 case BRANCH_TRACK_OVERRIDE:
bdaf1dfa 271 /* Remote matches not evaluated */
d3115660 272 case BRANCH_TRACK_INHERIT:
9ed36cfa 273 break;
bdaf1dfa 274 /* Otherwise, if no remote don't track */
9ed36cfa 275 default:
679e3693 276 goto cleanup;
9ed36cfa
JS
277 }
278
bdaf1dfa
TK
279 /*
280 * This check does not apply to BRANCH_TRACK_INHERIT;
281 * that supports multiple entries in tracking_srcs but
282 * leaves tracking.matches at 0.
283 */
e4921d87
TK
284 if (tracking.matches > 1) {
285 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
286 orig_ref);
287 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
288 struct strbuf remotes_advice = STRBUF_INIT;
289 struct string_list_item *item;
290
291 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
292 /*
293 * TRANSLATORS: This is a line listing a remote with duplicate
294 * refspecs in the advice message below. For RTL languages you'll
295 * probably want to swap the "%s" and leading " " space around.
296 */
297 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
298
299 /*
300 * TRANSLATORS: The second argument is a \n-delimited list of
301 * duplicate refspecs, composed above.
302 */
303 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
304 "tracking ref '%s':\n"
305 "%s"
306 "\n"
307 "This is typically a configuration error.\n"
308 "\n"
309 "To support setting up tracking branches, ensure that\n"
310 "different remotes' fetch refspecs map into different\n"
311 "tracking namespaces."), orig_ref,
312 remotes_advice.buf);
313 strbuf_release(&remotes_advice);
314 }
315 exit(status);
316 }
e496c003 317
bdaf1dfa
TK
318 if (track == BRANCH_TRACK_SIMPLE) {
319 /*
320 * Only track if remote branch name matches.
321 * Reaching into items[0].string is safe because
322 * we know there is at least one and not more than
323 * one entry (because only BRANCH_TRACK_INHERIT can
324 * produce more than one entry).
325 */
326 const char *tracked_branch;
327 if (!skip_prefix(tracking.srcs->items[0].string,
328 "refs/heads/", &tracked_branch) ||
329 strcmp(tracked_branch, new_ref))
330 return;
331 }
332
d3115660
JS
333 if (tracking.srcs->nr < 1)
334 string_list_append(tracking.srcs, orig_ref);
335 if (install_branch_config_multiple_remotes(config_flags, new_ref,
336 tracking.remote, tracking.srcs) < 0)
5391e948 337 exit(1);
e496c003 338
679e3693
GC
339cleanup:
340 string_list_clear(&tracking_srcs, 0);
e4921d87 341 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
e496c003
DB
342}
343
6f9a3321
JH
344int read_branch_desc(struct strbuf *buf, const char *branch_name)
345{
540b0f49 346 char *v = NULL;
6f9a3321
JH
347 struct strbuf name = STRBUF_INIT;
348 strbuf_addf(&name, "branch.%s.description", branch_name);
540b0f49
TA
349 if (git_config_get_string(name.buf, &v)) {
350 strbuf_release(&name);
351 return -1;
352 }
353 strbuf_addstr(buf, v);
354 free(v);
6f9a3321
JH
355 strbuf_release(&name);
356 return 0;
357}
358
bc1c9c0e
JH
359/*
360 * Check if 'name' can be a valid name for a branch; die otherwise.
361 * Return 1 if the named branch already exists; return 0 otherwise.
362 * Fill ref with the full refname for the branch.
363 */
364int validate_branchname(const char *name, struct strbuf *ref)
55c4a673 365{
55c4a673 366 if (strbuf_check_branch_ref(ref, name))
7435e7e2 367 die(_("'%s' is not a valid branch name"), name);
55c4a673 368
bc1c9c0e
JH
369 return ref_exists(ref->buf);
370}
55c4a673 371
bc1c9c0e
JH
372/*
373 * Check if a branch 'name' can be created as a new branch; die otherwise.
374 * 'force' can be used when it is OK for the named branch already exists.
375 * Return 1 if the named branch already exists; return 0 otherwise.
376 * Fill ref with the full refname for the branch.
377 */
378int validate_new_branchname(const char *name, struct strbuf *ref, int force)
379{
593a2a5d
AK
380 struct worktree **worktrees;
381 const struct worktree *wt;
bc1c9c0e
JH
382
383 if (!validate_branchname(name, ref))
55c4a673 384 return 0;
55c4a673 385
8280c4c1 386 if (!force)
7435e7e2 387 die(_("a branch named '%s' already exists"),
8280c4c1
JH
388 ref->buf + strlen("refs/heads/"));
389
593a2a5d
AK
390 worktrees = get_worktrees();
391 wt = find_shared_symref(worktrees, "HEAD", ref->buf);
392 if (wt && !wt->is_bare)
68d924e1 393 die(_("cannot force update the branch '%s' "
593a2a5d
AK
394 "checked out at '%s'"),
395 ref->buf + strlen("refs/heads/"), wt->path);
396 free_worktrees(worktrees);
55c4a673
CI
397
398 return 1;
399}
400
41c21f22
JH
401static int check_tracking_branch(struct remote *remote, void *cb_data)
402{
403 char *tracking_branch = cb_data;
0ad4a5ff
BW
404 struct refspec_item query;
405 memset(&query, 0, sizeof(struct refspec_item));
41c21f22 406 query.dst = tracking_branch;
1d7358c5 407 return !remote_find_tracking(remote, &query);
41c21f22
JH
408}
409
410static int validate_remote_tracking_branch(char *ref)
411{
412 return !for_each_remote(check_tracking_branch, ref);
413}
414
e2b6aa5f 415static const char upstream_not_branch[] =
7435e7e2 416N_("cannot set up tracking information; starting point '%s' is not a branch");
a5e91c72 417static const char upstream_missing[] =
caa2036b
JK
418N_("the requested upstream branch '%s' does not exist");
419static const char upstream_advice[] =
420N_("\n"
421"If you are planning on basing your work on an upstream\n"
422"branch that already exists at the remote, you may need to\n"
423"run \"git fetch\" to retrieve it.\n"
424"\n"
425"If you are planning to push out a new local branch that\n"
426"will track its remote counterpart, you may want to use\n"
427"\"git push -u\" to set the upstream config as you push.");
e2b6aa5f 428
e89f151d
GC
429/**
430 * DWIMs a user-provided ref to determine the starting point for a
431 * branch and validates it, where:
432 *
433 * - r is the repository to validate the branch for
434 *
435 * - start_name is the ref that we would like to test. This is
436 * expanded with DWIM and assigned to out_real_ref.
437 *
438 * - track is the tracking mode of the new branch. If tracking is
439 * explicitly requested, start_name must be a branch (because
440 * otherwise start_name cannot be tracked)
441 *
442 * - out_oid is an out parameter containing the object_id of start_name
443 *
444 * - out_real_ref is an out parameter containing the full, 'real' form
445 * of start_name e.g. refs/heads/main instead of main
446 *
447 */
448static void dwim_branch_start(struct repository *r, const char *start_name,
449 enum branch_track track, char **out_real_ref,
450 struct object_id *out_oid)
e496c003 451{
e496c003 452 struct commit *commit;
48713bfa 453 struct object_id oid;
3818b258 454 char *real_ref;
4fc50066
IL
455 int explicit_tracking = 0;
456
457 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
458 explicit_tracking = 1;
e496c003 459
e496c003 460 real_ref = NULL;
e3d6539d 461 if (get_oid_mb(start_name, &oid)) {
caa2036b 462 if (explicit_tracking) {
66966012
GC
463 int code = die_message(_(upstream_missing), start_name);
464 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
465 _(upstream_advice));
466 exit(code);
caa2036b 467 }
7435e7e2 468 die(_("not a valid object name: '%s'"), start_name);
a5e91c72 469 }
e496c003 470
f24c30e0 471 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
e496c003
DB
472 case 0:
473 /* Not branching from any existing branch */
4fc50066 474 if (explicit_tracking)
1a15d00b 475 die(_(upstream_not_branch), start_name);
e496c003
DB
476 break;
477 case 1:
21b5b1e8 478 /* Unique completion -- good, only if it is a real branch */
59556548 479 if (!starts_with(real_ref, "refs/heads/") &&
41c21f22 480 validate_remote_tracking_branch(real_ref)) {
21b5b1e8 481 if (explicit_tracking)
1a15d00b 482 die(_(upstream_not_branch), start_name);
21b5b1e8 483 else
d895804b 484 FREE_AND_NULL(real_ref);
21b5b1e8 485 }
e496c003
DB
486 break;
487 default:
7435e7e2 488 die(_("ambiguous object name: '%s'"), start_name);
e496c003
DB
489 break;
490 }
491
afe8a907 492 if (!(commit = lookup_commit_reference(r, &oid)))
7435e7e2 493 die(_("not a valid branch point: '%s'"), start_name);
e89f151d
GC
494 if (out_real_ref) {
495 *out_real_ref = real_ref;
496 real_ref = NULL;
497 }
498 if (out_oid)
499 oidcpy(out_oid, &commit->object.oid);
500
501 FREE_AND_NULL(real_ref);
502}
503
504void create_branch(struct repository *r,
505 const char *name, const char *start_name,
506 int force, int clobber_head_ok, int reflog,
3f3e7608 507 int quiet, enum branch_track track, int dry_run)
e89f151d
GC
508{
509 struct object_id oid;
510 char *real_ref;
511 struct strbuf ref = STRBUF_INIT;
512 int forcing = 0;
bc0893cf
GC
513 struct ref_transaction *transaction;
514 struct strbuf err = STRBUF_INIT;
515 char *msg;
516
517 if (track == BRANCH_TRACK_OVERRIDE)
518 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
519 if (clobber_head_ok && !force)
520 BUG("'clobber_head_ok' can only be used with 'force'");
521
522 if (clobber_head_ok ?
523 validate_branchname(name, &ref) :
524 validate_new_branchname(name, &ref, force)) {
525 forcing = 1;
e89f151d
GC
526 }
527
528 dwim_branch_start(r, start_name, track, &real_ref, &oid);
3f3e7608
GC
529 if (dry_run)
530 goto cleanup;
e496c003 531
d43f990f 532 if (reflog)
341fb286 533 log_all_ref_updates = LOG_REFS_NORMAL;
d43f990f 534
bc0893cf
GC
535 if (forcing)
536 msg = xstrfmt("branch: Reset to %s", start_name);
537 else
538 msg = xstrfmt("branch: Created from %s", start_name);
539 transaction = ref_transaction_begin(&err);
540 if (!transaction ||
541 ref_transaction_update(transaction, ref.buf,
542 &oid, forcing ? NULL : null_oid(),
543 0, msg, &err) ||
544 ref_transaction_commit(transaction, &err))
545 die("%s", err.buf);
546 ref_transaction_free(transaction);
547 strbuf_release(&err);
548 free(msg);
d43f990f 549
e496c003 550 if (real_ref && track)
82a0672f 551 setup_tracking(ref.buf + 11, real_ref, track, quiet);
e496c003 552
3f3e7608 553cleanup:
8415d5c7 554 strbuf_release(&ref);
9ed36cfa 555 free(real_ref);
e496c003 556}
c369e7b8 557
e89f151d
GC
558void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
559 const char *orig_ref, enum branch_track track,
560 int quiet)
561{
562 char *real_orig_ref;
563 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
564 setup_tracking(new_ref, real_orig_ref, track, quiet);
565}
566
961b130d
GC
567/**
568 * Creates a branch in a submodule by calling
569 * create_branches_recursively() in a child process. The child process
570 * is necessary because install_branch_config_multiple_remotes() (which
571 * is called by setup_tracking()) does not support writing configs to
572 * submodules.
573 */
574static int submodule_create_branch(struct repository *r,
575 const struct submodule *submodule,
576 const char *name, const char *start_oid,
577 const char *tracking_name, int force,
578 int reflog, int quiet,
579 enum branch_track track, int dry_run)
580{
581 int ret = 0;
582 struct child_process child = CHILD_PROCESS_INIT;
583 struct strbuf child_err = STRBUF_INIT;
584 struct strbuf out_buf = STRBUF_INIT;
585 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
586 child.git_cmd = 1;
587 child.err = -1;
588 child.stdout_to_stderr = 1;
589
590 prepare_other_repo_env(&child.env_array, r->gitdir);
591 /*
592 * submodule_create_branch() is indirectly invoked by "git
593 * branch", but we cannot invoke "git branch" in the child
594 * process. "git branch" accepts a branch name and start point,
595 * where the start point is assumed to provide both the OID
596 * (start_oid) and the branch to use for tracking
597 * (tracking_name). But when recursing through submodules,
598 * start_oid and tracking name need to be specified separately
599 * (see create_branches_recursively()).
600 */
601 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
602 if (dry_run)
603 strvec_push(&child.args, "--dry-run");
604 if (force)
605 strvec_push(&child.args, "--force");
606 if (quiet)
607 strvec_push(&child.args, "--quiet");
608 if (reflog)
609 strvec_push(&child.args, "--create-reflog");
75388bf5
GC
610
611 switch (track) {
612 case BRANCH_TRACK_NEVER:
613 strvec_push(&child.args, "--no-track");
614 break;
615 case BRANCH_TRACK_ALWAYS:
616 case BRANCH_TRACK_EXPLICIT:
617 strvec_push(&child.args, "--track=direct");
618 break;
619 case BRANCH_TRACK_OVERRIDE:
620 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
621 break;
622 case BRANCH_TRACK_INHERIT:
623 strvec_push(&child.args, "--track=inherit");
624 break;
625 case BRANCH_TRACK_UNSPECIFIED:
1f888282 626 /* Default for "git checkout". Do not pass --track. */
75388bf5 627 case BRANCH_TRACK_REMOTE:
1f888282 628 /* Default for "git branch". Do not pass --track. */
bdaf1dfa
TK
629 case BRANCH_TRACK_SIMPLE:
630 /* Config-driven only. Do not pass --track. */
75388bf5
GC
631 break;
632 }
961b130d
GC
633
634 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
635
636 if ((ret = start_command(&child)))
637 return ret;
638 ret = finish_command(&child);
639 strbuf_read(&child_err, child.err, 0);
640 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
641
642 if (ret)
643 fprintf(stderr, "%s", out_buf.buf);
644 else
645 printf("%s", out_buf.buf);
646
647 strbuf_release(&child_err);
648 strbuf_release(&out_buf);
649 return ret;
650}
651
652void create_branches_recursively(struct repository *r, const char *name,
653 const char *start_commitish,
654 const char *tracking_name, int force,
655 int reflog, int quiet, enum branch_track track,
656 int dry_run)
657{
658 int i = 0;
659 char *branch_point = NULL;
660 struct object_id super_oid;
661 struct submodule_entry_list submodule_entry_list;
662
663 /* Perform dwim on start_commitish to get super_oid and branch_point. */
664 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
665 &branch_point, &super_oid);
666
667 /*
668 * If we were not given an explicit name to track, then assume we are at
669 * the top level and, just like the non-recursive case, the tracking
670 * name is the branch point.
671 */
672 if (!tracking_name)
673 tracking_name = branch_point;
674
675 submodules_of_tree(r, &super_oid, &submodule_entry_list);
676 /*
677 * Before creating any branches, first check that the branch can
678 * be created in every submodule.
679 */
680 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
e6bf70d1 681 if (!submodule_entry_list.entries[i].repo) {
cfbda6ba
GC
682 int code = die_message(
683 _("submodule '%s': unable to find submodule"),
684 submodule_entry_list.entries[i].submodule->name);
961b130d
GC
685 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
686 advise(_("You may try updating the submodules using 'git checkout %s && git submodule update --init'"),
687 start_commitish);
cfbda6ba 688 exit(code);
961b130d
GC
689 }
690
691 if (submodule_create_branch(
692 submodule_entry_list.entries[i].repo,
693 submodule_entry_list.entries[i].submodule, name,
694 oid_to_hex(&submodule_entry_list.entries[i]
695 .name_entry->oid),
696 tracking_name, force, reflog, quiet, track, 1))
697 die(_("submodule '%s': cannot create branch '%s'"),
698 submodule_entry_list.entries[i].submodule->name,
699 name);
700 }
701
702 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
703 BRANCH_TRACK_NEVER, dry_run);
704 if (dry_run)
705 return;
706 /*
707 * NEEDSWORK If tracking was set up in the superproject but not the
708 * submodule, users might expect "git branch --recurse-submodules" to
709 * fail or give a warning, but this is not yet implemented because it is
710 * tedious to determine whether or not tracking was set up in the
711 * superproject.
712 */
75388bf5
GC
713 if (track)
714 setup_tracking(name, tracking_name, track, quiet);
961b130d
GC
715
716 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
717 if (submodule_create_branch(
718 submodule_entry_list.entries[i].repo,
719 submodule_entry_list.entries[i].submodule, name,
720 oid_to_hex(&submodule_entry_list.entries[i]
721 .name_entry->oid),
722 tracking_name, force, reflog, quiet, track, 0))
723 die(_("submodule '%s': cannot create branch '%s'"),
724 submodule_entry_list.entries[i].submodule->name,
725 name);
726 repo_clear(submodule_entry_list.entries[i].repo);
727 }
728}
729
b6433555 730void remove_merge_branch_state(struct repository *r)
c369e7b8 731{
4edce172
NTND
732 unlink(git_path_merge_head(r));
733 unlink(git_path_merge_rr(r));
734 unlink(git_path_merge_msg(r));
735 unlink(git_path_merge_mode(r));
5291828d 736 unlink(git_path_auto_merge(r));
a03b5553 737 save_autostash(git_path_merge_autostash(r));
b6433555
NTND
738}
739
f496b064 740void remove_branch_state(struct repository *r, int verbose)
b6433555 741{
f496b064 742 sequencer_post_commit_cleanup(r, verbose);
4edce172 743 unlink(git_path_squash_msg(r));
b6433555 744 remove_merge_branch_state(r);
c369e7b8 745}
ed89f84b 746
8d9fdd70 747void die_if_checked_out(const char *branch, int ignore_current_worktree)
41af6565 748{
c8dd491f 749 struct worktree **worktrees = get_worktrees();
d3b9ac07 750 const struct worktree *wt;
41af6565 751
c8dd491f
AK
752 wt = find_shared_symref(worktrees, "HEAD", branch);
753 if (wt && (!ignore_current_worktree || !wt->is_current)) {
754 skip_prefix(branch, "refs/heads/", &branch);
755 die(_("'%s' is already checked out at '%s'"), branch, wt->path);
756 }
757
758 free_worktrees(worktrees);
ed89f84b 759}
70999e9c 760
39ee4c6c
KM
761int replace_each_worktree_head_symref(const char *oldref, const char *newref,
762 const char *logmsg)
70999e9c
KY
763{
764 int ret = 0;
03f2465b 765 struct worktree **worktrees = get_worktrees();
70999e9c
KY
766 int i;
767
768 for (i = 0; worktrees[i]; i++) {
d026a256
NTND
769 struct ref_store *refs;
770
70999e9c
KY
771 if (worktrees[i]->is_detached)
772 continue;
31824d18
NTND
773 if (!worktrees[i]->head_ref)
774 continue;
775 if (strcmp(oldref, worktrees[i]->head_ref))
70999e9c
KY
776 continue;
777
d026a256
NTND
778 refs = get_worktree_ref_store(worktrees[i]);
779 if (refs_create_symref(refs, "HEAD", newref, logmsg))
780 ret = error(_("HEAD of working tree %s is not updated"),
781 worktrees[i]->path);
70999e9c
KY
782 }
783
784 free_worktrees(worktrees);
785 return ret;
786}