Merge branch 'pw/wildmatch-fixes'
[git.git] / branch.c
blobeacef62b7c97efde6e193f49f604346678d989f4
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "branch.h"
5 #include "hex.h"
6 #include "refs.h"
7 #include "refspec.h"
8 #include "remote.h"
9 #include "sequencer.h"
10 #include "commit.h"
11 #include "worktree.h"
12 #include "submodule-config.h"
13 #include "run-command.h"
14 #include "strmap.h"
16 struct tracking {
17 struct refspec_item spec;
18 struct string_list *srcs;
19 const char *remote;
20 int matches;
23 struct find_tracked_branch_cb {
24 struct tracking *tracking;
25 struct string_list ambiguous_remotes;
28 static int find_tracked_branch(struct remote *remote, void *priv)
30 struct find_tracked_branch_cb *ftb = priv;
31 struct tracking *tracking = ftb->tracking;
33 if (!remote_find_tracking(remote, &tracking->spec)) {
34 switch (++tracking->matches) {
35 case 1:
36 string_list_append(tracking->srcs, tracking->spec.src);
37 tracking->remote = remote->name;
38 break;
39 case 2:
40 /* there are at least two remotes; backfill the first one */
41 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
42 /* fall through */
43 default:
44 string_list_append(&ftb->ambiguous_remotes, remote->name);
45 free(tracking->spec.src);
46 string_list_clear(tracking->srcs, 0);
47 break;
49 /* remote_find_tracking() searches by src if present */
50 tracking->spec.src = NULL;
52 return 0;
55 static int should_setup_rebase(const char *origin)
57 switch (autorebase) {
58 case AUTOREBASE_NEVER:
59 return 0;
60 case AUTOREBASE_LOCAL:
61 return origin == NULL;
62 case AUTOREBASE_REMOTE:
63 return origin != NULL;
64 case AUTOREBASE_ALWAYS:
65 return 1;
67 return 0;
70 /**
71 * Install upstream tracking configuration for a branch; specifically, add
72 * `branch.<name>.remote` and `branch.<name>.merge` entries.
74 * `flag` contains integer flags for options; currently only
75 * BRANCH_CONFIG_VERBOSE is checked.
77 * `local` is the name of the branch whose configuration we're installing.
79 * `origin` is the name of the remote owning the upstream branches. NULL means
80 * the upstream branches are local to this repo.
82 * `remotes` is a list of refs that are upstream of local
84 static int install_branch_config_multiple_remotes(int flag, const char *local,
85 const char *origin, struct string_list *remotes)
87 const char *shortname = NULL;
88 struct strbuf key = STRBUF_INIT;
89 struct string_list_item *item;
90 int rebasing = should_setup_rebase(origin);
92 if (!remotes->nr)
93 BUG("must provide at least one remote for branch config");
94 if (rebasing && remotes->nr > 1)
95 die(_("cannot inherit upstream tracking configuration of "
96 "multiple refs when rebasing is requested"));
99 * If the new branch is trying to track itself, something has gone
100 * wrong. Warn the user and don't proceed any further.
102 if (!origin)
103 for_each_string_list_item(item, remotes)
104 if (skip_prefix(item->string, "refs/heads/", &shortname)
105 && !strcmp(local, shortname)) {
106 warning(_("not setting branch '%s' as its own upstream"),
107 local);
108 return 0;
111 strbuf_addf(&key, "branch.%s.remote", local);
112 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
113 goto out_err;
115 strbuf_reset(&key);
116 strbuf_addf(&key, "branch.%s.merge", local);
118 * We want to overwrite any existing config with all the branches in
119 * "remotes". Override any existing config, then write our branches. If
120 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
121 * we've written so far.
123 if (git_config_set_gently(key.buf, NULL) < 0)
124 goto out_err;
125 for_each_string_list_item(item, remotes)
126 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
127 goto out_err;
129 if (rebasing) {
130 strbuf_reset(&key);
131 strbuf_addf(&key, "branch.%s.rebase", local);
132 if (git_config_set_gently(key.buf, "true") < 0)
133 goto out_err;
135 strbuf_release(&key);
137 if (flag & BRANCH_CONFIG_VERBOSE) {
138 struct strbuf tmp_ref_name = STRBUF_INIT;
139 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
141 for_each_string_list_item(item, remotes) {
142 shortname = item->string;
143 skip_prefix(shortname, "refs/heads/", &shortname);
144 if (origin) {
145 strbuf_addf(&tmp_ref_name, "%s/%s",
146 origin, shortname);
147 string_list_append_nodup(
148 &friendly_ref_names,
149 strbuf_detach(&tmp_ref_name, NULL));
150 } else {
151 string_list_append(
152 &friendly_ref_names, shortname);
156 if (remotes->nr == 1) {
158 * Rebasing is only allowed in the case of a single
159 * upstream branch.
161 printf_ln(rebasing ?
162 _("branch '%s' set up to track '%s' by rebasing.") :
163 _("branch '%s' set up to track '%s'."),
164 local, friendly_ref_names.items[0].string);
165 } else {
166 printf_ln(_("branch '%s' set up to track:"), local);
167 for_each_string_list_item(item, &friendly_ref_names)
168 printf_ln(" %s", item->string);
171 string_list_clear(&friendly_ref_names, 0);
174 return 0;
176 out_err:
177 strbuf_release(&key);
178 error(_("unable to write upstream branch configuration"));
180 advise(_("\nAfter fixing the error cause you may try to fix up\n"
181 "the remote tracking information by invoking:"));
182 if (remotes->nr == 1)
183 advise(" git branch --set-upstream-to=%s%s%s",
184 origin ? origin : "",
185 origin ? "/" : "",
186 remotes->items[0].string);
187 else {
188 advise(" git config --add branch.\"%s\".remote %s",
189 local, origin ? origin : ".");
190 for_each_string_list_item(item, remotes)
191 advise(" git config --add branch.\"%s\".merge %s",
192 local, item->string);
195 return -1;
198 int install_branch_config(int flag, const char *local, const char *origin,
199 const char *remote)
201 int ret;
202 struct string_list remotes = STRING_LIST_INIT_DUP;
204 string_list_append(&remotes, remote);
205 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
206 string_list_clear(&remotes, 0);
207 return ret;
210 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
212 const char *bare_ref;
213 struct branch *branch;
214 int i;
216 bare_ref = orig_ref;
217 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
219 branch = branch_get(bare_ref);
220 if (!branch->remote_name) {
221 warning(_("asked to inherit tracking from '%s', but no remote is set"),
222 bare_ref);
223 return -1;
226 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
227 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
228 bare_ref);
229 return -1;
232 tracking->remote = xstrdup(branch->remote_name);
233 for (i = 0; i < branch->merge_nr; i++)
234 string_list_append(tracking->srcs, branch->merge_name[i]);
235 return 0;
239 * Used internally to set the branch.<new_ref>.{remote,merge} config
240 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
241 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
242 * will not be expanded to "refs/remotes/origin/main", so it is not safe
243 * for 'orig_ref' to be raw user input.
245 static void setup_tracking(const char *new_ref, const char *orig_ref,
246 enum branch_track track, int quiet)
248 struct tracking tracking;
249 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
250 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
251 struct find_tracked_branch_cb ftb_cb = {
252 .tracking = &tracking,
253 .ambiguous_remotes = STRING_LIST_INIT_DUP,
256 if (!track)
257 BUG("asked to set up tracking, but tracking is disallowed");
259 memset(&tracking, 0, sizeof(tracking));
260 tracking.spec.dst = (char *)orig_ref;
261 tracking.srcs = &tracking_srcs;
262 if (track != BRANCH_TRACK_INHERIT)
263 for_each_remote(find_tracked_branch, &ftb_cb);
264 else if (inherit_tracking(&tracking, orig_ref))
265 goto cleanup;
267 if (!tracking.matches)
268 switch (track) {
269 /* If ref is not remote, still use local */
270 case BRANCH_TRACK_ALWAYS:
271 case BRANCH_TRACK_EXPLICIT:
272 case BRANCH_TRACK_OVERRIDE:
273 /* Remote matches not evaluated */
274 case BRANCH_TRACK_INHERIT:
275 break;
276 /* Otherwise, if no remote don't track */
277 default:
278 goto cleanup;
282 * This check does not apply to BRANCH_TRACK_INHERIT;
283 * that supports multiple entries in tracking_srcs but
284 * leaves tracking.matches at 0.
286 if (tracking.matches > 1) {
287 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
288 orig_ref);
289 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
290 struct strbuf remotes_advice = STRBUF_INIT;
291 struct string_list_item *item;
293 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
295 * TRANSLATORS: This is a line listing a remote with duplicate
296 * refspecs in the advice message below. For RTL languages you'll
297 * probably want to swap the "%s" and leading " " space around.
299 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
302 * TRANSLATORS: The second argument is a \n-delimited list of
303 * duplicate refspecs, composed above.
305 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
306 "tracking ref '%s':\n"
307 "%s"
308 "\n"
309 "This is typically a configuration error.\n"
310 "\n"
311 "To support setting up tracking branches, ensure that\n"
312 "different remotes' fetch refspecs map into different\n"
313 "tracking namespaces."), orig_ref,
314 remotes_advice.buf);
315 strbuf_release(&remotes_advice);
317 exit(status);
320 if (track == BRANCH_TRACK_SIMPLE) {
322 * Only track if remote branch name matches.
323 * Reaching into items[0].string is safe because
324 * we know there is at least one and not more than
325 * one entry (because only BRANCH_TRACK_INHERIT can
326 * produce more than one entry).
328 const char *tracked_branch;
329 if (!skip_prefix(tracking.srcs->items[0].string,
330 "refs/heads/", &tracked_branch) ||
331 strcmp(tracked_branch, new_ref))
332 return;
335 if (tracking.srcs->nr < 1)
336 string_list_append(tracking.srcs, orig_ref);
337 if (install_branch_config_multiple_remotes(config_flags, new_ref,
338 tracking.remote, tracking.srcs) < 0)
339 exit(1);
341 cleanup:
342 string_list_clear(&tracking_srcs, 0);
343 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
346 int read_branch_desc(struct strbuf *buf, const char *branch_name)
348 char *v = NULL;
349 struct strbuf name = STRBUF_INIT;
350 strbuf_addf(&name, "branch.%s.description", branch_name);
351 if (git_config_get_string(name.buf, &v)) {
352 strbuf_release(&name);
353 return -1;
355 strbuf_addstr(buf, v);
356 free(v);
357 strbuf_release(&name);
358 return 0;
362 * Check if 'name' can be a valid name for a branch; die otherwise.
363 * Return 1 if the named branch already exists; return 0 otherwise.
364 * Fill ref with the full refname for the branch.
366 int validate_branchname(const char *name, struct strbuf *ref)
368 if (strbuf_check_branch_ref(ref, name))
369 die(_("'%s' is not a valid branch name"), name);
371 return ref_exists(ref->buf);
374 static int initialized_checked_out_branches;
375 static struct strmap current_checked_out_branches = STRMAP_INIT;
377 static void prepare_checked_out_branches(void)
379 int i = 0;
380 struct worktree **worktrees;
382 if (initialized_checked_out_branches)
383 return;
384 initialized_checked_out_branches = 1;
386 worktrees = get_worktrees();
388 while (worktrees[i]) {
389 char *old;
390 struct wt_status_state state = { 0 };
391 struct worktree *wt = worktrees[i++];
392 struct string_list update_refs = STRING_LIST_INIT_DUP;
394 if (wt->is_bare)
395 continue;
397 if (wt->head_ref) {
398 old = strmap_put(&current_checked_out_branches,
399 wt->head_ref,
400 xstrdup(wt->path));
401 free(old);
404 if (wt_status_check_rebase(wt, &state) &&
405 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
406 state.branch) {
407 struct strbuf ref = STRBUF_INIT;
408 strbuf_addf(&ref, "refs/heads/%s", state.branch);
409 old = strmap_put(&current_checked_out_branches,
410 ref.buf,
411 xstrdup(wt->path));
412 free(old);
413 strbuf_release(&ref);
415 wt_status_state_free_buffers(&state);
417 if (wt_status_check_bisect(wt, &state) &&
418 state.branch) {
419 struct strbuf ref = STRBUF_INIT;
420 strbuf_addf(&ref, "refs/heads/%s", state.branch);
421 old = strmap_put(&current_checked_out_branches,
422 ref.buf,
423 xstrdup(wt->path));
424 free(old);
425 strbuf_release(&ref);
427 wt_status_state_free_buffers(&state);
429 if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
430 &update_refs)) {
431 struct string_list_item *item;
432 for_each_string_list_item(item, &update_refs) {
433 old = strmap_put(&current_checked_out_branches,
434 item->string,
435 xstrdup(wt->path));
436 free(old);
438 string_list_clear(&update_refs, 1);
442 free_worktrees(worktrees);
445 const char *branch_checked_out(const char *refname)
447 prepare_checked_out_branches();
448 return strmap_get(&current_checked_out_branches, refname);
452 * Check if a branch 'name' can be created as a new branch; die otherwise.
453 * 'force' can be used when it is OK for the named branch already exists.
454 * Return 1 if the named branch already exists; return 0 otherwise.
455 * Fill ref with the full refname for the branch.
457 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
459 const char *path;
460 if (!validate_branchname(name, ref))
461 return 0;
463 if (!force)
464 die(_("a branch named '%s' already exists"),
465 ref->buf + strlen("refs/heads/"));
467 if ((path = branch_checked_out(ref->buf)))
468 die(_("cannot force update the branch '%s' "
469 "checked out at '%s'"),
470 ref->buf + strlen("refs/heads/"), path);
472 return 1;
475 static int check_tracking_branch(struct remote *remote, void *cb_data)
477 char *tracking_branch = cb_data;
478 struct refspec_item query;
479 memset(&query, 0, sizeof(struct refspec_item));
480 query.dst = tracking_branch;
481 return !remote_find_tracking(remote, &query);
484 static int validate_remote_tracking_branch(char *ref)
486 return !for_each_remote(check_tracking_branch, ref);
489 static const char upstream_not_branch[] =
490 N_("cannot set up tracking information; starting point '%s' is not a branch");
491 static const char upstream_missing[] =
492 N_("the requested upstream branch '%s' does not exist");
493 static const char upstream_advice[] =
494 N_("\n"
495 "If you are planning on basing your work on an upstream\n"
496 "branch that already exists at the remote, you may need to\n"
497 "run \"git fetch\" to retrieve it.\n"
498 "\n"
499 "If you are planning to push out a new local branch that\n"
500 "will track its remote counterpart, you may want to use\n"
501 "\"git push -u\" to set the upstream config as you push.");
504 * DWIMs a user-provided ref to determine the starting point for a
505 * branch and validates it, where:
507 * - r is the repository to validate the branch for
509 * - start_name is the ref that we would like to test. This is
510 * expanded with DWIM and assigned to out_real_ref.
512 * - track is the tracking mode of the new branch. If tracking is
513 * explicitly requested, start_name must be a branch (because
514 * otherwise start_name cannot be tracked)
516 * - out_oid is an out parameter containing the object_id of start_name
518 * - out_real_ref is an out parameter containing the full, 'real' form
519 * of start_name e.g. refs/heads/main instead of main
522 static void dwim_branch_start(struct repository *r, const char *start_name,
523 enum branch_track track, char **out_real_ref,
524 struct object_id *out_oid)
526 struct commit *commit;
527 struct object_id oid;
528 char *real_ref;
529 int explicit_tracking = 0;
531 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
532 explicit_tracking = 1;
534 real_ref = NULL;
535 if (get_oid_mb(start_name, &oid)) {
536 if (explicit_tracking) {
537 int code = die_message(_(upstream_missing), start_name);
538 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
539 _(upstream_advice));
540 exit(code);
542 die(_("not a valid object name: '%s'"), start_name);
545 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
546 case 0:
547 /* Not branching from any existing branch */
548 if (explicit_tracking)
549 die(_(upstream_not_branch), start_name);
550 break;
551 case 1:
552 /* Unique completion -- good, only if it is a real branch */
553 if (!starts_with(real_ref, "refs/heads/") &&
554 validate_remote_tracking_branch(real_ref)) {
555 if (explicit_tracking)
556 die(_(upstream_not_branch), start_name);
557 else
558 FREE_AND_NULL(real_ref);
560 break;
561 default:
562 die(_("ambiguous object name: '%s'"), start_name);
563 break;
566 if (!(commit = lookup_commit_reference(r, &oid)))
567 die(_("not a valid branch point: '%s'"), start_name);
568 if (out_real_ref) {
569 *out_real_ref = real_ref;
570 real_ref = NULL;
572 if (out_oid)
573 oidcpy(out_oid, &commit->object.oid);
575 FREE_AND_NULL(real_ref);
578 void create_branch(struct repository *r,
579 const char *name, const char *start_name,
580 int force, int clobber_head_ok, int reflog,
581 int quiet, enum branch_track track, int dry_run)
583 struct object_id oid;
584 char *real_ref;
585 struct strbuf ref = STRBUF_INIT;
586 int forcing = 0;
587 struct ref_transaction *transaction;
588 struct strbuf err = STRBUF_INIT;
589 char *msg;
591 if (track == BRANCH_TRACK_OVERRIDE)
592 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
593 if (clobber_head_ok && !force)
594 BUG("'clobber_head_ok' can only be used with 'force'");
596 if (clobber_head_ok ?
597 validate_branchname(name, &ref) :
598 validate_new_branchname(name, &ref, force)) {
599 forcing = 1;
602 dwim_branch_start(r, start_name, track, &real_ref, &oid);
603 if (dry_run)
604 goto cleanup;
606 if (reflog)
607 log_all_ref_updates = LOG_REFS_NORMAL;
609 if (forcing)
610 msg = xstrfmt("branch: Reset to %s", start_name);
611 else
612 msg = xstrfmt("branch: Created from %s", start_name);
613 transaction = ref_transaction_begin(&err);
614 if (!transaction ||
615 ref_transaction_update(transaction, ref.buf,
616 &oid, forcing ? NULL : null_oid(),
617 0, msg, &err) ||
618 ref_transaction_commit(transaction, &err))
619 die("%s", err.buf);
620 ref_transaction_free(transaction);
621 strbuf_release(&err);
622 free(msg);
624 if (real_ref && track)
625 setup_tracking(ref.buf + 11, real_ref, track, quiet);
627 cleanup:
628 strbuf_release(&ref);
629 free(real_ref);
632 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
633 const char *orig_ref, enum branch_track track,
634 int quiet)
636 char *real_orig_ref;
637 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
638 setup_tracking(new_ref, real_orig_ref, track, quiet);
642 * Creates a branch in a submodule by calling
643 * create_branches_recursively() in a child process. The child process
644 * is necessary because install_branch_config_multiple_remotes() (which
645 * is called by setup_tracking()) does not support writing configs to
646 * submodules.
648 static int submodule_create_branch(struct repository *r,
649 const struct submodule *submodule,
650 const char *name, const char *start_oid,
651 const char *tracking_name, int force,
652 int reflog, int quiet,
653 enum branch_track track, int dry_run)
655 int ret = 0;
656 struct child_process child = CHILD_PROCESS_INIT;
657 struct strbuf child_err = STRBUF_INIT;
658 struct strbuf out_buf = STRBUF_INIT;
659 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
660 child.git_cmd = 1;
661 child.err = -1;
662 child.stdout_to_stderr = 1;
664 prepare_other_repo_env(&child.env, r->gitdir);
666 * submodule_create_branch() is indirectly invoked by "git
667 * branch", but we cannot invoke "git branch" in the child
668 * process. "git branch" accepts a branch name and start point,
669 * where the start point is assumed to provide both the OID
670 * (start_oid) and the branch to use for tracking
671 * (tracking_name). But when recursing through submodules,
672 * start_oid and tracking name need to be specified separately
673 * (see create_branches_recursively()).
675 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
676 if (dry_run)
677 strvec_push(&child.args, "--dry-run");
678 if (force)
679 strvec_push(&child.args, "--force");
680 if (quiet)
681 strvec_push(&child.args, "--quiet");
682 if (reflog)
683 strvec_push(&child.args, "--create-reflog");
685 switch (track) {
686 case BRANCH_TRACK_NEVER:
687 strvec_push(&child.args, "--no-track");
688 break;
689 case BRANCH_TRACK_ALWAYS:
690 case BRANCH_TRACK_EXPLICIT:
691 strvec_push(&child.args, "--track=direct");
692 break;
693 case BRANCH_TRACK_OVERRIDE:
694 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
695 break;
696 case BRANCH_TRACK_INHERIT:
697 strvec_push(&child.args, "--track=inherit");
698 break;
699 case BRANCH_TRACK_UNSPECIFIED:
700 /* Default for "git checkout". Do not pass --track. */
701 case BRANCH_TRACK_REMOTE:
702 /* Default for "git branch". Do not pass --track. */
703 case BRANCH_TRACK_SIMPLE:
704 /* Config-driven only. Do not pass --track. */
705 break;
708 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
710 if ((ret = start_command(&child)))
711 return ret;
712 ret = finish_command(&child);
713 strbuf_read(&child_err, child.err, 0);
714 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
716 if (ret)
717 fprintf(stderr, "%s", out_buf.buf);
718 else
719 printf("%s", out_buf.buf);
721 strbuf_release(&child_err);
722 strbuf_release(&out_buf);
723 return ret;
726 void create_branches_recursively(struct repository *r, const char *name,
727 const char *start_commitish,
728 const char *tracking_name, int force,
729 int reflog, int quiet, enum branch_track track,
730 int dry_run)
732 int i = 0;
733 char *branch_point = NULL;
734 struct object_id super_oid;
735 struct submodule_entry_list submodule_entry_list;
737 /* Perform dwim on start_commitish to get super_oid and branch_point. */
738 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
739 &branch_point, &super_oid);
742 * If we were not given an explicit name to track, then assume we are at
743 * the top level and, just like the non-recursive case, the tracking
744 * name is the branch point.
746 if (!tracking_name)
747 tracking_name = branch_point;
749 submodules_of_tree(r, &super_oid, &submodule_entry_list);
751 * Before creating any branches, first check that the branch can
752 * be created in every submodule.
754 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
755 if (!submodule_entry_list.entries[i].repo) {
756 int code = die_message(
757 _("submodule '%s': unable to find submodule"),
758 submodule_entry_list.entries[i].submodule->name);
759 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
760 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
761 start_commitish);
762 exit(code);
765 if (submodule_create_branch(
766 submodule_entry_list.entries[i].repo,
767 submodule_entry_list.entries[i].submodule, name,
768 oid_to_hex(&submodule_entry_list.entries[i]
769 .name_entry->oid),
770 tracking_name, force, reflog, quiet, track, 1))
771 die(_("submodule '%s': cannot create branch '%s'"),
772 submodule_entry_list.entries[i].submodule->name,
773 name);
776 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
777 BRANCH_TRACK_NEVER, dry_run);
778 if (dry_run)
779 return;
781 * NEEDSWORK If tracking was set up in the superproject but not the
782 * submodule, users might expect "git branch --recurse-submodules" to
783 * fail or give a warning, but this is not yet implemented because it is
784 * tedious to determine whether or not tracking was set up in the
785 * superproject.
787 if (track)
788 setup_tracking(name, tracking_name, track, quiet);
790 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
791 if (submodule_create_branch(
792 submodule_entry_list.entries[i].repo,
793 submodule_entry_list.entries[i].submodule, name,
794 oid_to_hex(&submodule_entry_list.entries[i]
795 .name_entry->oid),
796 tracking_name, force, reflog, quiet, track, 0))
797 die(_("submodule '%s': cannot create branch '%s'"),
798 submodule_entry_list.entries[i].submodule->name,
799 name);
800 repo_clear(submodule_entry_list.entries[i].repo);
804 void remove_merge_branch_state(struct repository *r)
806 unlink(git_path_merge_head(r));
807 unlink(git_path_merge_rr(r));
808 unlink(git_path_merge_msg(r));
809 unlink(git_path_merge_mode(r));
810 unlink(git_path_auto_merge(r));
811 save_autostash(git_path_merge_autostash(r));
814 void remove_branch_state(struct repository *r, int verbose)
816 sequencer_post_commit_cleanup(r, verbose);
817 unlink(git_path_squash_msg(r));
818 remove_merge_branch_state(r);
821 void die_if_checked_out(const char *branch, int ignore_current_worktree)
823 struct worktree **worktrees = get_worktrees();
825 for (int i = 0; worktrees[i]; i++) {
826 if (worktrees[i]->is_current && ignore_current_worktree)
827 continue;
829 if (is_shared_symref(worktrees[i], "HEAD", branch)) {
830 skip_prefix(branch, "refs/heads/", &branch);
831 die(_("'%s' is already checked out at '%s'"),
832 branch, worktrees[i]->path);
836 free_worktrees(worktrees);
839 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
840 const char *logmsg)
842 int ret = 0;
843 struct worktree **worktrees = get_worktrees();
844 int i;
846 for (i = 0; worktrees[i]; i++) {
847 struct ref_store *refs;
849 if (worktrees[i]->is_detached)
850 continue;
851 if (!worktrees[i]->head_ref)
852 continue;
853 if (strcmp(oldref, worktrees[i]->head_ref))
854 continue;
856 refs = get_worktree_ref_store(worktrees[i]);
857 if (refs_create_symref(refs, "HEAD", newref, logmsg))
858 ret = error(_("HEAD of working tree %s is not updated"),
859 worktrees[i]->path);
862 free_worktrees(worktrees);
863 return ret;