wrapper.h: move declarations for wrapper.c functions from cache.h
[git/debian.git] / branch.c
blob66d32c68564526cf45c09411233986296152444f
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "branch.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "refs.h"
8 #include "refspec.h"
9 #include "remote.h"
10 #include "sequencer.h"
11 #include "commit.h"
12 #include "worktree.h"
13 #include "submodule-config.h"
14 #include "run-command.h"
15 #include "strmap.h"
17 struct tracking {
18 struct refspec_item spec;
19 struct string_list *srcs;
20 const char *remote;
21 int matches;
24 struct find_tracked_branch_cb {
25 struct tracking *tracking;
26 struct string_list ambiguous_remotes;
29 static int find_tracked_branch(struct remote *remote, void *priv)
31 struct find_tracked_branch_cb *ftb = priv;
32 struct tracking *tracking = ftb->tracking;
34 if (!remote_find_tracking(remote, &tracking->spec)) {
35 switch (++tracking->matches) {
36 case 1:
37 string_list_append(tracking->srcs, tracking->spec.src);
38 tracking->remote = remote->name;
39 break;
40 case 2:
41 /* there are at least two remotes; backfill the first one */
42 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
43 /* fall through */
44 default:
45 string_list_append(&ftb->ambiguous_remotes, remote->name);
46 free(tracking->spec.src);
47 string_list_clear(tracking->srcs, 0);
48 break;
50 /* remote_find_tracking() searches by src if present */
51 tracking->spec.src = NULL;
53 return 0;
56 static int should_setup_rebase(const char *origin)
58 switch (autorebase) {
59 case AUTOREBASE_NEVER:
60 return 0;
61 case AUTOREBASE_LOCAL:
62 return origin == NULL;
63 case AUTOREBASE_REMOTE:
64 return origin != NULL;
65 case AUTOREBASE_ALWAYS:
66 return 1;
68 return 0;
71 /**
72 * Install upstream tracking configuration for a branch; specifically, add
73 * `branch.<name>.remote` and `branch.<name>.merge` entries.
75 * `flag` contains integer flags for options; currently only
76 * BRANCH_CONFIG_VERBOSE is checked.
78 * `local` is the name of the branch whose configuration we're installing.
80 * `origin` is the name of the remote owning the upstream branches. NULL means
81 * the upstream branches are local to this repo.
83 * `remotes` is a list of refs that are upstream of local
85 static int install_branch_config_multiple_remotes(int flag, const char *local,
86 const char *origin, struct string_list *remotes)
88 const char *shortname = NULL;
89 struct strbuf key = STRBUF_INIT;
90 struct string_list_item *item;
91 int rebasing = should_setup_rebase(origin);
93 if (!remotes->nr)
94 BUG("must provide at least one remote for branch config");
95 if (rebasing && remotes->nr > 1)
96 die(_("cannot inherit upstream tracking configuration of "
97 "multiple refs when rebasing is requested"));
100 * If the new branch is trying to track itself, something has gone
101 * wrong. Warn the user and don't proceed any further.
103 if (!origin)
104 for_each_string_list_item(item, remotes)
105 if (skip_prefix(item->string, "refs/heads/", &shortname)
106 && !strcmp(local, shortname)) {
107 warning(_("not setting branch '%s' as its own upstream"),
108 local);
109 return 0;
112 strbuf_addf(&key, "branch.%s.remote", local);
113 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
114 goto out_err;
116 strbuf_reset(&key);
117 strbuf_addf(&key, "branch.%s.merge", local);
119 * We want to overwrite any existing config with all the branches in
120 * "remotes". Override any existing config, then write our branches. If
121 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
122 * we've written so far.
124 if (git_config_set_gently(key.buf, NULL) < 0)
125 goto out_err;
126 for_each_string_list_item(item, remotes)
127 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
128 goto out_err;
130 if (rebasing) {
131 strbuf_reset(&key);
132 strbuf_addf(&key, "branch.%s.rebase", local);
133 if (git_config_set_gently(key.buf, "true") < 0)
134 goto out_err;
136 strbuf_release(&key);
138 if (flag & BRANCH_CONFIG_VERBOSE) {
139 struct strbuf tmp_ref_name = STRBUF_INIT;
140 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
142 for_each_string_list_item(item, remotes) {
143 shortname = item->string;
144 skip_prefix(shortname, "refs/heads/", &shortname);
145 if (origin) {
146 strbuf_addf(&tmp_ref_name, "%s/%s",
147 origin, shortname);
148 string_list_append_nodup(
149 &friendly_ref_names,
150 strbuf_detach(&tmp_ref_name, NULL));
151 } else {
152 string_list_append(
153 &friendly_ref_names, shortname);
157 if (remotes->nr == 1) {
159 * Rebasing is only allowed in the case of a single
160 * upstream branch.
162 printf_ln(rebasing ?
163 _("branch '%s' set up to track '%s' by rebasing.") :
164 _("branch '%s' set up to track '%s'."),
165 local, friendly_ref_names.items[0].string);
166 } else {
167 printf_ln(_("branch '%s' set up to track:"), local);
168 for_each_string_list_item(item, &friendly_ref_names)
169 printf_ln(" %s", item->string);
172 string_list_clear(&friendly_ref_names, 0);
175 return 0;
177 out_err:
178 strbuf_release(&key);
179 error(_("unable to write upstream branch configuration"));
181 advise(_("\nAfter fixing the error cause you may try to fix up\n"
182 "the remote tracking information by invoking:"));
183 if (remotes->nr == 1)
184 advise(" git branch --set-upstream-to=%s%s%s",
185 origin ? origin : "",
186 origin ? "/" : "",
187 remotes->items[0].string);
188 else {
189 advise(" git config --add branch.\"%s\".remote %s",
190 local, origin ? origin : ".");
191 for_each_string_list_item(item, remotes)
192 advise(" git config --add branch.\"%s\".merge %s",
193 local, item->string);
196 return -1;
199 int install_branch_config(int flag, const char *local, const char *origin,
200 const char *remote)
202 int ret;
203 struct string_list remotes = STRING_LIST_INIT_DUP;
205 string_list_append(&remotes, remote);
206 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
207 string_list_clear(&remotes, 0);
208 return ret;
211 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
213 const char *bare_ref;
214 struct branch *branch;
215 int i;
217 bare_ref = orig_ref;
218 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
220 branch = branch_get(bare_ref);
221 if (!branch->remote_name) {
222 warning(_("asked to inherit tracking from '%s', but no remote is set"),
223 bare_ref);
224 return -1;
227 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
228 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
229 bare_ref);
230 return -1;
233 tracking->remote = xstrdup(branch->remote_name);
234 for (i = 0; i < branch->merge_nr; i++)
235 string_list_append(tracking->srcs, branch->merge_name[i]);
236 return 0;
240 * Used internally to set the branch.<new_ref>.{remote,merge} config
241 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
242 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
243 * will not be expanded to "refs/remotes/origin/main", so it is not safe
244 * for 'orig_ref' to be raw user input.
246 static void setup_tracking(const char *new_ref, const char *orig_ref,
247 enum branch_track track, int quiet)
249 struct tracking tracking;
250 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
251 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
252 struct find_tracked_branch_cb ftb_cb = {
253 .tracking = &tracking,
254 .ambiguous_remotes = STRING_LIST_INIT_DUP,
257 if (!track)
258 BUG("asked to set up tracking, but tracking is disallowed");
260 memset(&tracking, 0, sizeof(tracking));
261 tracking.spec.dst = (char *)orig_ref;
262 tracking.srcs = &tracking_srcs;
263 if (track != BRANCH_TRACK_INHERIT)
264 for_each_remote(find_tracked_branch, &ftb_cb);
265 else if (inherit_tracking(&tracking, orig_ref))
266 goto cleanup;
268 if (!tracking.matches)
269 switch (track) {
270 /* If ref is not remote, still use local */
271 case BRANCH_TRACK_ALWAYS:
272 case BRANCH_TRACK_EXPLICIT:
273 case BRANCH_TRACK_OVERRIDE:
274 /* Remote matches not evaluated */
275 case BRANCH_TRACK_INHERIT:
276 break;
277 /* Otherwise, if no remote don't track */
278 default:
279 goto cleanup;
283 * This check does not apply to BRANCH_TRACK_INHERIT;
284 * that supports multiple entries in tracking_srcs but
285 * leaves tracking.matches at 0.
287 if (tracking.matches > 1) {
288 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
289 orig_ref);
290 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
291 struct strbuf remotes_advice = STRBUF_INIT;
292 struct string_list_item *item;
294 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
296 * TRANSLATORS: This is a line listing a remote with duplicate
297 * refspecs in the advice message below. For RTL languages you'll
298 * probably want to swap the "%s" and leading " " space around.
300 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
303 * TRANSLATORS: The second argument is a \n-delimited list of
304 * duplicate refspecs, composed above.
306 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
307 "tracking ref '%s':\n"
308 "%s"
309 "\n"
310 "This is typically a configuration error.\n"
311 "\n"
312 "To support setting up tracking branches, ensure that\n"
313 "different remotes' fetch refspecs map into different\n"
314 "tracking namespaces."), orig_ref,
315 remotes_advice.buf);
316 strbuf_release(&remotes_advice);
318 exit(status);
321 if (track == BRANCH_TRACK_SIMPLE) {
323 * Only track if remote branch name matches.
324 * Reaching into items[0].string is safe because
325 * we know there is at least one and not more than
326 * one entry (because only BRANCH_TRACK_INHERIT can
327 * produce more than one entry).
329 const char *tracked_branch;
330 if (!skip_prefix(tracking.srcs->items[0].string,
331 "refs/heads/", &tracked_branch) ||
332 strcmp(tracked_branch, new_ref))
333 return;
336 if (tracking.srcs->nr < 1)
337 string_list_append(tracking.srcs, orig_ref);
338 if (install_branch_config_multiple_remotes(config_flags, new_ref,
339 tracking.remote, tracking.srcs) < 0)
340 exit(1);
342 cleanup:
343 string_list_clear(&tracking_srcs, 0);
344 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
347 int read_branch_desc(struct strbuf *buf, const char *branch_name)
349 char *v = NULL;
350 struct strbuf name = STRBUF_INIT;
351 strbuf_addf(&name, "branch.%s.description", branch_name);
352 if (git_config_get_string(name.buf, &v)) {
353 strbuf_release(&name);
354 return -1;
356 strbuf_addstr(buf, v);
357 free(v);
358 strbuf_release(&name);
359 return 0;
363 * Check if 'name' can be a valid name for a branch; die otherwise.
364 * Return 1 if the named branch already exists; return 0 otherwise.
365 * Fill ref with the full refname for the branch.
367 int validate_branchname(const char *name, struct strbuf *ref)
369 if (strbuf_check_branch_ref(ref, name))
370 die(_("'%s' is not a valid branch name"), name);
372 return ref_exists(ref->buf);
375 static int initialized_checked_out_branches;
376 static struct strmap current_checked_out_branches = STRMAP_INIT;
378 static void prepare_checked_out_branches(void)
380 int i = 0;
381 struct worktree **worktrees;
383 if (initialized_checked_out_branches)
384 return;
385 initialized_checked_out_branches = 1;
387 worktrees = get_worktrees();
389 while (worktrees[i]) {
390 char *old;
391 struct wt_status_state state = { 0 };
392 struct worktree *wt = worktrees[i++];
393 struct string_list update_refs = STRING_LIST_INIT_DUP;
395 if (wt->is_bare)
396 continue;
398 if (wt->head_ref) {
399 old = strmap_put(&current_checked_out_branches,
400 wt->head_ref,
401 xstrdup(wt->path));
402 free(old);
405 if (wt_status_check_rebase(wt, &state) &&
406 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
407 state.branch) {
408 struct strbuf ref = STRBUF_INIT;
409 strbuf_addf(&ref, "refs/heads/%s", state.branch);
410 old = strmap_put(&current_checked_out_branches,
411 ref.buf,
412 xstrdup(wt->path));
413 free(old);
414 strbuf_release(&ref);
416 wt_status_state_free_buffers(&state);
418 if (wt_status_check_bisect(wt, &state) &&
419 state.branch) {
420 struct strbuf ref = STRBUF_INIT;
421 strbuf_addf(&ref, "refs/heads/%s", state.branch);
422 old = strmap_put(&current_checked_out_branches,
423 ref.buf,
424 xstrdup(wt->path));
425 free(old);
426 strbuf_release(&ref);
428 wt_status_state_free_buffers(&state);
430 if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
431 &update_refs)) {
432 struct string_list_item *item;
433 for_each_string_list_item(item, &update_refs) {
434 old = strmap_put(&current_checked_out_branches,
435 item->string,
436 xstrdup(wt->path));
437 free(old);
439 string_list_clear(&update_refs, 1);
443 free_worktrees(worktrees);
446 const char *branch_checked_out(const char *refname)
448 prepare_checked_out_branches();
449 return strmap_get(&current_checked_out_branches, refname);
453 * Check if a branch 'name' can be created as a new branch; die otherwise.
454 * 'force' can be used when it is OK for the named branch already exists.
455 * Return 1 if the named branch already exists; return 0 otherwise.
456 * Fill ref with the full refname for the branch.
458 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
460 const char *path;
461 if (!validate_branchname(name, ref))
462 return 0;
464 if (!force)
465 die(_("a branch named '%s' already exists"),
466 ref->buf + strlen("refs/heads/"));
468 if ((path = branch_checked_out(ref->buf)))
469 die(_("cannot force update the branch '%s' "
470 "checked out at '%s'"),
471 ref->buf + strlen("refs/heads/"), path);
473 return 1;
476 static int check_tracking_branch(struct remote *remote, void *cb_data)
478 char *tracking_branch = cb_data;
479 struct refspec_item query;
480 memset(&query, 0, sizeof(struct refspec_item));
481 query.dst = tracking_branch;
482 return !remote_find_tracking(remote, &query);
485 static int validate_remote_tracking_branch(char *ref)
487 return !for_each_remote(check_tracking_branch, ref);
490 static const char upstream_not_branch[] =
491 N_("cannot set up tracking information; starting point '%s' is not a branch");
492 static const char upstream_missing[] =
493 N_("the requested upstream branch '%s' does not exist");
494 static const char upstream_advice[] =
495 N_("\n"
496 "If you are planning on basing your work on an upstream\n"
497 "branch that already exists at the remote, you may need to\n"
498 "run \"git fetch\" to retrieve it.\n"
499 "\n"
500 "If you are planning to push out a new local branch that\n"
501 "will track its remote counterpart, you may want to use\n"
502 "\"git push -u\" to set the upstream config as you push.");
505 * DWIMs a user-provided ref to determine the starting point for a
506 * branch and validates it, where:
508 * - r is the repository to validate the branch for
510 * - start_name is the ref that we would like to test. This is
511 * expanded with DWIM and assigned to out_real_ref.
513 * - track is the tracking mode of the new branch. If tracking is
514 * explicitly requested, start_name must be a branch (because
515 * otherwise start_name cannot be tracked)
517 * - out_oid is an out parameter containing the object_id of start_name
519 * - out_real_ref is an out parameter containing the full, 'real' form
520 * of start_name e.g. refs/heads/main instead of main
523 static void dwim_branch_start(struct repository *r, const char *start_name,
524 enum branch_track track, char **out_real_ref,
525 struct object_id *out_oid)
527 struct commit *commit;
528 struct object_id oid;
529 char *real_ref;
530 int explicit_tracking = 0;
532 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
533 explicit_tracking = 1;
535 real_ref = NULL;
536 if (get_oid_mb(start_name, &oid)) {
537 if (explicit_tracking) {
538 int code = die_message(_(upstream_missing), start_name);
539 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
540 _(upstream_advice));
541 exit(code);
543 die(_("not a valid object name: '%s'"), start_name);
546 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
547 case 0:
548 /* Not branching from any existing branch */
549 if (explicit_tracking)
550 die(_(upstream_not_branch), start_name);
551 break;
552 case 1:
553 /* Unique completion -- good, only if it is a real branch */
554 if (!starts_with(real_ref, "refs/heads/") &&
555 validate_remote_tracking_branch(real_ref)) {
556 if (explicit_tracking)
557 die(_(upstream_not_branch), start_name);
558 else
559 FREE_AND_NULL(real_ref);
561 break;
562 default:
563 die(_("ambiguous object name: '%s'"), start_name);
564 break;
567 if (!(commit = lookup_commit_reference(r, &oid)))
568 die(_("not a valid branch point: '%s'"), start_name);
569 if (out_real_ref) {
570 *out_real_ref = real_ref;
571 real_ref = NULL;
573 if (out_oid)
574 oidcpy(out_oid, &commit->object.oid);
576 FREE_AND_NULL(real_ref);
579 void create_branch(struct repository *r,
580 const char *name, const char *start_name,
581 int force, int clobber_head_ok, int reflog,
582 int quiet, enum branch_track track, int dry_run)
584 struct object_id oid;
585 char *real_ref;
586 struct strbuf ref = STRBUF_INIT;
587 int forcing = 0;
588 struct ref_transaction *transaction;
589 struct strbuf err = STRBUF_INIT;
590 char *msg;
592 if (track == BRANCH_TRACK_OVERRIDE)
593 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
594 if (clobber_head_ok && !force)
595 BUG("'clobber_head_ok' can only be used with 'force'");
597 if (clobber_head_ok ?
598 validate_branchname(name, &ref) :
599 validate_new_branchname(name, &ref, force)) {
600 forcing = 1;
603 dwim_branch_start(r, start_name, track, &real_ref, &oid);
604 if (dry_run)
605 goto cleanup;
607 if (reflog)
608 log_all_ref_updates = LOG_REFS_NORMAL;
610 if (forcing)
611 msg = xstrfmt("branch: Reset to %s", start_name);
612 else
613 msg = xstrfmt("branch: Created from %s", start_name);
614 transaction = ref_transaction_begin(&err);
615 if (!transaction ||
616 ref_transaction_update(transaction, ref.buf,
617 &oid, forcing ? NULL : null_oid(),
618 0, msg, &err) ||
619 ref_transaction_commit(transaction, &err))
620 die("%s", err.buf);
621 ref_transaction_free(transaction);
622 strbuf_release(&err);
623 free(msg);
625 if (real_ref && track)
626 setup_tracking(ref.buf + 11, real_ref, track, quiet);
628 cleanup:
629 strbuf_release(&ref);
630 free(real_ref);
633 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
634 const char *orig_ref, enum branch_track track,
635 int quiet)
637 char *real_orig_ref;
638 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
639 setup_tracking(new_ref, real_orig_ref, track, quiet);
643 * Creates a branch in a submodule by calling
644 * create_branches_recursively() in a child process. The child process
645 * is necessary because install_branch_config_multiple_remotes() (which
646 * is called by setup_tracking()) does not support writing configs to
647 * submodules.
649 static int submodule_create_branch(struct repository *r,
650 const struct submodule *submodule,
651 const char *name, const char *start_oid,
652 const char *tracking_name, int force,
653 int reflog, int quiet,
654 enum branch_track track, int dry_run)
656 int ret = 0;
657 struct child_process child = CHILD_PROCESS_INIT;
658 struct strbuf child_err = STRBUF_INIT;
659 struct strbuf out_buf = STRBUF_INIT;
660 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
661 child.git_cmd = 1;
662 child.err = -1;
663 child.stdout_to_stderr = 1;
665 prepare_other_repo_env(&child.env, r->gitdir);
667 * submodule_create_branch() is indirectly invoked by "git
668 * branch", but we cannot invoke "git branch" in the child
669 * process. "git branch" accepts a branch name and start point,
670 * where the start point is assumed to provide both the OID
671 * (start_oid) and the branch to use for tracking
672 * (tracking_name). But when recursing through submodules,
673 * start_oid and tracking name need to be specified separately
674 * (see create_branches_recursively()).
676 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
677 if (dry_run)
678 strvec_push(&child.args, "--dry-run");
679 if (force)
680 strvec_push(&child.args, "--force");
681 if (quiet)
682 strvec_push(&child.args, "--quiet");
683 if (reflog)
684 strvec_push(&child.args, "--create-reflog");
686 switch (track) {
687 case BRANCH_TRACK_NEVER:
688 strvec_push(&child.args, "--no-track");
689 break;
690 case BRANCH_TRACK_ALWAYS:
691 case BRANCH_TRACK_EXPLICIT:
692 strvec_push(&child.args, "--track=direct");
693 break;
694 case BRANCH_TRACK_OVERRIDE:
695 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
696 break;
697 case BRANCH_TRACK_INHERIT:
698 strvec_push(&child.args, "--track=inherit");
699 break;
700 case BRANCH_TRACK_UNSPECIFIED:
701 /* Default for "git checkout". Do not pass --track. */
702 case BRANCH_TRACK_REMOTE:
703 /* Default for "git branch". Do not pass --track. */
704 case BRANCH_TRACK_SIMPLE:
705 /* Config-driven only. Do not pass --track. */
706 break;
709 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
711 if ((ret = start_command(&child)))
712 return ret;
713 ret = finish_command(&child);
714 strbuf_read(&child_err, child.err, 0);
715 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
717 if (ret)
718 fprintf(stderr, "%s", out_buf.buf);
719 else
720 printf("%s", out_buf.buf);
722 strbuf_release(&child_err);
723 strbuf_release(&out_buf);
724 return ret;
727 void create_branches_recursively(struct repository *r, const char *name,
728 const char *start_commitish,
729 const char *tracking_name, int force,
730 int reflog, int quiet, enum branch_track track,
731 int dry_run)
733 int i = 0;
734 char *branch_point = NULL;
735 struct object_id super_oid;
736 struct submodule_entry_list submodule_entry_list;
738 /* Perform dwim on start_commitish to get super_oid and branch_point. */
739 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
740 &branch_point, &super_oid);
743 * If we were not given an explicit name to track, then assume we are at
744 * the top level and, just like the non-recursive case, the tracking
745 * name is the branch point.
747 if (!tracking_name)
748 tracking_name = branch_point;
750 submodules_of_tree(r, &super_oid, &submodule_entry_list);
752 * Before creating any branches, first check that the branch can
753 * be created in every submodule.
755 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
756 if (!submodule_entry_list.entries[i].repo) {
757 int code = die_message(
758 _("submodule '%s': unable to find submodule"),
759 submodule_entry_list.entries[i].submodule->name);
760 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
761 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
762 start_commitish);
763 exit(code);
766 if (submodule_create_branch(
767 submodule_entry_list.entries[i].repo,
768 submodule_entry_list.entries[i].submodule, name,
769 oid_to_hex(&submodule_entry_list.entries[i]
770 .name_entry->oid),
771 tracking_name, force, reflog, quiet, track, 1))
772 die(_("submodule '%s': cannot create branch '%s'"),
773 submodule_entry_list.entries[i].submodule->name,
774 name);
777 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
778 BRANCH_TRACK_NEVER, dry_run);
779 if (dry_run)
780 return;
782 * NEEDSWORK If tracking was set up in the superproject but not the
783 * submodule, users might expect "git branch --recurse-submodules" to
784 * fail or give a warning, but this is not yet implemented because it is
785 * tedious to determine whether or not tracking was set up in the
786 * superproject.
788 if (track)
789 setup_tracking(name, tracking_name, track, quiet);
791 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
792 if (submodule_create_branch(
793 submodule_entry_list.entries[i].repo,
794 submodule_entry_list.entries[i].submodule, name,
795 oid_to_hex(&submodule_entry_list.entries[i]
796 .name_entry->oid),
797 tracking_name, force, reflog, quiet, track, 0))
798 die(_("submodule '%s': cannot create branch '%s'"),
799 submodule_entry_list.entries[i].submodule->name,
800 name);
801 repo_clear(submodule_entry_list.entries[i].repo);
805 void remove_merge_branch_state(struct repository *r)
807 unlink(git_path_merge_head(r));
808 unlink(git_path_merge_rr(r));
809 unlink(git_path_merge_msg(r));
810 unlink(git_path_merge_mode(r));
811 unlink(git_path_auto_merge(r));
812 save_autostash(git_path_merge_autostash(r));
815 void remove_branch_state(struct repository *r, int verbose)
817 sequencer_post_commit_cleanup(r, verbose);
818 unlink(git_path_squash_msg(r));
819 remove_merge_branch_state(r);
822 void die_if_checked_out(const char *branch, int ignore_current_worktree)
824 struct worktree **worktrees = get_worktrees();
826 for (int i = 0; worktrees[i]; i++) {
827 if (worktrees[i]->is_current && ignore_current_worktree)
828 continue;
830 if (is_shared_symref(worktrees[i], "HEAD", branch)) {
831 skip_prefix(branch, "refs/heads/", &branch);
832 die(_("'%s' is already checked out at '%s'"),
833 branch, worktrees[i]->path);
837 free_worktrees(worktrees);
840 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
841 const char *logmsg)
843 int ret = 0;
844 struct worktree **worktrees = get_worktrees();
845 int i;
847 for (i = 0; worktrees[i]; i++) {
848 struct ref_store *refs;
850 if (worktrees[i]->is_detached)
851 continue;
852 if (!worktrees[i]->head_ref)
853 continue;
854 if (strcmp(oldref, worktrees[i]->head_ref))
855 continue;
857 refs = get_worktree_ref_store(worktrees[i]);
858 if (refs_create_symref(refs, "HEAD", newref, logmsg))
859 ret = error(_("HEAD of working tree %s is not updated"),
860 worktrees[i]->path);
863 free_worktrees(worktrees);
864 return ret;