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