setup.h: move declarations for setup.c functions from cache.h
[git.git] / branch.c
blob3865bdbc87c33e12e09919f44490ee3e41c21080
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 (get_oid_mb(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 (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
548 case 0:
549 /* Not branching from any existing branch */
550 if (explicit_tracking)
551 die(_(upstream_not_branch), start_name);
552 break;
553 case 1:
554 /* Unique completion -- good, only if it is a real branch */
555 if (!starts_with(real_ref, "refs/heads/") &&
556 validate_remote_tracking_branch(real_ref)) {
557 if (explicit_tracking)
558 die(_(upstream_not_branch), start_name);
559 else
560 FREE_AND_NULL(real_ref);
562 break;
563 default:
564 die(_("ambiguous object name: '%s'"), start_name);
565 break;
568 if (!(commit = lookup_commit_reference(r, &oid)))
569 die(_("not a valid branch point: '%s'"), start_name);
570 if (out_real_ref) {
571 *out_real_ref = real_ref;
572 real_ref = NULL;
574 if (out_oid)
575 oidcpy(out_oid, &commit->object.oid);
577 FREE_AND_NULL(real_ref);
580 void create_branch(struct repository *r,
581 const char *name, const char *start_name,
582 int force, int clobber_head_ok, int reflog,
583 int quiet, enum branch_track track, int dry_run)
585 struct object_id oid;
586 char *real_ref;
587 struct strbuf ref = STRBUF_INIT;
588 int forcing = 0;
589 struct ref_transaction *transaction;
590 struct strbuf err = STRBUF_INIT;
591 char *msg;
593 if (track == BRANCH_TRACK_OVERRIDE)
594 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
595 if (clobber_head_ok && !force)
596 BUG("'clobber_head_ok' can only be used with 'force'");
598 if (clobber_head_ok ?
599 validate_branchname(name, &ref) :
600 validate_new_branchname(name, &ref, force)) {
601 forcing = 1;
604 dwim_branch_start(r, start_name, track, &real_ref, &oid);
605 if (dry_run)
606 goto cleanup;
608 if (reflog)
609 log_all_ref_updates = LOG_REFS_NORMAL;
611 if (forcing)
612 msg = xstrfmt("branch: Reset to %s", start_name);
613 else
614 msg = xstrfmt("branch: Created from %s", start_name);
615 transaction = ref_transaction_begin(&err);
616 if (!transaction ||
617 ref_transaction_update(transaction, ref.buf,
618 &oid, forcing ? NULL : null_oid(),
619 0, msg, &err) ||
620 ref_transaction_commit(transaction, &err))
621 die("%s", err.buf);
622 ref_transaction_free(transaction);
623 strbuf_release(&err);
624 free(msg);
626 if (real_ref && track)
627 setup_tracking(ref.buf + 11, real_ref, track, quiet);
629 cleanup:
630 strbuf_release(&ref);
631 free(real_ref);
634 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
635 const char *orig_ref, enum branch_track track,
636 int quiet)
638 char *real_orig_ref;
639 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
640 setup_tracking(new_ref, real_orig_ref, track, quiet);
644 * Creates a branch in a submodule by calling
645 * create_branches_recursively() in a child process. The child process
646 * is necessary because install_branch_config_multiple_remotes() (which
647 * is called by setup_tracking()) does not support writing configs to
648 * submodules.
650 static int submodule_create_branch(struct repository *r,
651 const struct submodule *submodule,
652 const char *name, const char *start_oid,
653 const char *tracking_name, int force,
654 int reflog, int quiet,
655 enum branch_track track, int dry_run)
657 int ret = 0;
658 struct child_process child = CHILD_PROCESS_INIT;
659 struct strbuf child_err = STRBUF_INIT;
660 struct strbuf out_buf = STRBUF_INIT;
661 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
662 child.git_cmd = 1;
663 child.err = -1;
664 child.stdout_to_stderr = 1;
666 prepare_other_repo_env(&child.env, r->gitdir);
668 * submodule_create_branch() is indirectly invoked by "git
669 * branch", but we cannot invoke "git branch" in the child
670 * process. "git branch" accepts a branch name and start point,
671 * where the start point is assumed to provide both the OID
672 * (start_oid) and the branch to use for tracking
673 * (tracking_name). But when recursing through submodules,
674 * start_oid and tracking name need to be specified separately
675 * (see create_branches_recursively()).
677 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
678 if (dry_run)
679 strvec_push(&child.args, "--dry-run");
680 if (force)
681 strvec_push(&child.args, "--force");
682 if (quiet)
683 strvec_push(&child.args, "--quiet");
684 if (reflog)
685 strvec_push(&child.args, "--create-reflog");
687 switch (track) {
688 case BRANCH_TRACK_NEVER:
689 strvec_push(&child.args, "--no-track");
690 break;
691 case BRANCH_TRACK_ALWAYS:
692 case BRANCH_TRACK_EXPLICIT:
693 strvec_push(&child.args, "--track=direct");
694 break;
695 case BRANCH_TRACK_OVERRIDE:
696 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
697 break;
698 case BRANCH_TRACK_INHERIT:
699 strvec_push(&child.args, "--track=inherit");
700 break;
701 case BRANCH_TRACK_UNSPECIFIED:
702 /* Default for "git checkout". Do not pass --track. */
703 case BRANCH_TRACK_REMOTE:
704 /* Default for "git branch". Do not pass --track. */
705 case BRANCH_TRACK_SIMPLE:
706 /* Config-driven only. Do not pass --track. */
707 break;
710 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
712 if ((ret = start_command(&child)))
713 return ret;
714 ret = finish_command(&child);
715 strbuf_read(&child_err, child.err, 0);
716 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
718 if (ret)
719 fprintf(stderr, "%s", out_buf.buf);
720 else
721 printf("%s", out_buf.buf);
723 strbuf_release(&child_err);
724 strbuf_release(&out_buf);
725 return ret;
728 void create_branches_recursively(struct repository *r, const char *name,
729 const char *start_commitish,
730 const char *tracking_name, int force,
731 int reflog, int quiet, enum branch_track track,
732 int dry_run)
734 int i = 0;
735 char *branch_point = NULL;
736 struct object_id super_oid;
737 struct submodule_entry_list submodule_entry_list;
739 /* Perform dwim on start_commitish to get super_oid and branch_point. */
740 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
741 &branch_point, &super_oid);
744 * If we were not given an explicit name to track, then assume we are at
745 * the top level and, just like the non-recursive case, the tracking
746 * name is the branch point.
748 if (!tracking_name)
749 tracking_name = branch_point;
751 submodules_of_tree(r, &super_oid, &submodule_entry_list);
753 * Before creating any branches, first check that the branch can
754 * be created in every submodule.
756 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
757 if (!submodule_entry_list.entries[i].repo) {
758 int code = die_message(
759 _("submodule '%s': unable to find submodule"),
760 submodule_entry_list.entries[i].submodule->name);
761 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
762 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
763 start_commitish);
764 exit(code);
767 if (submodule_create_branch(
768 submodule_entry_list.entries[i].repo,
769 submodule_entry_list.entries[i].submodule, name,
770 oid_to_hex(&submodule_entry_list.entries[i]
771 .name_entry->oid),
772 tracking_name, force, reflog, quiet, track, 1))
773 die(_("submodule '%s': cannot create branch '%s'"),
774 submodule_entry_list.entries[i].submodule->name,
775 name);
778 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
779 BRANCH_TRACK_NEVER, dry_run);
780 if (dry_run)
781 return;
783 * NEEDSWORK If tracking was set up in the superproject but not the
784 * submodule, users might expect "git branch --recurse-submodules" to
785 * fail or give a warning, but this is not yet implemented because it is
786 * tedious to determine whether or not tracking was set up in the
787 * superproject.
789 if (track)
790 setup_tracking(name, tracking_name, track, quiet);
792 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
793 if (submodule_create_branch(
794 submodule_entry_list.entries[i].repo,
795 submodule_entry_list.entries[i].submodule, name,
796 oid_to_hex(&submodule_entry_list.entries[i]
797 .name_entry->oid),
798 tracking_name, force, reflog, quiet, track, 0))
799 die(_("submodule '%s': cannot create branch '%s'"),
800 submodule_entry_list.entries[i].submodule->name,
801 name);
802 repo_clear(submodule_entry_list.entries[i].repo);
806 void remove_merge_branch_state(struct repository *r)
808 unlink(git_path_merge_head(r));
809 unlink(git_path_merge_rr(r));
810 unlink(git_path_merge_msg(r));
811 unlink(git_path_merge_mode(r));
812 unlink(git_path_auto_merge(r));
813 save_autostash(git_path_merge_autostash(r));
816 void remove_branch_state(struct repository *r, int verbose)
818 sequencer_post_commit_cleanup(r, verbose);
819 unlink(git_path_squash_msg(r));
820 remove_merge_branch_state(r);
823 void die_if_checked_out(const char *branch, int ignore_current_worktree)
825 struct worktree **worktrees = get_worktrees();
827 for (int i = 0; worktrees[i]; i++) {
828 if (worktrees[i]->is_current && ignore_current_worktree)
829 continue;
831 if (is_shared_symref(worktrees[i], "HEAD", branch)) {
832 skip_prefix(branch, "refs/heads/", &branch);
833 die(_("'%s' is already checked out at '%s'"),
834 branch, worktrees[i]->path);
838 free_worktrees(worktrees);
841 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
842 const char *logmsg)
844 int ret = 0;
845 struct worktree **worktrees = get_worktrees();
846 int i;
848 for (i = 0; worktrees[i]; i++) {
849 struct ref_store *refs;
851 if (worktrees[i]->is_detached)
852 continue;
853 if (!worktrees[i]->head_ref)
854 continue;
855 if (strcmp(oldref, worktrees[i]->head_ref))
856 continue;
858 refs = get_worktree_ref_store(worktrees[i]);
859 if (refs_create_symref(refs, "HEAD", newref, logmsg))
860 ret = error(_("HEAD of working tree %s is not updated"),
861 worktrees[i]->path);
864 free_worktrees(worktrees);
865 return ret;