test-lib: add a --invert-exit-code switch
[git/debian.git] / branch.c
blob6dbd933288845ec1cd522252bd2f21e07a3481d3
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "branch.h"
5 #include "refs.h"
6 #include "refspec.h"
7 #include "remote.h"
8 #include "sequencer.h"
9 #include "commit.h"
10 #include "worktree.h"
11 #include "submodule-config.h"
12 #include "run-command.h"
13 #include "strmap.h"
15 struct tracking {
16 struct refspec_item spec;
17 struct string_list *srcs;
18 const char *remote;
19 int matches;
22 struct find_tracked_branch_cb {
23 struct tracking *tracking;
24 struct string_list ambiguous_remotes;
27 static int find_tracked_branch(struct remote *remote, void *priv)
29 struct find_tracked_branch_cb *ftb = priv;
30 struct tracking *tracking = ftb->tracking;
32 if (!remote_find_tracking(remote, &tracking->spec)) {
33 switch (++tracking->matches) {
34 case 1:
35 string_list_append(tracking->srcs, tracking->spec.src);
36 tracking->remote = remote->name;
37 break;
38 case 2:
39 /* there are at least two remotes; backfill the first one */
40 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
41 /* fall through */
42 default:
43 string_list_append(&ftb->ambiguous_remotes, remote->name);
44 free(tracking->spec.src);
45 string_list_clear(tracking->srcs, 0);
46 break;
48 /* remote_find_tracking() searches by src if present */
49 tracking->spec.src = NULL;
51 return 0;
54 static int should_setup_rebase(const char *origin)
56 switch (autorebase) {
57 case AUTOREBASE_NEVER:
58 return 0;
59 case AUTOREBASE_LOCAL:
60 return origin == NULL;
61 case AUTOREBASE_REMOTE:
62 return origin != NULL;
63 case AUTOREBASE_ALWAYS:
64 return 1;
66 return 0;
69 /**
70 * Install upstream tracking configuration for a branch; specifically, add
71 * `branch.<name>.remote` and `branch.<name>.merge` entries.
73 * `flag` contains integer flags for options; currently only
74 * BRANCH_CONFIG_VERBOSE is checked.
76 * `local` is the name of the branch whose configuration we're installing.
78 * `origin` is the name of the remote owning the upstream branches. NULL means
79 * the upstream branches are local to this repo.
81 * `remotes` is a list of refs that are upstream of local
83 static int install_branch_config_multiple_remotes(int flag, const char *local,
84 const char *origin, struct string_list *remotes)
86 const char *shortname = NULL;
87 struct strbuf key = STRBUF_INIT;
88 struct string_list_item *item;
89 int rebasing = should_setup_rebase(origin);
91 if (!remotes->nr)
92 BUG("must provide at least one remote for branch config");
93 if (rebasing && remotes->nr > 1)
94 die(_("cannot inherit upstream tracking configuration of "
95 "multiple refs when rebasing is requested"));
98 * If the new branch is trying to track itself, something has gone
99 * wrong. Warn the user and don't proceed any further.
101 if (!origin)
102 for_each_string_list_item(item, remotes)
103 if (skip_prefix(item->string, "refs/heads/", &shortname)
104 && !strcmp(local, shortname)) {
105 warning(_("not setting branch '%s' as its own upstream"),
106 local);
107 return 0;
110 strbuf_addf(&key, "branch.%s.remote", local);
111 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
112 goto out_err;
114 strbuf_reset(&key);
115 strbuf_addf(&key, "branch.%s.merge", local);
117 * We want to overwrite any existing config with all the branches in
118 * "remotes". Override any existing config, then write our branches. If
119 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
120 * we've written so far.
122 if (git_config_set_gently(key.buf, NULL) < 0)
123 goto out_err;
124 for_each_string_list_item(item, remotes)
125 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
126 goto out_err;
128 if (rebasing) {
129 strbuf_reset(&key);
130 strbuf_addf(&key, "branch.%s.rebase", local);
131 if (git_config_set_gently(key.buf, "true") < 0)
132 goto out_err;
134 strbuf_release(&key);
136 if (flag & BRANCH_CONFIG_VERBOSE) {
137 struct strbuf tmp_ref_name = STRBUF_INIT;
138 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
140 for_each_string_list_item(item, remotes) {
141 shortname = item->string;
142 skip_prefix(shortname, "refs/heads/", &shortname);
143 if (origin) {
144 strbuf_addf(&tmp_ref_name, "%s/%s",
145 origin, shortname);
146 string_list_append_nodup(
147 &friendly_ref_names,
148 strbuf_detach(&tmp_ref_name, NULL));
149 } else {
150 string_list_append(
151 &friendly_ref_names, shortname);
155 if (remotes->nr == 1) {
157 * Rebasing is only allowed in the case of a single
158 * upstream branch.
160 printf_ln(rebasing ?
161 _("branch '%s' set up to track '%s' by rebasing.") :
162 _("branch '%s' set up to track '%s'."),
163 local, friendly_ref_names.items[0].string);
164 } else {
165 printf_ln(_("branch '%s' set up to track:"), local);
166 for_each_string_list_item(item, &friendly_ref_names)
167 printf_ln(" %s", item->string);
170 string_list_clear(&friendly_ref_names, 0);
173 return 0;
175 out_err:
176 strbuf_release(&key);
177 error(_("unable to write upstream branch configuration"));
179 advise(_("\nAfter fixing the error cause you may try to fix up\n"
180 "the remote tracking information by invoking:"));
181 if (remotes->nr == 1)
182 advise(" git branch --set-upstream-to=%s%s%s",
183 origin ? origin : "",
184 origin ? "/" : "",
185 remotes->items[0].string);
186 else {
187 advise(" git config --add branch.\"%s\".remote %s",
188 local, origin ? origin : ".");
189 for_each_string_list_item(item, remotes)
190 advise(" git config --add branch.\"%s\".merge %s",
191 local, item->string);
194 return -1;
197 int install_branch_config(int flag, const char *local, const char *origin,
198 const char *remote)
200 int ret;
201 struct string_list remotes = STRING_LIST_INIT_DUP;
203 string_list_append(&remotes, remote);
204 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
205 string_list_clear(&remotes, 0);
206 return ret;
209 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
211 const char *bare_ref;
212 struct branch *branch;
213 int i;
215 bare_ref = orig_ref;
216 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
218 branch = branch_get(bare_ref);
219 if (!branch->remote_name) {
220 warning(_("asked to inherit tracking from '%s', but no remote is set"),
221 bare_ref);
222 return -1;
225 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
226 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
227 bare_ref);
228 return -1;
231 tracking->remote = xstrdup(branch->remote_name);
232 for (i = 0; i < branch->merge_nr; i++)
233 string_list_append(tracking->srcs, branch->merge_name[i]);
234 return 0;
238 * Used internally to set the branch.<new_ref>.{remote,merge} config
239 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
240 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
241 * will not be expanded to "refs/remotes/origin/main", so it is not safe
242 * for 'orig_ref' to be raw user input.
244 static void setup_tracking(const char *new_ref, const char *orig_ref,
245 enum branch_track track, int quiet)
247 struct tracking tracking;
248 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
249 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
250 struct find_tracked_branch_cb ftb_cb = {
251 .tracking = &tracking,
252 .ambiguous_remotes = STRING_LIST_INIT_DUP,
255 if (!track)
256 BUG("asked to set up tracking, but tracking is disallowed");
258 memset(&tracking, 0, sizeof(tracking));
259 tracking.spec.dst = (char *)orig_ref;
260 tracking.srcs = &tracking_srcs;
261 if (track != BRANCH_TRACK_INHERIT)
262 for_each_remote(find_tracked_branch, &ftb_cb);
263 else if (inherit_tracking(&tracking, orig_ref))
264 goto cleanup;
266 if (!tracking.matches)
267 switch (track) {
268 /* If ref is not remote, still use local */
269 case BRANCH_TRACK_ALWAYS:
270 case BRANCH_TRACK_EXPLICIT:
271 case BRANCH_TRACK_OVERRIDE:
272 /* Remote matches not evaluated */
273 case BRANCH_TRACK_INHERIT:
274 break;
275 /* Otherwise, if no remote don't track */
276 default:
277 goto cleanup;
281 * This check does not apply to BRANCH_TRACK_INHERIT;
282 * that supports multiple entries in tracking_srcs but
283 * leaves tracking.matches at 0.
285 if (tracking.matches > 1) {
286 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
287 orig_ref);
288 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
289 struct strbuf remotes_advice = STRBUF_INIT;
290 struct string_list_item *item;
292 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
294 * TRANSLATORS: This is a line listing a remote with duplicate
295 * refspecs in the advice message below. For RTL languages you'll
296 * probably want to swap the "%s" and leading " " space around.
298 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
301 * TRANSLATORS: The second argument is a \n-delimited list of
302 * duplicate refspecs, composed above.
304 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
305 "tracking ref '%s':\n"
306 "%s"
307 "\n"
308 "This is typically a configuration error.\n"
309 "\n"
310 "To support setting up tracking branches, ensure that\n"
311 "different remotes' fetch refspecs map into different\n"
312 "tracking namespaces."), orig_ref,
313 remotes_advice.buf);
314 strbuf_release(&remotes_advice);
316 exit(status);
319 if (track == BRANCH_TRACK_SIMPLE) {
321 * Only track if remote branch name matches.
322 * Reaching into items[0].string is safe because
323 * we know there is at least one and not more than
324 * one entry (because only BRANCH_TRACK_INHERIT can
325 * produce more than one entry).
327 const char *tracked_branch;
328 if (!skip_prefix(tracking.srcs->items[0].string,
329 "refs/heads/", &tracked_branch) ||
330 strcmp(tracked_branch, new_ref))
331 return;
334 if (tracking.srcs->nr < 1)
335 string_list_append(tracking.srcs, orig_ref);
336 if (install_branch_config_multiple_remotes(config_flags, new_ref,
337 tracking.remote, tracking.srcs) < 0)
338 exit(1);
340 cleanup:
341 string_list_clear(&tracking_srcs, 0);
342 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
345 int read_branch_desc(struct strbuf *buf, const char *branch_name)
347 char *v = NULL;
348 struct strbuf name = STRBUF_INIT;
349 strbuf_addf(&name, "branch.%s.description", branch_name);
350 if (git_config_get_string(name.buf, &v)) {
351 strbuf_release(&name);
352 return -1;
354 strbuf_addstr(buf, v);
355 free(v);
356 strbuf_release(&name);
357 return 0;
361 * Check if 'name' can be a valid name for a branch; die otherwise.
362 * Return 1 if the named branch already exists; return 0 otherwise.
363 * Fill ref with the full refname for the branch.
365 int validate_branchname(const char *name, struct strbuf *ref)
367 if (strbuf_check_branch_ref(ref, name))
368 die(_("'%s' is not a valid branch name"), name);
370 return ref_exists(ref->buf);
373 static int initialized_checked_out_branches;
374 static struct strmap current_checked_out_branches = STRMAP_INIT;
376 static void prepare_checked_out_branches(void)
378 int i = 0;
379 struct worktree **worktrees;
381 if (initialized_checked_out_branches)
382 return;
383 initialized_checked_out_branches = 1;
385 worktrees = get_worktrees();
387 while (worktrees[i]) {
388 char *old;
389 struct wt_status_state state = { 0 };
390 struct worktree *wt = worktrees[i++];
392 if (wt->is_bare)
393 continue;
395 if (wt->head_ref) {
396 old = strmap_put(&current_checked_out_branches,
397 wt->head_ref,
398 xstrdup(wt->path));
399 free(old);
402 if (wt_status_check_rebase(wt, &state) &&
403 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
404 state.branch) {
405 struct strbuf ref = STRBUF_INIT;
406 strbuf_addf(&ref, "refs/heads/%s", state.branch);
407 old = strmap_put(&current_checked_out_branches,
408 ref.buf,
409 xstrdup(wt->path));
410 free(old);
411 strbuf_release(&ref);
413 wt_status_state_free_buffers(&state);
415 if (wt_status_check_bisect(wt, &state) &&
416 state.branch) {
417 struct strbuf ref = STRBUF_INIT;
418 strbuf_addf(&ref, "refs/heads/%s", state.branch);
419 old = strmap_put(&current_checked_out_branches,
420 ref.buf,
421 xstrdup(wt->path));
422 free(old);
423 strbuf_release(&ref);
425 wt_status_state_free_buffers(&state);
428 free_worktrees(worktrees);
431 const char *branch_checked_out(const char *refname)
433 prepare_checked_out_branches();
434 return strmap_get(&current_checked_out_branches, refname);
438 * Check if a branch 'name' can be created as a new branch; die otherwise.
439 * 'force' can be used when it is OK for the named branch already exists.
440 * Return 1 if the named branch already exists; return 0 otherwise.
441 * Fill ref with the full refname for the branch.
443 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
445 const char *path;
446 if (!validate_branchname(name, ref))
447 return 0;
449 if (!force)
450 die(_("a branch named '%s' already exists"),
451 ref->buf + strlen("refs/heads/"));
453 if ((path = branch_checked_out(ref->buf)))
454 die(_("cannot force update the branch '%s' "
455 "checked out at '%s'"),
456 ref->buf + strlen("refs/heads/"), path);
458 return 1;
461 static int check_tracking_branch(struct remote *remote, void *cb_data)
463 char *tracking_branch = cb_data;
464 struct refspec_item query;
465 memset(&query, 0, sizeof(struct refspec_item));
466 query.dst = tracking_branch;
467 return !remote_find_tracking(remote, &query);
470 static int validate_remote_tracking_branch(char *ref)
472 return !for_each_remote(check_tracking_branch, ref);
475 static const char upstream_not_branch[] =
476 N_("cannot set up tracking information; starting point '%s' is not a branch");
477 static const char upstream_missing[] =
478 N_("the requested upstream branch '%s' does not exist");
479 static const char upstream_advice[] =
480 N_("\n"
481 "If you are planning on basing your work on an upstream\n"
482 "branch that already exists at the remote, you may need to\n"
483 "run \"git fetch\" to retrieve it.\n"
484 "\n"
485 "If you are planning to push out a new local branch that\n"
486 "will track its remote counterpart, you may want to use\n"
487 "\"git push -u\" to set the upstream config as you push.");
490 * DWIMs a user-provided ref to determine the starting point for a
491 * branch and validates it, where:
493 * - r is the repository to validate the branch for
495 * - start_name is the ref that we would like to test. This is
496 * expanded with DWIM and assigned to out_real_ref.
498 * - track is the tracking mode of the new branch. If tracking is
499 * explicitly requested, start_name must be a branch (because
500 * otherwise start_name cannot be tracked)
502 * - out_oid is an out parameter containing the object_id of start_name
504 * - out_real_ref is an out parameter containing the full, 'real' form
505 * of start_name e.g. refs/heads/main instead of main
508 static void dwim_branch_start(struct repository *r, const char *start_name,
509 enum branch_track track, char **out_real_ref,
510 struct object_id *out_oid)
512 struct commit *commit;
513 struct object_id oid;
514 char *real_ref;
515 int explicit_tracking = 0;
517 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
518 explicit_tracking = 1;
520 real_ref = NULL;
521 if (get_oid_mb(start_name, &oid)) {
522 if (explicit_tracking) {
523 int code = die_message(_(upstream_missing), start_name);
524 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
525 _(upstream_advice));
526 exit(code);
528 die(_("not a valid object name: '%s'"), start_name);
531 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
532 case 0:
533 /* Not branching from any existing branch */
534 if (explicit_tracking)
535 die(_(upstream_not_branch), start_name);
536 break;
537 case 1:
538 /* Unique completion -- good, only if it is a real branch */
539 if (!starts_with(real_ref, "refs/heads/") &&
540 validate_remote_tracking_branch(real_ref)) {
541 if (explicit_tracking)
542 die(_(upstream_not_branch), start_name);
543 else
544 FREE_AND_NULL(real_ref);
546 break;
547 default:
548 die(_("ambiguous object name: '%s'"), start_name);
549 break;
552 if (!(commit = lookup_commit_reference(r, &oid)))
553 die(_("not a valid branch point: '%s'"), start_name);
554 if (out_real_ref) {
555 *out_real_ref = real_ref;
556 real_ref = NULL;
558 if (out_oid)
559 oidcpy(out_oid, &commit->object.oid);
561 FREE_AND_NULL(real_ref);
564 void create_branch(struct repository *r,
565 const char *name, const char *start_name,
566 int force, int clobber_head_ok, int reflog,
567 int quiet, enum branch_track track, int dry_run)
569 struct object_id oid;
570 char *real_ref;
571 struct strbuf ref = STRBUF_INIT;
572 int forcing = 0;
573 struct ref_transaction *transaction;
574 struct strbuf err = STRBUF_INIT;
575 char *msg;
577 if (track == BRANCH_TRACK_OVERRIDE)
578 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
579 if (clobber_head_ok && !force)
580 BUG("'clobber_head_ok' can only be used with 'force'");
582 if (clobber_head_ok ?
583 validate_branchname(name, &ref) :
584 validate_new_branchname(name, &ref, force)) {
585 forcing = 1;
588 dwim_branch_start(r, start_name, track, &real_ref, &oid);
589 if (dry_run)
590 goto cleanup;
592 if (reflog)
593 log_all_ref_updates = LOG_REFS_NORMAL;
595 if (forcing)
596 msg = xstrfmt("branch: Reset to %s", start_name);
597 else
598 msg = xstrfmt("branch: Created from %s", start_name);
599 transaction = ref_transaction_begin(&err);
600 if (!transaction ||
601 ref_transaction_update(transaction, ref.buf,
602 &oid, forcing ? NULL : null_oid(),
603 0, msg, &err) ||
604 ref_transaction_commit(transaction, &err))
605 die("%s", err.buf);
606 ref_transaction_free(transaction);
607 strbuf_release(&err);
608 free(msg);
610 if (real_ref && track)
611 setup_tracking(ref.buf + 11, real_ref, track, quiet);
613 cleanup:
614 strbuf_release(&ref);
615 free(real_ref);
618 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
619 const char *orig_ref, enum branch_track track,
620 int quiet)
622 char *real_orig_ref;
623 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
624 setup_tracking(new_ref, real_orig_ref, track, quiet);
628 * Creates a branch in a submodule by calling
629 * create_branches_recursively() in a child process. The child process
630 * is necessary because install_branch_config_multiple_remotes() (which
631 * is called by setup_tracking()) does not support writing configs to
632 * submodules.
634 static int submodule_create_branch(struct repository *r,
635 const struct submodule *submodule,
636 const char *name, const char *start_oid,
637 const char *tracking_name, int force,
638 int reflog, int quiet,
639 enum branch_track track, int dry_run)
641 int ret = 0;
642 struct child_process child = CHILD_PROCESS_INIT;
643 struct strbuf child_err = STRBUF_INIT;
644 struct strbuf out_buf = STRBUF_INIT;
645 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
646 child.git_cmd = 1;
647 child.err = -1;
648 child.stdout_to_stderr = 1;
650 prepare_other_repo_env(&child.env, r->gitdir);
652 * submodule_create_branch() is indirectly invoked by "git
653 * branch", but we cannot invoke "git branch" in the child
654 * process. "git branch" accepts a branch name and start point,
655 * where the start point is assumed to provide both the OID
656 * (start_oid) and the branch to use for tracking
657 * (tracking_name). But when recursing through submodules,
658 * start_oid and tracking name need to be specified separately
659 * (see create_branches_recursively()).
661 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
662 if (dry_run)
663 strvec_push(&child.args, "--dry-run");
664 if (force)
665 strvec_push(&child.args, "--force");
666 if (quiet)
667 strvec_push(&child.args, "--quiet");
668 if (reflog)
669 strvec_push(&child.args, "--create-reflog");
671 switch (track) {
672 case BRANCH_TRACK_NEVER:
673 strvec_push(&child.args, "--no-track");
674 break;
675 case BRANCH_TRACK_ALWAYS:
676 case BRANCH_TRACK_EXPLICIT:
677 strvec_push(&child.args, "--track=direct");
678 break;
679 case BRANCH_TRACK_OVERRIDE:
680 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
681 break;
682 case BRANCH_TRACK_INHERIT:
683 strvec_push(&child.args, "--track=inherit");
684 break;
685 case BRANCH_TRACK_UNSPECIFIED:
686 /* Default for "git checkout". Do not pass --track. */
687 case BRANCH_TRACK_REMOTE:
688 /* Default for "git branch". Do not pass --track. */
689 case BRANCH_TRACK_SIMPLE:
690 /* Config-driven only. Do not pass --track. */
691 break;
694 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
696 if ((ret = start_command(&child)))
697 return ret;
698 ret = finish_command(&child);
699 strbuf_read(&child_err, child.err, 0);
700 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
702 if (ret)
703 fprintf(stderr, "%s", out_buf.buf);
704 else
705 printf("%s", out_buf.buf);
707 strbuf_release(&child_err);
708 strbuf_release(&out_buf);
709 return ret;
712 void create_branches_recursively(struct repository *r, const char *name,
713 const char *start_commitish,
714 const char *tracking_name, int force,
715 int reflog, int quiet, enum branch_track track,
716 int dry_run)
718 int i = 0;
719 char *branch_point = NULL;
720 struct object_id super_oid;
721 struct submodule_entry_list submodule_entry_list;
723 /* Perform dwim on start_commitish to get super_oid and branch_point. */
724 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
725 &branch_point, &super_oid);
728 * If we were not given an explicit name to track, then assume we are at
729 * the top level and, just like the non-recursive case, the tracking
730 * name is the branch point.
732 if (!tracking_name)
733 tracking_name = branch_point;
735 submodules_of_tree(r, &super_oid, &submodule_entry_list);
737 * Before creating any branches, first check that the branch can
738 * be created in every submodule.
740 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
741 if (!submodule_entry_list.entries[i].repo) {
742 int code = die_message(
743 _("submodule '%s': unable to find submodule"),
744 submodule_entry_list.entries[i].submodule->name);
745 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
746 advise(_("You may try updating the submodules using 'git checkout %s && git submodule update --init'"),
747 start_commitish);
748 exit(code);
751 if (submodule_create_branch(
752 submodule_entry_list.entries[i].repo,
753 submodule_entry_list.entries[i].submodule, name,
754 oid_to_hex(&submodule_entry_list.entries[i]
755 .name_entry->oid),
756 tracking_name, force, reflog, quiet, track, 1))
757 die(_("submodule '%s': cannot create branch '%s'"),
758 submodule_entry_list.entries[i].submodule->name,
759 name);
762 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
763 BRANCH_TRACK_NEVER, dry_run);
764 if (dry_run)
765 return;
767 * NEEDSWORK If tracking was set up in the superproject but not the
768 * submodule, users might expect "git branch --recurse-submodules" to
769 * fail or give a warning, but this is not yet implemented because it is
770 * tedious to determine whether or not tracking was set up in the
771 * superproject.
773 if (track)
774 setup_tracking(name, tracking_name, track, quiet);
776 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
777 if (submodule_create_branch(
778 submodule_entry_list.entries[i].repo,
779 submodule_entry_list.entries[i].submodule, name,
780 oid_to_hex(&submodule_entry_list.entries[i]
781 .name_entry->oid),
782 tracking_name, force, reflog, quiet, track, 0))
783 die(_("submodule '%s': cannot create branch '%s'"),
784 submodule_entry_list.entries[i].submodule->name,
785 name);
786 repo_clear(submodule_entry_list.entries[i].repo);
790 void remove_merge_branch_state(struct repository *r)
792 unlink(git_path_merge_head(r));
793 unlink(git_path_merge_rr(r));
794 unlink(git_path_merge_msg(r));
795 unlink(git_path_merge_mode(r));
796 unlink(git_path_auto_merge(r));
797 save_autostash(git_path_merge_autostash(r));
800 void remove_branch_state(struct repository *r, int verbose)
802 sequencer_post_commit_cleanup(r, verbose);
803 unlink(git_path_squash_msg(r));
804 remove_merge_branch_state(r);
807 void die_if_checked_out(const char *branch, int ignore_current_worktree)
809 struct worktree **worktrees = get_worktrees();
810 const struct worktree *wt;
812 wt = find_shared_symref(worktrees, "HEAD", branch);
813 if (wt && (!ignore_current_worktree || !wt->is_current)) {
814 skip_prefix(branch, "refs/heads/", &branch);
815 die(_("'%s' is already checked out at '%s'"), branch, wt->path);
818 free_worktrees(worktrees);
821 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
822 const char *logmsg)
824 int ret = 0;
825 struct worktree **worktrees = get_worktrees();
826 int i;
828 for (i = 0; worktrees[i]; i++) {
829 struct ref_store *refs;
831 if (worktrees[i]->is_detached)
832 continue;
833 if (!worktrees[i]->head_ref)
834 continue;
835 if (strcmp(oldref, worktrees[i]->head_ref))
836 continue;
838 refs = get_worktree_ref_store(worktrees[i]);
839 if (refs_create_symref(refs, "HEAD", newref, logmsg))
840 ret = error(_("HEAD of working tree %s is not updated"),
841 worktrees[i]->path);
844 free_worktrees(worktrees);
845 return ret;