t2400: drop no-op `--sq` from rev-parse call
[git.git] / branch.c
blob99a0e7889e42226ce8230e8d0472268bcfec7fe5
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "branch.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "refs.h"
9 #include "refspec.h"
10 #include "remote.h"
11 #include "sequencer.h"
12 #include "commit.h"
13 #include "worktree.h"
14 #include "submodule-config.h"
15 #include "run-command.h"
16 #include "strmap.h"
18 struct tracking {
19 struct refspec_item spec;
20 struct string_list *srcs;
21 const char *remote;
22 int matches;
25 struct find_tracked_branch_cb {
26 struct tracking *tracking;
27 struct string_list ambiguous_remotes;
30 static int find_tracked_branch(struct remote *remote, void *priv)
32 struct find_tracked_branch_cb *ftb = priv;
33 struct tracking *tracking = ftb->tracking;
35 if (!remote_find_tracking(remote, &tracking->spec)) {
36 switch (++tracking->matches) {
37 case 1:
38 string_list_append(tracking->srcs, tracking->spec.src);
39 tracking->remote = remote->name;
40 break;
41 case 2:
42 /* there are at least two remotes; backfill the first one */
43 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
44 /* fall through */
45 default:
46 string_list_append(&ftb->ambiguous_remotes, remote->name);
47 free(tracking->spec.src);
48 string_list_clear(tracking->srcs, 0);
49 break;
51 /* remote_find_tracking() searches by src if present */
52 tracking->spec.src = NULL;
54 return 0;
57 static int should_setup_rebase(const char *origin)
59 switch (autorebase) {
60 case AUTOREBASE_NEVER:
61 return 0;
62 case AUTOREBASE_LOCAL:
63 return origin == NULL;
64 case AUTOREBASE_REMOTE:
65 return origin != NULL;
66 case AUTOREBASE_ALWAYS:
67 return 1;
69 return 0;
72 /**
73 * Install upstream tracking configuration for a branch; specifically, add
74 * `branch.<name>.remote` and `branch.<name>.merge` entries.
76 * `flag` contains integer flags for options; currently only
77 * BRANCH_CONFIG_VERBOSE is checked.
79 * `local` is the name of the branch whose configuration we're installing.
81 * `origin` is the name of the remote owning the upstream branches. NULL means
82 * the upstream branches are local to this repo.
84 * `remotes` is a list of refs that are upstream of local
86 static int install_branch_config_multiple_remotes(int flag, const char *local,
87 const char *origin, struct string_list *remotes)
89 const char *shortname = NULL;
90 struct strbuf key = STRBUF_INIT;
91 struct string_list_item *item;
92 int rebasing = should_setup_rebase(origin);
94 if (!remotes->nr)
95 BUG("must provide at least one remote for branch config");
96 if (rebasing && remotes->nr > 1)
97 die(_("cannot inherit upstream tracking configuration of "
98 "multiple refs when rebasing is requested"));
101 * If the new branch is trying to track itself, something has gone
102 * wrong. Warn the user and don't proceed any further.
104 if (!origin)
105 for_each_string_list_item(item, remotes)
106 if (skip_prefix(item->string, "refs/heads/", &shortname)
107 && !strcmp(local, shortname)) {
108 warning(_("not setting branch '%s' as its own upstream"),
109 local);
110 return 0;
113 strbuf_addf(&key, "branch.%s.remote", local);
114 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
115 goto out_err;
117 strbuf_reset(&key);
118 strbuf_addf(&key, "branch.%s.merge", local);
120 * We want to overwrite any existing config with all the branches in
121 * "remotes". Override any existing config, then write our branches. If
122 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
123 * we've written so far.
125 if (git_config_set_gently(key.buf, NULL) < 0)
126 goto out_err;
127 for_each_string_list_item(item, remotes)
128 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
129 goto out_err;
131 if (rebasing) {
132 strbuf_reset(&key);
133 strbuf_addf(&key, "branch.%s.rebase", local);
134 if (git_config_set_gently(key.buf, "true") < 0)
135 goto out_err;
137 strbuf_release(&key);
139 if (flag & BRANCH_CONFIG_VERBOSE) {
140 struct strbuf tmp_ref_name = STRBUF_INIT;
141 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
143 for_each_string_list_item(item, remotes) {
144 shortname = item->string;
145 skip_prefix(shortname, "refs/heads/", &shortname);
146 if (origin) {
147 strbuf_addf(&tmp_ref_name, "%s/%s",
148 origin, shortname);
149 string_list_append_nodup(
150 &friendly_ref_names,
151 strbuf_detach(&tmp_ref_name, NULL));
152 } else {
153 string_list_append(
154 &friendly_ref_names, shortname);
158 if (remotes->nr == 1) {
160 * Rebasing is only allowed in the case of a single
161 * upstream branch.
163 printf_ln(rebasing ?
164 _("branch '%s' set up to track '%s' by rebasing.") :
165 _("branch '%s' set up to track '%s'."),
166 local, friendly_ref_names.items[0].string);
167 } else {
168 printf_ln(_("branch '%s' set up to track:"), local);
169 for_each_string_list_item(item, &friendly_ref_names)
170 printf_ln(" %s", item->string);
173 string_list_clear(&friendly_ref_names, 0);
176 return 0;
178 out_err:
179 strbuf_release(&key);
180 error(_("unable to write upstream branch configuration"));
182 advise(_("\nAfter fixing the error cause you may try to fix up\n"
183 "the remote tracking information by invoking:"));
184 if (remotes->nr == 1)
185 advise(" git branch --set-upstream-to=%s%s%s",
186 origin ? origin : "",
187 origin ? "/" : "",
188 remotes->items[0].string);
189 else {
190 advise(" git config --add branch.\"%s\".remote %s",
191 local, origin ? origin : ".");
192 for_each_string_list_item(item, remotes)
193 advise(" git config --add branch.\"%s\".merge %s",
194 local, item->string);
197 return -1;
200 int install_branch_config(int flag, const char *local, const char *origin,
201 const char *remote)
203 int ret;
204 struct string_list remotes = STRING_LIST_INIT_DUP;
206 string_list_append(&remotes, remote);
207 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
208 string_list_clear(&remotes, 0);
209 return ret;
212 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
214 const char *bare_ref;
215 struct branch *branch;
216 int i;
218 bare_ref = orig_ref;
219 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
221 branch = branch_get(bare_ref);
222 if (!branch->remote_name) {
223 warning(_("asked to inherit tracking from '%s', but no remote is set"),
224 bare_ref);
225 return -1;
228 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
229 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
230 bare_ref);
231 return -1;
234 tracking->remote = xstrdup(branch->remote_name);
235 for (i = 0; i < branch->merge_nr; i++)
236 string_list_append(tracking->srcs, branch->merge_name[i]);
237 return 0;
241 * Used internally to set the branch.<new_ref>.{remote,merge} config
242 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
243 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
244 * will not be expanded to "refs/remotes/origin/main", so it is not safe
245 * for 'orig_ref' to be raw user input.
247 static void setup_tracking(const char *new_ref, const char *orig_ref,
248 enum branch_track track, int quiet)
250 struct tracking tracking;
251 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
252 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
253 struct find_tracked_branch_cb ftb_cb = {
254 .tracking = &tracking,
255 .ambiguous_remotes = STRING_LIST_INIT_DUP,
258 if (!track)
259 BUG("asked to set up tracking, but tracking is disallowed");
261 memset(&tracking, 0, sizeof(tracking));
262 tracking.spec.dst = (char *)orig_ref;
263 tracking.srcs = &tracking_srcs;
264 if (track != BRANCH_TRACK_INHERIT)
265 for_each_remote(find_tracked_branch, &ftb_cb);
266 else if (inherit_tracking(&tracking, orig_ref))
267 goto cleanup;
269 if (!tracking.matches)
270 switch (track) {
271 /* If ref is not remote, still use local */
272 case BRANCH_TRACK_ALWAYS:
273 case BRANCH_TRACK_EXPLICIT:
274 case BRANCH_TRACK_OVERRIDE:
275 /* Remote matches not evaluated */
276 case BRANCH_TRACK_INHERIT:
277 break;
278 /* Otherwise, if no remote don't track */
279 default:
280 goto cleanup;
284 * This check does not apply to BRANCH_TRACK_INHERIT;
285 * that supports multiple entries in tracking_srcs but
286 * leaves tracking.matches at 0.
288 if (tracking.matches > 1) {
289 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
290 orig_ref);
291 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
292 struct strbuf remotes_advice = STRBUF_INIT;
293 struct string_list_item *item;
295 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
297 * TRANSLATORS: This is a line listing a remote with duplicate
298 * refspecs in the advice message below. For RTL languages you'll
299 * probably want to swap the "%s" and leading " " space around.
301 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
304 * TRANSLATORS: The second argument is a \n-delimited list of
305 * duplicate refspecs, composed above.
307 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
308 "tracking ref '%s':\n"
309 "%s"
310 "\n"
311 "This is typically a configuration error.\n"
312 "\n"
313 "To support setting up tracking branches, ensure that\n"
314 "different remotes' fetch refspecs map into different\n"
315 "tracking namespaces."), orig_ref,
316 remotes_advice.buf);
317 strbuf_release(&remotes_advice);
319 exit(status);
322 if (track == BRANCH_TRACK_SIMPLE) {
324 * Only track if remote branch name matches.
325 * Reaching into items[0].string is safe because
326 * we know there is at least one and not more than
327 * one entry (because only BRANCH_TRACK_INHERIT can
328 * produce more than one entry).
330 const char *tracked_branch;
331 if (!skip_prefix(tracking.srcs->items[0].string,
332 "refs/heads/", &tracked_branch) ||
333 strcmp(tracked_branch, new_ref))
334 return;
337 if (tracking.srcs->nr < 1)
338 string_list_append(tracking.srcs, orig_ref);
339 if (install_branch_config_multiple_remotes(config_flags, new_ref,
340 tracking.remote, tracking.srcs) < 0)
341 exit(1);
343 cleanup:
344 string_list_clear(&tracking_srcs, 0);
345 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
348 int read_branch_desc(struct strbuf *buf, const char *branch_name)
350 char *v = NULL;
351 struct strbuf name = STRBUF_INIT;
352 strbuf_addf(&name, "branch.%s.description", branch_name);
353 if (git_config_get_string(name.buf, &v)) {
354 strbuf_release(&name);
355 return -1;
357 strbuf_addstr(buf, v);
358 free(v);
359 strbuf_release(&name);
360 return 0;
364 * Check if 'name' can be a valid name for a branch; die otherwise.
365 * Return 1 if the named branch already exists; return 0 otherwise.
366 * Fill ref with the full refname for the branch.
368 int validate_branchname(const char *name, struct strbuf *ref)
370 if (strbuf_check_branch_ref(ref, name))
371 die(_("'%s' is not a valid branch name"), name);
373 return ref_exists(ref->buf);
376 static int initialized_checked_out_branches;
377 static struct strmap current_checked_out_branches = STRMAP_INIT;
379 static void prepare_checked_out_branches(void)
381 int i = 0;
382 struct worktree **worktrees;
384 if (initialized_checked_out_branches)
385 return;
386 initialized_checked_out_branches = 1;
388 worktrees = get_worktrees();
390 while (worktrees[i]) {
391 char *old;
392 struct wt_status_state state = { 0 };
393 struct worktree *wt = worktrees[i++];
394 struct string_list update_refs = STRING_LIST_INIT_DUP;
396 if (wt->is_bare)
397 continue;
399 if (wt->head_ref) {
400 old = strmap_put(&current_checked_out_branches,
401 wt->head_ref,
402 xstrdup(wt->path));
403 free(old);
406 if (wt_status_check_rebase(wt, &state) &&
407 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
408 state.branch) {
409 struct strbuf ref = STRBUF_INIT;
410 strbuf_addf(&ref, "refs/heads/%s", state.branch);
411 old = strmap_put(&current_checked_out_branches,
412 ref.buf,
413 xstrdup(wt->path));
414 free(old);
415 strbuf_release(&ref);
417 wt_status_state_free_buffers(&state);
419 if (wt_status_check_bisect(wt, &state) &&
420 state.branch) {
421 struct strbuf ref = STRBUF_INIT;
422 strbuf_addf(&ref, "refs/heads/%s", state.branch);
423 old = strmap_put(&current_checked_out_branches,
424 ref.buf,
425 xstrdup(wt->path));
426 free(old);
427 strbuf_release(&ref);
429 wt_status_state_free_buffers(&state);
431 if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
432 &update_refs)) {
433 struct string_list_item *item;
434 for_each_string_list_item(item, &update_refs) {
435 old = strmap_put(&current_checked_out_branches,
436 item->string,
437 xstrdup(wt->path));
438 free(old);
440 string_list_clear(&update_refs, 1);
444 free_worktrees(worktrees);
447 const char *branch_checked_out(const char *refname)
449 prepare_checked_out_branches();
450 return strmap_get(&current_checked_out_branches, refname);
454 * Check if a branch 'name' can be created as a new branch; die otherwise.
455 * 'force' can be used when it is OK for the named branch already exists.
456 * Return 1 if the named branch already exists; return 0 otherwise.
457 * Fill ref with the full refname for the branch.
459 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
461 const char *path;
462 if (!validate_branchname(name, ref))
463 return 0;
465 if (!force)
466 die(_("a branch named '%s' already exists"),
467 ref->buf + strlen("refs/heads/"));
469 if ((path = branch_checked_out(ref->buf)))
470 die(_("cannot force update the branch '%s' "
471 "checked out at '%s'"),
472 ref->buf + strlen("refs/heads/"), path);
474 return 1;
477 static int check_tracking_branch(struct remote *remote, void *cb_data)
479 char *tracking_branch = cb_data;
480 struct refspec_item query;
481 memset(&query, 0, sizeof(struct refspec_item));
482 query.dst = tracking_branch;
483 return !remote_find_tracking(remote, &query);
486 static int validate_remote_tracking_branch(char *ref)
488 return !for_each_remote(check_tracking_branch, ref);
491 static const char upstream_not_branch[] =
492 N_("cannot set up tracking information; starting point '%s' is not a branch");
493 static const char upstream_missing[] =
494 N_("the requested upstream branch '%s' does not exist");
495 static const char upstream_advice[] =
496 N_("\n"
497 "If you are planning on basing your work on an upstream\n"
498 "branch that already exists at the remote, you may need to\n"
499 "run \"git fetch\" to retrieve it.\n"
500 "\n"
501 "If you are planning to push out a new local branch that\n"
502 "will track its remote counterpart, you may want to use\n"
503 "\"git push -u\" to set the upstream config as you push.");
506 * DWIMs a user-provided ref to determine the starting point for a
507 * branch and validates it, where:
509 * - r is the repository to validate the branch for
511 * - start_name is the ref that we would like to test. This is
512 * expanded with DWIM and assigned to out_real_ref.
514 * - track is the tracking mode of the new branch. If tracking is
515 * explicitly requested, start_name must be a branch (because
516 * otherwise start_name cannot be tracked)
518 * - out_oid is an out parameter containing the object_id of start_name
520 * - out_real_ref is an out parameter containing the full, 'real' form
521 * of start_name e.g. refs/heads/main instead of main
524 static void dwim_branch_start(struct repository *r, const char *start_name,
525 enum branch_track track, char **out_real_ref,
526 struct object_id *out_oid)
528 struct commit *commit;
529 struct object_id oid;
530 char *real_ref;
531 int explicit_tracking = 0;
533 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
534 explicit_tracking = 1;
536 real_ref = NULL;
537 if (repo_get_oid_mb(r, start_name, &oid)) {
538 if (explicit_tracking) {
539 int code = die_message(_(upstream_missing), start_name);
540 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
541 _(upstream_advice));
542 exit(code);
544 die(_("not a valid object name: '%s'"), start_name);
547 switch (repo_dwim_ref(r, start_name, strlen(start_name), &oid,
548 &real_ref, 0)) {
549 case 0:
550 /* Not branching from any existing branch */
551 if (explicit_tracking)
552 die(_(upstream_not_branch), start_name);
553 break;
554 case 1:
555 /* Unique completion -- good, only if it is a real branch */
556 if (!starts_with(real_ref, "refs/heads/") &&
557 validate_remote_tracking_branch(real_ref)) {
558 if (explicit_tracking)
559 die(_(upstream_not_branch), start_name);
560 else
561 FREE_AND_NULL(real_ref);
563 break;
564 default:
565 die(_("ambiguous object name: '%s'"), start_name);
566 break;
569 if (!(commit = lookup_commit_reference(r, &oid)))
570 die(_("not a valid branch point: '%s'"), start_name);
571 if (out_real_ref) {
572 *out_real_ref = real_ref;
573 real_ref = NULL;
575 if (out_oid)
576 oidcpy(out_oid, &commit->object.oid);
578 FREE_AND_NULL(real_ref);
581 void create_branch(struct repository *r,
582 const char *name, const char *start_name,
583 int force, int clobber_head_ok, int reflog,
584 int quiet, enum branch_track track, int dry_run)
586 struct object_id oid;
587 char *real_ref;
588 struct strbuf ref = STRBUF_INIT;
589 int forcing = 0;
590 struct ref_transaction *transaction;
591 struct strbuf err = STRBUF_INIT;
592 char *msg;
594 if (track == BRANCH_TRACK_OVERRIDE)
595 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
596 if (clobber_head_ok && !force)
597 BUG("'clobber_head_ok' can only be used with 'force'");
599 if (clobber_head_ok ?
600 validate_branchname(name, &ref) :
601 validate_new_branchname(name, &ref, force)) {
602 forcing = 1;
605 dwim_branch_start(r, start_name, track, &real_ref, &oid);
606 if (dry_run)
607 goto cleanup;
609 if (reflog)
610 log_all_ref_updates = LOG_REFS_NORMAL;
612 if (forcing)
613 msg = xstrfmt("branch: Reset to %s", start_name);
614 else
615 msg = xstrfmt("branch: Created from %s", start_name);
616 transaction = ref_transaction_begin(&err);
617 if (!transaction ||
618 ref_transaction_update(transaction, ref.buf,
619 &oid, forcing ? NULL : null_oid(),
620 0, msg, &err) ||
621 ref_transaction_commit(transaction, &err))
622 die("%s", err.buf);
623 ref_transaction_free(transaction);
624 strbuf_release(&err);
625 free(msg);
627 if (real_ref && track)
628 setup_tracking(ref.buf + 11, real_ref, track, quiet);
630 cleanup:
631 strbuf_release(&ref);
632 free(real_ref);
635 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
636 const char *orig_ref, enum branch_track track,
637 int quiet)
639 char *real_orig_ref;
640 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
641 setup_tracking(new_ref, real_orig_ref, track, quiet);
645 * Creates a branch in a submodule by calling
646 * create_branches_recursively() in a child process. The child process
647 * is necessary because install_branch_config_multiple_remotes() (which
648 * is called by setup_tracking()) does not support writing configs to
649 * submodules.
651 static int submodule_create_branch(struct repository *r,
652 const struct submodule *submodule,
653 const char *name, const char *start_oid,
654 const char *tracking_name, int force,
655 int reflog, int quiet,
656 enum branch_track track, int dry_run)
658 int ret = 0;
659 struct child_process child = CHILD_PROCESS_INIT;
660 struct strbuf child_err = STRBUF_INIT;
661 struct strbuf out_buf = STRBUF_INIT;
662 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
663 child.git_cmd = 1;
664 child.err = -1;
665 child.stdout_to_stderr = 1;
667 prepare_other_repo_env(&child.env, r->gitdir);
669 * submodule_create_branch() is indirectly invoked by "git
670 * branch", but we cannot invoke "git branch" in the child
671 * process. "git branch" accepts a branch name and start point,
672 * where the start point is assumed to provide both the OID
673 * (start_oid) and the branch to use for tracking
674 * (tracking_name). But when recursing through submodules,
675 * start_oid and tracking name need to be specified separately
676 * (see create_branches_recursively()).
678 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
679 if (dry_run)
680 strvec_push(&child.args, "--dry-run");
681 if (force)
682 strvec_push(&child.args, "--force");
683 if (quiet)
684 strvec_push(&child.args, "--quiet");
685 if (reflog)
686 strvec_push(&child.args, "--create-reflog");
688 switch (track) {
689 case BRANCH_TRACK_NEVER:
690 strvec_push(&child.args, "--no-track");
691 break;
692 case BRANCH_TRACK_ALWAYS:
693 case BRANCH_TRACK_EXPLICIT:
694 strvec_push(&child.args, "--track=direct");
695 break;
696 case BRANCH_TRACK_OVERRIDE:
697 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
698 break;
699 case BRANCH_TRACK_INHERIT:
700 strvec_push(&child.args, "--track=inherit");
701 break;
702 case BRANCH_TRACK_UNSPECIFIED:
703 /* Default for "git checkout". Do not pass --track. */
704 case BRANCH_TRACK_REMOTE:
705 /* Default for "git branch". Do not pass --track. */
706 case BRANCH_TRACK_SIMPLE:
707 /* Config-driven only. Do not pass --track. */
708 break;
711 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
713 if ((ret = start_command(&child)))
714 return ret;
715 ret = finish_command(&child);
716 strbuf_read(&child_err, child.err, 0);
717 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
719 if (ret)
720 fprintf(stderr, "%s", out_buf.buf);
721 else
722 printf("%s", out_buf.buf);
724 strbuf_release(&child_err);
725 strbuf_release(&out_buf);
726 return ret;
729 void create_branches_recursively(struct repository *r, const char *name,
730 const char *start_commitish,
731 const char *tracking_name, int force,
732 int reflog, int quiet, enum branch_track track,
733 int dry_run)
735 int i = 0;
736 char *branch_point = NULL;
737 struct object_id super_oid;
738 struct submodule_entry_list submodule_entry_list;
740 /* Perform dwim on start_commitish to get super_oid and branch_point. */
741 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
742 &branch_point, &super_oid);
745 * If we were not given an explicit name to track, then assume we are at
746 * the top level and, just like the non-recursive case, the tracking
747 * name is the branch point.
749 if (!tracking_name)
750 tracking_name = branch_point;
752 submodules_of_tree(r, &super_oid, &submodule_entry_list);
754 * Before creating any branches, first check that the branch can
755 * be created in every submodule.
757 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
758 if (!submodule_entry_list.entries[i].repo) {
759 int code = die_message(
760 _("submodule '%s': unable to find submodule"),
761 submodule_entry_list.entries[i].submodule->name);
762 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
763 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
764 start_commitish);
765 exit(code);
768 if (submodule_create_branch(
769 submodule_entry_list.entries[i].repo,
770 submodule_entry_list.entries[i].submodule, name,
771 oid_to_hex(&submodule_entry_list.entries[i]
772 .name_entry->oid),
773 tracking_name, force, reflog, quiet, track, 1))
774 die(_("submodule '%s': cannot create branch '%s'"),
775 submodule_entry_list.entries[i].submodule->name,
776 name);
779 create_branch(r, name, start_commitish, force, 0, reflog, quiet,
780 BRANCH_TRACK_NEVER, dry_run);
781 if (dry_run)
782 return;
784 * NEEDSWORK If tracking was set up in the superproject but not the
785 * submodule, users might expect "git branch --recurse-submodules" to
786 * fail or give a warning, but this is not yet implemented because it is
787 * tedious to determine whether or not tracking was set up in the
788 * superproject.
790 if (track)
791 setup_tracking(name, tracking_name, track, quiet);
793 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
794 if (submodule_create_branch(
795 submodule_entry_list.entries[i].repo,
796 submodule_entry_list.entries[i].submodule, name,
797 oid_to_hex(&submodule_entry_list.entries[i]
798 .name_entry->oid),
799 tracking_name, force, reflog, quiet, track, 0))
800 die(_("submodule '%s': cannot create branch '%s'"),
801 submodule_entry_list.entries[i].submodule->name,
802 name);
803 repo_clear(submodule_entry_list.entries[i].repo);
807 void remove_merge_branch_state(struct repository *r)
809 unlink(git_path_merge_head(r));
810 unlink(git_path_merge_rr(r));
811 unlink(git_path_merge_msg(r));
812 unlink(git_path_merge_mode(r));
813 unlink(git_path_auto_merge(r));
814 save_autostash(git_path_merge_autostash(r));
817 void remove_branch_state(struct repository *r, int verbose)
819 sequencer_post_commit_cleanup(r, verbose);
820 unlink(git_path_squash_msg(r));
821 remove_merge_branch_state(r);
824 void die_if_checked_out(const char *branch, int ignore_current_worktree)
826 struct worktree **worktrees = get_worktrees();
828 for (int i = 0; worktrees[i]; i++) {
829 if (worktrees[i]->is_current && ignore_current_worktree)
830 continue;
832 if (is_shared_symref(worktrees[i], "HEAD", branch)) {
833 skip_prefix(branch, "refs/heads/", &branch);
834 die(_("'%s' is already checked out at '%s'"),
835 branch, worktrees[i]->path);
839 free_worktrees(worktrees);
842 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
843 const char *logmsg)
845 int ret = 0;
846 struct worktree **worktrees = get_worktrees();
847 int i;
849 for (i = 0; worktrees[i]; i++) {
850 struct ref_store *refs;
852 if (worktrees[i]->is_detached)
853 continue;
854 if (!worktrees[i]->head_ref)
855 continue;
856 if (strcmp(oldref, worktrees[i]->head_ref))
857 continue;
859 refs = get_worktree_ref_store(worktrees[i]);
860 if (refs_create_symref(refs, "HEAD", newref, logmsg))
861 ret = error(_("HEAD of working tree %s is not updated"),
862 worktrees[i]->path);
865 free_worktrees(worktrees);
866 return ret;