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