object: add object_array initializer helper function
[git/gitster.git] / branch.c
blob7df982693af14f79a97b5c456a4416c3bd133a51
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 "refs.h"
10 #include "refspec.h"
11 #include "remote.h"
12 #include "sequencer.h"
13 #include "commit.h"
14 #include "worktree.h"
15 #include "submodule-config.h"
16 #include "run-command.h"
17 #include "strmap.h"
19 struct tracking {
20 struct refspec_item spec;
21 struct string_list *srcs;
22 const char *remote;
23 int matches;
26 struct find_tracked_branch_cb {
27 struct tracking *tracking;
28 struct string_list ambiguous_remotes;
31 static int find_tracked_branch(struct remote *remote, void *priv)
33 struct find_tracked_branch_cb *ftb = priv;
34 struct tracking *tracking = ftb->tracking;
36 if (!remote_find_tracking(remote, &tracking->spec)) {
37 switch (++tracking->matches) {
38 case 1:
39 string_list_append(tracking->srcs, tracking->spec.src);
40 tracking->remote = remote->name;
41 break;
42 case 2:
43 /* there are at least two remotes; backfill the first one */
44 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
45 /* fall through */
46 default:
47 string_list_append(&ftb->ambiguous_remotes, remote->name);
48 free(tracking->spec.src);
49 string_list_clear(tracking->srcs, 0);
50 break;
52 /* remote_find_tracking() searches by src if present */
53 tracking->spec.src = NULL;
55 return 0;
58 static int should_setup_rebase(const char *origin)
60 switch (autorebase) {
61 case AUTOREBASE_NEVER:
62 return 0;
63 case AUTOREBASE_LOCAL:
64 return origin == NULL;
65 case AUTOREBASE_REMOTE:
66 return origin != NULL;
67 case AUTOREBASE_ALWAYS:
68 return 1;
70 return 0;
73 /**
74 * Install upstream tracking configuration for a branch; specifically, add
75 * `branch.<name>.remote` and `branch.<name>.merge` entries.
77 * `flag` contains integer flags for options; currently only
78 * BRANCH_CONFIG_VERBOSE is checked.
80 * `local` is the name of the branch whose configuration we're installing.
82 * `origin` is the name of the remote owning the upstream branches. NULL means
83 * the upstream branches are local to this repo.
85 * `remotes` is a list of refs that are upstream of local
87 static int install_branch_config_multiple_remotes(int flag, const char *local,
88 const char *origin, struct string_list *remotes)
90 const char *shortname = NULL;
91 struct strbuf key = STRBUF_INIT;
92 struct string_list_item *item;
93 int rebasing = should_setup_rebase(origin);
95 if (!remotes->nr)
96 BUG("must provide at least one remote for branch config");
97 if (rebasing && remotes->nr > 1)
98 die(_("cannot inherit upstream tracking configuration of "
99 "multiple refs when rebasing is requested"));
102 * If the new branch is trying to track itself, something has gone
103 * wrong. Warn the user and don't proceed any further.
105 if (!origin)
106 for_each_string_list_item(item, remotes)
107 if (skip_prefix(item->string, "refs/heads/", &shortname)
108 && !strcmp(local, shortname)) {
109 warning(_("not setting branch '%s' as its own upstream"),
110 local);
111 return 0;
114 strbuf_addf(&key, "branch.%s.remote", local);
115 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
116 goto out_err;
118 strbuf_reset(&key);
119 strbuf_addf(&key, "branch.%s.merge", local);
121 * We want to overwrite any existing config with all the branches in
122 * "remotes". Override any existing config, then write our branches. If
123 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
124 * we've written so far.
126 if (git_config_set_gently(key.buf, NULL) < 0)
127 goto out_err;
128 for_each_string_list_item(item, remotes)
129 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
130 goto out_err;
132 if (rebasing) {
133 strbuf_reset(&key);
134 strbuf_addf(&key, "branch.%s.rebase", local);
135 if (git_config_set_gently(key.buf, "true") < 0)
136 goto out_err;
138 strbuf_release(&key);
140 if (flag & BRANCH_CONFIG_VERBOSE) {
141 struct strbuf tmp_ref_name = STRBUF_INIT;
142 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
144 for_each_string_list_item(item, remotes) {
145 shortname = item->string;
146 skip_prefix(shortname, "refs/heads/", &shortname);
147 if (origin) {
148 strbuf_addf(&tmp_ref_name, "%s/%s",
149 origin, shortname);
150 string_list_append_nodup(
151 &friendly_ref_names,
152 strbuf_detach(&tmp_ref_name, NULL));
153 } else {
154 string_list_append(
155 &friendly_ref_names, shortname);
159 if (remotes->nr == 1) {
161 * Rebasing is only allowed in the case of a single
162 * upstream branch.
164 printf_ln(rebasing ?
165 _("branch '%s' set up to track '%s' by rebasing.") :
166 _("branch '%s' set up to track '%s'."),
167 local, friendly_ref_names.items[0].string);
168 } else {
169 printf_ln(_("branch '%s' set up to track:"), local);
170 for_each_string_list_item(item, &friendly_ref_names)
171 printf_ln(" %s", item->string);
174 string_list_clear(&friendly_ref_names, 0);
177 return 0;
179 out_err:
180 strbuf_release(&key);
181 error(_("unable to write upstream branch configuration"));
183 advise(_("\nAfter fixing the error cause you may try to fix up\n"
184 "the remote tracking information by invoking:"));
185 if (remotes->nr == 1)
186 advise(" git branch --set-upstream-to=%s%s%s",
187 origin ? origin : "",
188 origin ? "/" : "",
189 remotes->items[0].string);
190 else {
191 advise(" git config --add branch.\"%s\".remote %s",
192 local, origin ? origin : ".");
193 for_each_string_list_item(item, remotes)
194 advise(" git config --add branch.\"%s\".merge %s",
195 local, item->string);
198 return -1;
201 int install_branch_config(int flag, const char *local, const char *origin,
202 const char *remote)
204 int ret;
205 struct string_list remotes = STRING_LIST_INIT_DUP;
207 string_list_append(&remotes, remote);
208 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
209 string_list_clear(&remotes, 0);
210 return ret;
213 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
215 const char *bare_ref;
216 struct branch *branch;
217 int i;
219 bare_ref = orig_ref;
220 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
222 branch = branch_get(bare_ref);
223 if (!branch->remote_name) {
224 warning(_("asked to inherit tracking from '%s', but no remote is set"),
225 bare_ref);
226 return -1;
229 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
230 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
231 bare_ref);
232 return -1;
235 tracking->remote = xstrdup(branch->remote_name);
236 for (i = 0; i < branch->merge_nr; i++)
237 string_list_append(tracking->srcs, branch->merge_name[i]);
238 return 0;
242 * Used internally to set the branch.<new_ref>.{remote,merge} config
243 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
244 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
245 * will not be expanded to "refs/remotes/origin/main", so it is not safe
246 * for 'orig_ref' to be raw user input.
248 static void setup_tracking(const char *new_ref, const char *orig_ref,
249 enum branch_track track, int quiet)
251 struct tracking tracking;
252 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
253 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
254 struct find_tracked_branch_cb ftb_cb = {
255 .tracking = &tracking,
256 .ambiguous_remotes = STRING_LIST_INIT_DUP,
259 if (!track)
260 BUG("asked to set up tracking, but tracking is disallowed");
262 memset(&tracking, 0, sizeof(tracking));
263 tracking.spec.dst = (char *)orig_ref;
264 tracking.srcs = &tracking_srcs;
265 if (track != BRANCH_TRACK_INHERIT)
266 for_each_remote(find_tracked_branch, &ftb_cb);
267 else if (inherit_tracking(&tracking, orig_ref))
268 goto cleanup;
270 if (!tracking.matches)
271 switch (track) {
272 /* If ref is not remote, still use local */
273 case BRANCH_TRACK_ALWAYS:
274 case BRANCH_TRACK_EXPLICIT:
275 case BRANCH_TRACK_OVERRIDE:
276 /* Remote matches not evaluated */
277 case BRANCH_TRACK_INHERIT:
278 break;
279 /* Otherwise, if no remote don't track */
280 default:
281 goto cleanup;
285 * This check does not apply to BRANCH_TRACK_INHERIT;
286 * that supports multiple entries in tracking_srcs but
287 * leaves tracking.matches at 0.
289 if (tracking.matches > 1) {
290 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
291 orig_ref);
292 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
293 struct strbuf remotes_advice = STRBUF_INIT;
294 struct string_list_item *item;
296 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
298 * TRANSLATORS: This is a line listing a remote with duplicate
299 * refspecs in the advice message below. For RTL languages you'll
300 * probably want to swap the "%s" and leading " " space around.
302 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
305 * TRANSLATORS: The second argument is a \n-delimited list of
306 * duplicate refspecs, composed above.
308 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
309 "tracking ref '%s':\n"
310 "%s"
311 "\n"
312 "This is typically a configuration error.\n"
313 "\n"
314 "To support setting up tracking branches, ensure that\n"
315 "different remotes' fetch refspecs map into different\n"
316 "tracking namespaces."), orig_ref,
317 remotes_advice.buf);
318 strbuf_release(&remotes_advice);
320 exit(status);
323 if (track == BRANCH_TRACK_SIMPLE) {
325 * Only track if remote branch name matches.
326 * Reaching into items[0].string is safe because
327 * we know there is at least one and not more than
328 * one entry (because only BRANCH_TRACK_INHERIT can
329 * produce more than one entry).
331 const char *tracked_branch;
332 if (!skip_prefix(tracking.srcs->items[0].string,
333 "refs/heads/", &tracked_branch) ||
334 strcmp(tracked_branch, new_ref))
335 return;
338 if (tracking.srcs->nr < 1)
339 string_list_append(tracking.srcs, orig_ref);
340 if (install_branch_config_multiple_remotes(config_flags, new_ref,
341 tracking.remote, tracking.srcs) < 0)
342 exit(1);
344 cleanup:
345 string_list_clear(&tracking_srcs, 0);
346 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
349 int read_branch_desc(struct strbuf *buf, const char *branch_name)
351 char *v = NULL;
352 struct strbuf name = STRBUF_INIT;
353 strbuf_addf(&name, "branch.%s.description", branch_name);
354 if (git_config_get_string(name.buf, &v)) {
355 strbuf_release(&name);
356 return -1;
358 strbuf_addstr(buf, v);
359 free(v);
360 strbuf_release(&name);
361 return 0;
365 * Check if 'name' can be a valid name for a branch; die otherwise.
366 * Return 1 if the named branch already exists; return 0 otherwise.
367 * Fill ref with the full refname for the branch.
369 int validate_branchname(const char *name, struct strbuf *ref)
371 if (strbuf_check_branch_ref(ref, name))
372 die(_("'%s' is not a valid branch name"), name);
374 return ref_exists(ref->buf);
377 static int initialized_checked_out_branches;
378 static struct strmap current_checked_out_branches = STRMAP_INIT;
380 static void prepare_checked_out_branches(void)
382 int i = 0;
383 struct worktree **worktrees;
385 if (initialized_checked_out_branches)
386 return;
387 initialized_checked_out_branches = 1;
389 worktrees = get_worktrees();
391 while (worktrees[i]) {
392 char *old;
393 struct wt_status_state state = { 0 };
394 struct worktree *wt = worktrees[i++];
395 struct string_list update_refs = STRING_LIST_INIT_DUP;
397 if (wt->is_bare)
398 continue;
400 if (wt->head_ref) {
401 old = strmap_put(&current_checked_out_branches,
402 wt->head_ref,
403 xstrdup(wt->path));
404 free(old);
407 if (wt_status_check_rebase(wt, &state) &&
408 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
409 state.branch) {
410 struct strbuf ref = STRBUF_INIT;
411 strbuf_addf(&ref, "refs/heads/%s", state.branch);
412 old = strmap_put(&current_checked_out_branches,
413 ref.buf,
414 xstrdup(wt->path));
415 free(old);
416 strbuf_release(&ref);
418 wt_status_state_free_buffers(&state);
420 if (wt_status_check_bisect(wt, &state) &&
421 state.branch) {
422 struct strbuf ref = STRBUF_INIT;
423 strbuf_addf(&ref, "refs/heads/%s", state.branch);
424 old = strmap_put(&current_checked_out_branches,
425 ref.buf,
426 xstrdup(wt->path));
427 free(old);
428 strbuf_release(&ref);
430 wt_status_state_free_buffers(&state);
432 if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt),
433 &update_refs)) {
434 struct string_list_item *item;
435 for_each_string_list_item(item, &update_refs) {
436 old = strmap_put(&current_checked_out_branches,
437 item->string,
438 xstrdup(wt->path));
439 free(old);
441 string_list_clear(&update_refs, 1);
445 free_worktrees(worktrees);
448 const char *branch_checked_out(const char *refname)
450 prepare_checked_out_branches();
451 return strmap_get(&current_checked_out_branches, refname);
455 * Check if a branch 'name' can be created as a new branch; die otherwise.
456 * 'force' can be used when it is OK for the named branch already exists.
457 * Return 1 if the named branch already exists; return 0 otherwise.
458 * Fill ref with the full refname for the branch.
460 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
462 const char *path;
463 if (!validate_branchname(name, ref))
464 return 0;
466 if (!force)
467 die(_("a branch named '%s' already exists"),
468 ref->buf + strlen("refs/heads/"));
470 if ((path = branch_checked_out(ref->buf)))
471 die(_("cannot force update the branch '%s' "
472 "checked out at '%s'"),
473 ref->buf + strlen("refs/heads/"), path);
475 return 1;
478 static int check_tracking_branch(struct remote *remote, void *cb_data)
480 char *tracking_branch = cb_data;
481 struct refspec_item query;
482 memset(&query, 0, sizeof(struct refspec_item));
483 query.dst = tracking_branch;
484 return !remote_find_tracking(remote, &query);
487 static int validate_remote_tracking_branch(char *ref)
489 return !for_each_remote(check_tracking_branch, ref);
492 static const char upstream_not_branch[] =
493 N_("cannot set up tracking information; starting point '%s' is not a branch");
494 static const char upstream_missing[] =
495 N_("the requested upstream branch '%s' does not exist");
496 static const char upstream_advice[] =
497 N_("\n"
498 "If you are planning on basing your work on an upstream\n"
499 "branch that already exists at the remote, you may need to\n"
500 "run \"git fetch\" to retrieve it.\n"
501 "\n"
502 "If you are planning to push out a new local branch that\n"
503 "will track its remote counterpart, you may want to use\n"
504 "\"git push -u\" to set the upstream config as you push.");
507 * DWIMs a user-provided ref to determine the starting point for a
508 * branch and validates it, where:
510 * - r is the repository to validate the branch for
512 * - start_name is the ref that we would like to test. This is
513 * expanded with DWIM and assigned to out_real_ref.
515 * - track is the tracking mode of the new branch. If tracking is
516 * explicitly requested, start_name must be a branch (because
517 * otherwise start_name cannot be tracked)
519 * - out_oid is an out parameter containing the object_id of start_name
521 * - out_real_ref is an out parameter containing the full, 'real' form
522 * of start_name e.g. refs/heads/main instead of main
525 static void dwim_branch_start(struct repository *r, const char *start_name,
526 enum branch_track track, char **out_real_ref,
527 struct object_id *out_oid)
529 struct commit *commit;
530 struct object_id oid;
531 char *real_ref;
532 int explicit_tracking = 0;
534 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
535 explicit_tracking = 1;
537 real_ref = NULL;
538 if (repo_get_oid_mb(r, start_name, &oid)) {
539 if (explicit_tracking) {
540 int code = die_message(_(upstream_missing), start_name);
541 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
542 _(upstream_advice));
543 exit(code);
545 die(_("not a valid object name: '%s'"), start_name);
548 switch (repo_dwim_ref(r, start_name, strlen(start_name), &oid,
549 &real_ref, 0)) {
550 case 0:
551 /* Not branching from any existing branch */
552 if (explicit_tracking)
553 die(_(upstream_not_branch), start_name);
554 break;
555 case 1:
556 /* Unique completion -- good, only if it is a real branch */
557 if (!starts_with(real_ref, "refs/heads/") &&
558 validate_remote_tracking_branch(real_ref)) {
559 if (explicit_tracking)
560 die(_(upstream_not_branch), start_name);
561 else
562 FREE_AND_NULL(real_ref);
564 break;
565 default:
566 die(_("ambiguous object name: '%s'"), start_name);
567 break;
570 if (!(commit = lookup_commit_reference(r, &oid)))
571 die(_("not a valid branch point: '%s'"), start_name);
572 if (out_real_ref) {
573 *out_real_ref = real_ref;
574 real_ref = NULL;
576 if (out_oid)
577 oidcpy(out_oid, &commit->object.oid);
579 FREE_AND_NULL(real_ref);
582 void create_branch(struct repository *r,
583 const char *name, const char *start_name,
584 int force, int clobber_head_ok, int reflog,
585 int quiet, enum branch_track track, int dry_run)
587 struct object_id oid;
588 char *real_ref;
589 struct strbuf ref = STRBUF_INIT;
590 int forcing = 0;
591 struct ref_transaction *transaction;
592 struct strbuf err = STRBUF_INIT;
593 char *msg;
595 if (track == BRANCH_TRACK_OVERRIDE)
596 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
597 if (clobber_head_ok && !force)
598 BUG("'clobber_head_ok' can only be used with 'force'");
600 if (clobber_head_ok ?
601 validate_branchname(name, &ref) :
602 validate_new_branchname(name, &ref, force)) {
603 forcing = 1;
606 dwim_branch_start(r, start_name, track, &real_ref, &oid);
607 if (dry_run)
608 goto cleanup;
610 if (reflog)
611 log_all_ref_updates = LOG_REFS_NORMAL;
613 if (forcing)
614 msg = xstrfmt("branch: Reset to %s", start_name);
615 else
616 msg = xstrfmt("branch: Created from %s", start_name);
617 transaction = ref_transaction_begin(&err);
618 if (!transaction ||
619 ref_transaction_update(transaction, ref.buf,
620 &oid, forcing ? NULL : null_oid(),
621 0, msg, &err) ||
622 ref_transaction_commit(transaction, &err))
623 die("%s", err.buf);
624 ref_transaction_free(transaction);
625 strbuf_release(&err);
626 free(msg);
628 if (real_ref && track)
629 setup_tracking(ref.buf + 11, real_ref, track, quiet);
631 cleanup:
632 strbuf_release(&ref);
633 free(real_ref);
636 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
637 const char *orig_ref, enum branch_track track,
638 int quiet)
640 char *real_orig_ref;
641 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
642 setup_tracking(new_ref, real_orig_ref, track, quiet);
646 * Creates a branch in a submodule by calling
647 * create_branches_recursively() in a child process. The child process
648 * is necessary because install_branch_config_multiple_remotes() (which
649 * is called by setup_tracking()) does not support writing configs to
650 * submodules.
652 static int submodule_create_branch(struct repository *r,
653 const struct submodule *submodule,
654 const char *name, const char *start_oid,
655 const char *tracking_name, int force,
656 int reflog, int quiet,
657 enum branch_track track, int dry_run)
659 int ret = 0;
660 struct child_process child = CHILD_PROCESS_INIT;
661 struct strbuf child_err = STRBUF_INIT;
662 struct strbuf out_buf = STRBUF_INIT;
663 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
664 child.git_cmd = 1;
665 child.err = -1;
666 child.stdout_to_stderr = 1;
668 prepare_other_repo_env(&child.env, r->gitdir);
670 * submodule_create_branch() is indirectly invoked by "git
671 * branch", but we cannot invoke "git branch" in the child
672 * process. "git branch" accepts a branch name and start point,
673 * where the start point is assumed to provide both the OID
674 * (start_oid) and the branch to use for tracking
675 * (tracking_name). But when recursing through submodules,
676 * start_oid and tracking name need to be specified separately
677 * (see create_branches_recursively()).
679 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
680 if (dry_run)
681 strvec_push(&child.args, "--dry-run");
682 if (force)
683 strvec_push(&child.args, "--force");
684 if (quiet)
685 strvec_push(&child.args, "--quiet");
686 if (reflog)
687 strvec_push(&child.args, "--create-reflog");
689 switch (track) {
690 case BRANCH_TRACK_NEVER:
691 strvec_push(&child.args, "--no-track");
692 break;
693 case BRANCH_TRACK_ALWAYS:
694 case BRANCH_TRACK_EXPLICIT:
695 strvec_push(&child.args, "--track=direct");
696 break;
697 case BRANCH_TRACK_OVERRIDE:
698 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
699 break;
700 case BRANCH_TRACK_INHERIT:
701 strvec_push(&child.args, "--track=inherit");
702 break;
703 case BRANCH_TRACK_UNSPECIFIED:
704 /* Default for "git checkout". Do not pass --track. */
705 case BRANCH_TRACK_REMOTE:
706 /* Default for "git branch". Do not pass --track. */
707 case BRANCH_TRACK_SIMPLE:
708 /* Config-driven only. Do not pass --track. */
709 break;
712 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
714 if ((ret = start_command(&child)))
715 return ret;
716 ret = finish_command(&child);
717 strbuf_read(&child_err, child.err, 0);
718 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
720 if (ret)
721 fprintf(stderr, "%s", out_buf.buf);
722 else
723 printf("%s", out_buf.buf);
725 strbuf_release(&child_err);
726 strbuf_release(&out_buf);
727 return ret;
730 void create_branches_recursively(struct repository *r, const char *name,
731 const char *start_commitish,
732 const char *tracking_name, int force,
733 int reflog, int quiet, enum branch_track track,
734 int dry_run)
736 int i = 0;
737 char *branch_point = NULL;
738 struct object_id super_oid;
739 struct submodule_entry_list submodule_entry_list;
741 /* Perform dwim on start_commitish to get super_oid and branch_point. */
742 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
743 &branch_point, &super_oid);
746 * If we were not given an explicit name to track, then assume we are at
747 * the top level and, just like the non-recursive case, the tracking
748 * name is the branch point.
750 if (!tracking_name)
751 tracking_name = branch_point;
753 submodules_of_tree(r, &super_oid, &submodule_entry_list);
755 * Before creating any branches, first check that the branch can
756 * be created in every submodule.
758 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
759 if (!submodule_entry_list.entries[i].repo) {
760 int code = die_message(
761 _("submodule '%s': unable to find submodule"),
762 submodule_entry_list.entries[i].submodule->name);
763 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
764 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
765 start_commitish);
766 exit(code);
769 if (submodule_create_branch(
770 submodule_entry_list.entries[i].repo,
771 submodule_entry_list.entries[i].submodule, name,
772 oid_to_hex(&submodule_entry_list.entries[i]
773 .name_entry->oid),
774 tracking_name, force, reflog, quiet, track, 1))
775 die(_("submodule '%s': cannot create branch '%s'"),
776 submodule_entry_list.entries[i].submodule->name,
777 name);
780 create_branch(r, name, start_commitish, force, 0, reflog, quiet,
781 BRANCH_TRACK_NEVER, dry_run);
782 if (dry_run)
783 return;
785 * NEEDSWORK If tracking was set up in the superproject but not the
786 * submodule, users might expect "git branch --recurse-submodules" to
787 * fail or give a warning, but this is not yet implemented because it is
788 * tedious to determine whether or not tracking was set up in the
789 * superproject.
791 if (track)
792 setup_tracking(name, tracking_name, track, quiet);
794 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
795 if (submodule_create_branch(
796 submodule_entry_list.entries[i].repo,
797 submodule_entry_list.entries[i].submodule, name,
798 oid_to_hex(&submodule_entry_list.entries[i]
799 .name_entry->oid),
800 tracking_name, force, reflog, quiet, track, 0))
801 die(_("submodule '%s': cannot create branch '%s'"),
802 submodule_entry_list.entries[i].submodule->name,
803 name);
804 repo_clear(submodule_entry_list.entries[i].repo);
808 void remove_merge_branch_state(struct repository *r)
810 unlink(git_path_merge_head(r));
811 unlink(git_path_merge_rr(r));
812 unlink(git_path_merge_msg(r));
813 unlink(git_path_merge_mode(r));
814 unlink(git_path_auto_merge(r));
815 save_autostash(git_path_merge_autostash(r));
818 void remove_branch_state(struct repository *r, int verbose)
820 sequencer_post_commit_cleanup(r, verbose);
821 unlink(git_path_squash_msg(r));
822 remove_merge_branch_state(r);
825 void die_if_checked_out(const char *branch, int ignore_current_worktree)
827 struct worktree **worktrees = get_worktrees();
829 for (int i = 0; worktrees[i]; i++) {
830 if (worktrees[i]->is_current && ignore_current_worktree)
831 continue;
833 if (is_shared_symref(worktrees[i], "HEAD", branch)) {
834 skip_prefix(branch, "refs/heads/", &branch);
835 die(_("'%s' is already checked out at '%s'"),
836 branch, worktrees[i]->path);
840 free_worktrees(worktrees);
843 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
844 const char *logmsg)
846 int ret = 0;
847 struct worktree **worktrees = get_worktrees();
848 int i;
850 for (i = 0; worktrees[i]; i++) {
851 struct ref_store *refs;
853 if (worktrees[i]->is_detached)
854 continue;
855 if (!worktrees[i]->head_ref)
856 continue;
857 if (strcmp(oldref, worktrees[i]->head_ref))
858 continue;
860 refs = get_worktree_ref_store(worktrees[i]);
861 if (refs_create_symref(refs, "HEAD", newref, logmsg))
862 ret = error(_("HEAD of working tree %s is not updated"),
863 worktrees[i]->path);
866 free_worktrees(worktrees);
867 return ret;