object-store-ll.h: split this header out of object-store.h
[alt-git.git] / branch.c
blob20073284c8f135d1d6136dab00ae0d6a8573a8b4
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "config.h"
4 #include "branch.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "object-name.h"
9 #include "path.h"
10 #include "refs.h"
11 #include "refspec.h"
12 #include "remote.h"
13 #include "repository.h"
14 #include "sequencer.h"
15 #include "commit.h"
16 #include "worktree.h"
17 #include "submodule-config.h"
18 #include "run-command.h"
19 #include "strmap.h"
21 struct tracking {
22 struct refspec_item spec;
23 struct string_list *srcs;
24 const char *remote;
25 int matches;
28 struct find_tracked_branch_cb {
29 struct tracking *tracking;
30 struct string_list ambiguous_remotes;
33 static int find_tracked_branch(struct remote *remote, void *priv)
35 struct find_tracked_branch_cb *ftb = priv;
36 struct tracking *tracking = ftb->tracking;
38 if (!remote_find_tracking(remote, &tracking->spec)) {
39 switch (++tracking->matches) {
40 case 1:
41 string_list_append(tracking->srcs, tracking->spec.src);
42 tracking->remote = remote->name;
43 break;
44 case 2:
45 /* there are at least two remotes; backfill the first one */
46 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
47 /* fall through */
48 default:
49 string_list_append(&ftb->ambiguous_remotes, remote->name);
50 free(tracking->spec.src);
51 string_list_clear(tracking->srcs, 0);
52 break;
54 /* remote_find_tracking() searches by src if present */
55 tracking->spec.src = NULL;
57 return 0;
60 static int should_setup_rebase(const char *origin)
62 switch (autorebase) {
63 case AUTOREBASE_NEVER:
64 return 0;
65 case AUTOREBASE_LOCAL:
66 return origin == NULL;
67 case AUTOREBASE_REMOTE:
68 return origin != NULL;
69 case AUTOREBASE_ALWAYS:
70 return 1;
72 return 0;
75 /**
76 * Install upstream tracking configuration for a branch; specifically, add
77 * `branch.<name>.remote` and `branch.<name>.merge` entries.
79 * `flag` contains integer flags for options; currently only
80 * BRANCH_CONFIG_VERBOSE is checked.
82 * `local` is the name of the branch whose configuration we're installing.
84 * `origin` is the name of the remote owning the upstream branches. NULL means
85 * the upstream branches are local to this repo.
87 * `remotes` is a list of refs that are upstream of local
89 static int install_branch_config_multiple_remotes(int flag, const char *local,
90 const char *origin, struct string_list *remotes)
92 const char *shortname = NULL;
93 struct strbuf key = STRBUF_INIT;
94 struct string_list_item *item;
95 int rebasing = should_setup_rebase(origin);
97 if (!remotes->nr)
98 BUG("must provide at least one remote for branch config");
99 if (rebasing && remotes->nr > 1)
100 die(_("cannot inherit upstream tracking configuration of "
101 "multiple refs when rebasing is requested"));
104 * If the new branch is trying to track itself, something has gone
105 * wrong. Warn the user and don't proceed any further.
107 if (!origin)
108 for_each_string_list_item(item, remotes)
109 if (skip_prefix(item->string, "refs/heads/", &shortname)
110 && !strcmp(local, shortname)) {
111 warning(_("not setting branch '%s' as its own upstream"),
112 local);
113 return 0;
116 strbuf_addf(&key, "branch.%s.remote", local);
117 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
118 goto out_err;
120 strbuf_reset(&key);
121 strbuf_addf(&key, "branch.%s.merge", local);
123 * We want to overwrite any existing config with all the branches in
124 * "remotes". Override any existing config, then write our branches. If
125 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
126 * we've written so far.
128 if (git_config_set_gently(key.buf, NULL) < 0)
129 goto out_err;
130 for_each_string_list_item(item, remotes)
131 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
132 goto out_err;
134 if (rebasing) {
135 strbuf_reset(&key);
136 strbuf_addf(&key, "branch.%s.rebase", local);
137 if (git_config_set_gently(key.buf, "true") < 0)
138 goto out_err;
140 strbuf_release(&key);
142 if (flag & BRANCH_CONFIG_VERBOSE) {
143 struct strbuf tmp_ref_name = STRBUF_INIT;
144 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
146 for_each_string_list_item(item, remotes) {
147 shortname = item->string;
148 skip_prefix(shortname, "refs/heads/", &shortname);
149 if (origin) {
150 strbuf_addf(&tmp_ref_name, "%s/%s",
151 origin, shortname);
152 string_list_append_nodup(
153 &friendly_ref_names,
154 strbuf_detach(&tmp_ref_name, NULL));
155 } else {
156 string_list_append(
157 &friendly_ref_names, shortname);
161 if (remotes->nr == 1) {
163 * Rebasing is only allowed in the case of a single
164 * upstream branch.
166 printf_ln(rebasing ?
167 _("branch '%s' set up to track '%s' by rebasing.") :
168 _("branch '%s' set up to track '%s'."),
169 local, friendly_ref_names.items[0].string);
170 } else {
171 printf_ln(_("branch '%s' set up to track:"), local);
172 for_each_string_list_item(item, &friendly_ref_names)
173 printf_ln(" %s", item->string);
176 string_list_clear(&friendly_ref_names, 0);
179 return 0;
181 out_err:
182 strbuf_release(&key);
183 error(_("unable to write upstream branch configuration"));
185 advise(_("\nAfter fixing the error cause you may try to fix up\n"
186 "the remote tracking information by invoking:"));
187 if (remotes->nr == 1)
188 advise(" git branch --set-upstream-to=%s%s%s",
189 origin ? origin : "",
190 origin ? "/" : "",
191 remotes->items[0].string);
192 else {
193 advise(" git config --add branch.\"%s\".remote %s",
194 local, origin ? origin : ".");
195 for_each_string_list_item(item, remotes)
196 advise(" git config --add branch.\"%s\".merge %s",
197 local, item->string);
200 return -1;
203 int install_branch_config(int flag, const char *local, const char *origin,
204 const char *remote)
206 int ret;
207 struct string_list remotes = STRING_LIST_INIT_DUP;
209 string_list_append(&remotes, remote);
210 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
211 string_list_clear(&remotes, 0);
212 return ret;
215 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
217 const char *bare_ref;
218 struct branch *branch;
219 int i;
221 bare_ref = orig_ref;
222 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
224 branch = branch_get(bare_ref);
225 if (!branch->remote_name) {
226 warning(_("asked to inherit tracking from '%s', but no remote is set"),
227 bare_ref);
228 return -1;
231 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
232 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
233 bare_ref);
234 return -1;
237 tracking->remote = xstrdup(branch->remote_name);
238 for (i = 0; i < branch->merge_nr; i++)
239 string_list_append(tracking->srcs, branch->merge_name[i]);
240 return 0;
244 * Used internally to set the branch.<new_ref>.{remote,merge} config
245 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
246 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
247 * will not be expanded to "refs/remotes/origin/main", so it is not safe
248 * for 'orig_ref' to be raw user input.
250 static void setup_tracking(const char *new_ref, const char *orig_ref,
251 enum branch_track track, int quiet)
253 struct tracking tracking;
254 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
255 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
256 struct find_tracked_branch_cb ftb_cb = {
257 .tracking = &tracking,
258 .ambiguous_remotes = STRING_LIST_INIT_DUP,
261 if (!track)
262 BUG("asked to set up tracking, but tracking is disallowed");
264 memset(&tracking, 0, sizeof(tracking));
265 tracking.spec.dst = (char *)orig_ref;
266 tracking.srcs = &tracking_srcs;
267 if (track != BRANCH_TRACK_INHERIT)
268 for_each_remote(find_tracked_branch, &ftb_cb);
269 else if (inherit_tracking(&tracking, orig_ref))
270 goto cleanup;
272 if (!tracking.matches)
273 switch (track) {
274 /* If ref is not remote, still use local */
275 case BRANCH_TRACK_ALWAYS:
276 case BRANCH_TRACK_EXPLICIT:
277 case BRANCH_TRACK_OVERRIDE:
278 /* Remote matches not evaluated */
279 case BRANCH_TRACK_INHERIT:
280 break;
281 /* Otherwise, if no remote don't track */
282 default:
283 goto cleanup;
287 * This check does not apply to BRANCH_TRACK_INHERIT;
288 * that supports multiple entries in tracking_srcs but
289 * leaves tracking.matches at 0.
291 if (tracking.matches > 1) {
292 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
293 orig_ref);
294 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
295 struct strbuf remotes_advice = STRBUF_INIT;
296 struct string_list_item *item;
298 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
300 * TRANSLATORS: This is a line listing a remote with duplicate
301 * refspecs in the advice message below. For RTL languages you'll
302 * probably want to swap the "%s" and leading " " space around.
304 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
307 * TRANSLATORS: The second argument is a \n-delimited list of
308 * duplicate refspecs, composed above.
310 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
311 "tracking ref '%s':\n"
312 "%s"
313 "\n"
314 "This is typically a configuration error.\n"
315 "\n"
316 "To support setting up tracking branches, ensure that\n"
317 "different remotes' fetch refspecs map into different\n"
318 "tracking namespaces."), orig_ref,
319 remotes_advice.buf);
320 strbuf_release(&remotes_advice);
322 exit(status);
325 if (track == BRANCH_TRACK_SIMPLE) {
327 * Only track if remote branch name matches.
328 * Reaching into items[0].string is safe because
329 * we know there is at least one and not more than
330 * one entry (because only BRANCH_TRACK_INHERIT can
331 * produce more than one entry).
333 const char *tracked_branch;
334 if (!skip_prefix(tracking.srcs->items[0].string,
335 "refs/heads/", &tracked_branch) ||
336 strcmp(tracked_branch, new_ref))
337 return;
340 if (tracking.srcs->nr < 1)
341 string_list_append(tracking.srcs, orig_ref);
342 if (install_branch_config_multiple_remotes(config_flags, new_ref,
343 tracking.remote, tracking.srcs) < 0)
344 exit(1);
346 cleanup:
347 string_list_clear(&tracking_srcs, 0);
348 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
351 int read_branch_desc(struct strbuf *buf, const char *branch_name)
353 char *v = NULL;
354 struct strbuf name = STRBUF_INIT;
355 strbuf_addf(&name, "branch.%s.description", branch_name);
356 if (git_config_get_string(name.buf, &v)) {
357 strbuf_release(&name);
358 return -1;
360 strbuf_addstr(buf, v);
361 free(v);
362 strbuf_release(&name);
363 return 0;
367 * Check if 'name' can be a valid name for a branch; die otherwise.
368 * Return 1 if the named branch already exists; return 0 otherwise.
369 * Fill ref with the full refname for the branch.
371 int validate_branchname(const char *name, struct strbuf *ref)
373 if (strbuf_check_branch_ref(ref, name))
374 die(_("'%s' is not a valid branch name"), name);
376 return ref_exists(ref->buf);
379 static int initialized_checked_out_branches;
380 static struct strmap current_checked_out_branches = STRMAP_INIT;
382 static void prepare_checked_out_branches(void)
384 int i = 0;
385 struct worktree **worktrees;
387 if (initialized_checked_out_branches)
388 return;
389 initialized_checked_out_branches = 1;
391 worktrees = get_worktrees();
393 while (worktrees[i]) {
394 char *old;
395 struct wt_status_state state = { 0 };
396 struct worktree *wt = worktrees[i++];
397 struct string_list update_refs = STRING_LIST_INIT_DUP;
399 if (wt->is_bare)
400 continue;
402 if (wt->head_ref) {
403 old = strmap_put(&current_checked_out_branches,
404 wt->head_ref,
405 xstrdup(wt->path));
406 free(old);
409 if (wt_status_check_rebase(wt, &state) &&
410 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
411 state.branch) {
412 struct strbuf ref = STRBUF_INIT;
413 strbuf_addf(&ref, "refs/heads/%s", state.branch);
414 old = strmap_put(&current_checked_out_branches,
415 ref.buf,
416 xstrdup(wt->path));
417 free(old);
418 strbuf_release(&ref);
420 wt_status_state_free_buffers(&state);
422 if (wt_status_check_bisect(wt, &state) &&
423 state.branch) {
424 struct strbuf ref = STRBUF_INIT;
425 strbuf_addf(&ref, "refs/heads/%s", state.branch);
426 old = strmap_put(&current_checked_out_branches,
427 ref.buf,
428 xstrdup(wt->path));
429 free(old);
430 strbuf_release(&ref);
432 wt_status_state_free_buffers(&state);
434 if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
435 &update_refs)) {
436 struct string_list_item *item;
437 for_each_string_list_item(item, &update_refs) {
438 old = strmap_put(&current_checked_out_branches,
439 item->string,
440 xstrdup(wt->path));
441 free(old);
443 string_list_clear(&update_refs, 1);
447 free_worktrees(worktrees);
450 const char *branch_checked_out(const char *refname)
452 prepare_checked_out_branches();
453 return strmap_get(&current_checked_out_branches, refname);
457 * Check if a branch 'name' can be created as a new branch; die otherwise.
458 * 'force' can be used when it is OK for the named branch already exists.
459 * Return 1 if the named branch already exists; return 0 otherwise.
460 * Fill ref with the full refname for the branch.
462 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
464 const char *path;
465 if (!validate_branchname(name, ref))
466 return 0;
468 if (!force)
469 die(_("a branch named '%s' already exists"),
470 ref->buf + strlen("refs/heads/"));
472 if ((path = branch_checked_out(ref->buf)))
473 die(_("cannot force update the branch '%s' "
474 "checked out at '%s'"),
475 ref->buf + strlen("refs/heads/"), path);
477 return 1;
480 static int check_tracking_branch(struct remote *remote, void *cb_data)
482 char *tracking_branch = cb_data;
483 struct refspec_item query;
484 memset(&query, 0, sizeof(struct refspec_item));
485 query.dst = tracking_branch;
486 return !remote_find_tracking(remote, &query);
489 static int validate_remote_tracking_branch(char *ref)
491 return !for_each_remote(check_tracking_branch, ref);
494 static const char upstream_not_branch[] =
495 N_("cannot set up tracking information; starting point '%s' is not a branch");
496 static const char upstream_missing[] =
497 N_("the requested upstream branch '%s' does not exist");
498 static const char upstream_advice[] =
499 N_("\n"
500 "If you are planning on basing your work on an upstream\n"
501 "branch that already exists at the remote, you may need to\n"
502 "run \"git fetch\" to retrieve it.\n"
503 "\n"
504 "If you are planning to push out a new local branch that\n"
505 "will track its remote counterpart, you may want to use\n"
506 "\"git push -u\" to set the upstream config as you push.");
509 * DWIMs a user-provided ref to determine the starting point for a
510 * branch and validates it, where:
512 * - r is the repository to validate the branch for
514 * - start_name is the ref that we would like to test. This is
515 * expanded with DWIM and assigned to out_real_ref.
517 * - track is the tracking mode of the new branch. If tracking is
518 * explicitly requested, start_name must be a branch (because
519 * otherwise start_name cannot be tracked)
521 * - out_oid is an out parameter containing the object_id of start_name
523 * - out_real_ref is an out parameter containing the full, 'real' form
524 * of start_name e.g. refs/heads/main instead of main
527 static void dwim_branch_start(struct repository *r, const char *start_name,
528 enum branch_track track, char **out_real_ref,
529 struct object_id *out_oid)
531 struct commit *commit;
532 struct object_id oid;
533 char *real_ref;
534 int explicit_tracking = 0;
536 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
537 explicit_tracking = 1;
539 real_ref = NULL;
540 if (repo_get_oid_mb(r, start_name, &oid)) {
541 if (explicit_tracking) {
542 int code = die_message(_(upstream_missing), start_name);
543 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
544 _(upstream_advice));
545 exit(code);
547 die(_("not a valid object name: '%s'"), start_name);
550 switch (repo_dwim_ref(r, start_name, strlen(start_name), &oid,
551 &real_ref, 0)) {
552 case 0:
553 /* Not branching from any existing branch */
554 if (explicit_tracking)
555 die(_(upstream_not_branch), start_name);
556 break;
557 case 1:
558 /* Unique completion -- good, only if it is a real branch */
559 if (!starts_with(real_ref, "refs/heads/") &&
560 validate_remote_tracking_branch(real_ref)) {
561 if (explicit_tracking)
562 die(_(upstream_not_branch), start_name);
563 else
564 FREE_AND_NULL(real_ref);
566 break;
567 default:
568 die(_("ambiguous object name: '%s'"), start_name);
569 break;
572 if (!(commit = lookup_commit_reference(r, &oid)))
573 die(_("not a valid branch point: '%s'"), start_name);
574 if (out_real_ref) {
575 *out_real_ref = real_ref;
576 real_ref = NULL;
578 if (out_oid)
579 oidcpy(out_oid, &commit->object.oid);
581 FREE_AND_NULL(real_ref);
584 void create_branch(struct repository *r,
585 const char *name, const char *start_name,
586 int force, int clobber_head_ok, int reflog,
587 int quiet, enum branch_track track, int dry_run)
589 struct object_id oid;
590 char *real_ref;
591 struct strbuf ref = STRBUF_INIT;
592 int forcing = 0;
593 struct ref_transaction *transaction;
594 struct strbuf err = STRBUF_INIT;
595 char *msg;
597 if (track == BRANCH_TRACK_OVERRIDE)
598 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
599 if (clobber_head_ok && !force)
600 BUG("'clobber_head_ok' can only be used with 'force'");
602 if (clobber_head_ok ?
603 validate_branchname(name, &ref) :
604 validate_new_branchname(name, &ref, force)) {
605 forcing = 1;
608 dwim_branch_start(r, start_name, track, &real_ref, &oid);
609 if (dry_run)
610 goto cleanup;
612 if (reflog)
613 log_all_ref_updates = LOG_REFS_NORMAL;
615 if (forcing)
616 msg = xstrfmt("branch: Reset to %s", start_name);
617 else
618 msg = xstrfmt("branch: Created from %s", start_name);
619 transaction = ref_transaction_begin(&err);
620 if (!transaction ||
621 ref_transaction_update(transaction, ref.buf,
622 &oid, forcing ? NULL : null_oid(),
623 0, msg, &err) ||
624 ref_transaction_commit(transaction, &err))
625 die("%s", err.buf);
626 ref_transaction_free(transaction);
627 strbuf_release(&err);
628 free(msg);
630 if (real_ref && track)
631 setup_tracking(ref.buf + 11, real_ref, track, quiet);
633 cleanup:
634 strbuf_release(&ref);
635 free(real_ref);
638 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
639 const char *orig_ref, enum branch_track track,
640 int quiet)
642 char *real_orig_ref;
643 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
644 setup_tracking(new_ref, real_orig_ref, track, quiet);
648 * Creates a branch in a submodule by calling
649 * create_branches_recursively() in a child process. The child process
650 * is necessary because install_branch_config_multiple_remotes() (which
651 * is called by setup_tracking()) does not support writing configs to
652 * submodules.
654 static int submodule_create_branch(struct repository *r,
655 const struct submodule *submodule,
656 const char *name, const char *start_oid,
657 const char *tracking_name, int force,
658 int reflog, int quiet,
659 enum branch_track track, int dry_run)
661 int ret = 0;
662 struct child_process child = CHILD_PROCESS_INIT;
663 struct strbuf child_err = STRBUF_INIT;
664 struct strbuf out_buf = STRBUF_INIT;
665 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
666 child.git_cmd = 1;
667 child.err = -1;
668 child.stdout_to_stderr = 1;
670 prepare_other_repo_env(&child.env, r->gitdir);
672 * submodule_create_branch() is indirectly invoked by "git
673 * branch", but we cannot invoke "git branch" in the child
674 * process. "git branch" accepts a branch name and start point,
675 * where the start point is assumed to provide both the OID
676 * (start_oid) and the branch to use for tracking
677 * (tracking_name). But when recursing through submodules,
678 * start_oid and tracking name need to be specified separately
679 * (see create_branches_recursively()).
681 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
682 if (dry_run)
683 strvec_push(&child.args, "--dry-run");
684 if (force)
685 strvec_push(&child.args, "--force");
686 if (quiet)
687 strvec_push(&child.args, "--quiet");
688 if (reflog)
689 strvec_push(&child.args, "--create-reflog");
691 switch (track) {
692 case BRANCH_TRACK_NEVER:
693 strvec_push(&child.args, "--no-track");
694 break;
695 case BRANCH_TRACK_ALWAYS:
696 case BRANCH_TRACK_EXPLICIT:
697 strvec_push(&child.args, "--track=direct");
698 break;
699 case BRANCH_TRACK_OVERRIDE:
700 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
701 break;
702 case BRANCH_TRACK_INHERIT:
703 strvec_push(&child.args, "--track=inherit");
704 break;
705 case BRANCH_TRACK_UNSPECIFIED:
706 /* Default for "git checkout". Do not pass --track. */
707 case BRANCH_TRACK_REMOTE:
708 /* Default for "git branch". Do not pass --track. */
709 case BRANCH_TRACK_SIMPLE:
710 /* Config-driven only. Do not pass --track. */
711 break;
714 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
716 if ((ret = start_command(&child)))
717 return ret;
718 ret = finish_command(&child);
719 strbuf_read(&child_err, child.err, 0);
720 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
722 if (ret)
723 fprintf(stderr, "%s", out_buf.buf);
724 else
725 printf("%s", out_buf.buf);
727 strbuf_release(&child_err);
728 strbuf_release(&out_buf);
729 return ret;
732 void create_branches_recursively(struct repository *r, const char *name,
733 const char *start_commitish,
734 const char *tracking_name, int force,
735 int reflog, int quiet, enum branch_track track,
736 int dry_run)
738 int i = 0;
739 char *branch_point = NULL;
740 struct object_id super_oid;
741 struct submodule_entry_list submodule_entry_list;
743 /* Perform dwim on start_commitish to get super_oid and branch_point. */
744 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
745 &branch_point, &super_oid);
748 * If we were not given an explicit name to track, then assume we are at
749 * the top level and, just like the non-recursive case, the tracking
750 * name is the branch point.
752 if (!tracking_name)
753 tracking_name = branch_point;
755 submodules_of_tree(r, &super_oid, &submodule_entry_list);
757 * Before creating any branches, first check that the branch can
758 * be created in every submodule.
760 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
761 if (!submodule_entry_list.entries[i].repo) {
762 int code = die_message(
763 _("submodule '%s': unable to find submodule"),
764 submodule_entry_list.entries[i].submodule->name);
765 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
766 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
767 start_commitish);
768 exit(code);
771 if (submodule_create_branch(
772 submodule_entry_list.entries[i].repo,
773 submodule_entry_list.entries[i].submodule, name,
774 oid_to_hex(&submodule_entry_list.entries[i]
775 .name_entry->oid),
776 tracking_name, force, reflog, quiet, track, 1))
777 die(_("submodule '%s': cannot create branch '%s'"),
778 submodule_entry_list.entries[i].submodule->name,
779 name);
782 create_branch(r, name, start_commitish, force, 0, reflog, quiet,
783 BRANCH_TRACK_NEVER, dry_run);
784 if (dry_run)
785 return;
787 * NEEDSWORK If tracking was set up in the superproject but not the
788 * submodule, users might expect "git branch --recurse-submodules" to
789 * fail or give a warning, but this is not yet implemented because it is
790 * tedious to determine whether or not tracking was set up in the
791 * superproject.
793 if (track)
794 setup_tracking(name, tracking_name, track, quiet);
796 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
797 if (submodule_create_branch(
798 submodule_entry_list.entries[i].repo,
799 submodule_entry_list.entries[i].submodule, name,
800 oid_to_hex(&submodule_entry_list.entries[i]
801 .name_entry->oid),
802 tracking_name, force, reflog, quiet, track, 0))
803 die(_("submodule '%s': cannot create branch '%s'"),
804 submodule_entry_list.entries[i].submodule->name,
805 name);
806 repo_clear(submodule_entry_list.entries[i].repo);
810 void remove_merge_branch_state(struct repository *r)
812 unlink(git_path_merge_head(r));
813 unlink(git_path_merge_rr(r));
814 unlink(git_path_merge_msg(r));
815 unlink(git_path_merge_mode(r));
816 unlink(git_path_auto_merge(r));
817 save_autostash(git_path_merge_autostash(r));
820 void remove_branch_state(struct repository *r, int verbose)
822 sequencer_post_commit_cleanup(r, verbose);
823 unlink(git_path_squash_msg(r));
824 remove_merge_branch_state(r);
827 void die_if_checked_out(const char *branch, int ignore_current_worktree)
829 struct worktree **worktrees = get_worktrees();
831 for (int i = 0; worktrees[i]; i++) {
832 if (worktrees[i]->is_current && ignore_current_worktree)
833 continue;
835 if (is_shared_symref(worktrees[i], "HEAD", branch)) {
836 skip_prefix(branch, "refs/heads/", &branch);
837 die(_("'%s' is already checked out at '%s'"),
838 branch, worktrees[i]->path);
842 free_worktrees(worktrees);