1 #include "git-compat-util.h"
5 #include "environment.h"
11 #include "sequencer.h"
14 #include "submodule-config.h"
15 #include "run-command.h"
19 struct refspec_item spec
;
20 struct string_list
*srcs
;
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
) {
38 string_list_append(tracking
->srcs
, tracking
->spec
.src
);
39 tracking
->remote
= remote
->name
;
42 /* there are at least two remotes; backfill the first one */
43 string_list_append(&ftb
->ambiguous_remotes
, tracking
->remote
);
46 string_list_append(&ftb
->ambiguous_remotes
, remote
->name
);
47 free(tracking
->spec
.src
);
48 string_list_clear(tracking
->srcs
, 0);
51 /* remote_find_tracking() searches by src if present */
52 tracking
->spec
.src
= NULL
;
57 static int should_setup_rebase(const char *origin
)
60 case AUTOREBASE_NEVER
:
62 case AUTOREBASE_LOCAL
:
63 return origin
== NULL
;
64 case AUTOREBASE_REMOTE
:
65 return origin
!= NULL
;
66 case AUTOREBASE_ALWAYS
:
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
);
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.
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"),
113 strbuf_addf(&key
, "branch.%s.remote", local
);
114 if (git_config_set_gently(key
.buf
, origin
? origin
: ".") < 0)
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)
127 for_each_string_list_item(item
, remotes
)
128 if (git_config_set_multivar_gently(key
.buf
, item
->string
, CONFIG_REGEX_NONE
, 0) < 0)
133 strbuf_addf(&key
, "branch.%s.rebase", local
);
134 if (git_config_set_gently(key
.buf
, "true") < 0)
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
);
147 strbuf_addf(&tmp_ref_name
, "%s/%s",
149 string_list_append_nodup(
151 strbuf_detach(&tmp_ref_name
, NULL
));
154 &friendly_ref_names
, shortname
);
158 if (remotes
->nr
== 1) {
160 * Rebasing is only allowed in the case of a single
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
);
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);
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
: "",
188 remotes
->items
[0].string
);
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
);
200 int install_branch_config(int flag
, const char *local
, const char *origin
,
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);
212 static int inherit_tracking(struct tracking
*tracking
, const char *orig_ref
)
214 const char *bare_ref
;
215 struct branch
*branch
;
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"),
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"),
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
]);
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
,
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
))
269 if (!tracking
.matches
)
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
:
278 /* Otherwise, if no remote don't track */
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'"),
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"
311 "This is typically a configuration error.\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
,
317 strbuf_release(&remotes_advice
);
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
))
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)
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
)
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
);
357 strbuf_addstr(buf
, v
);
359 strbuf_release(&name
);
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)
382 struct worktree
**worktrees
;
384 if (initialized_checked_out_branches
)
386 initialized_checked_out_branches
= 1;
388 worktrees
= get_worktrees();
390 while (worktrees
[i
]) {
392 struct wt_status_state state
= { 0 };
393 struct worktree
*wt
= worktrees
[i
++];
394 struct string_list update_refs
= STRING_LIST_INIT_DUP
;
400 old
= strmap_put(¤t_checked_out_branches
,
406 if (wt_status_check_rebase(wt
, &state
) &&
407 (state
.rebase_in_progress
|| state
.rebase_interactive_in_progress
) &&
409 struct strbuf ref
= STRBUF_INIT
;
410 strbuf_addf(&ref
, "refs/heads/%s", state
.branch
);
411 old
= strmap_put(¤t_checked_out_branches
,
415 strbuf_release(&ref
);
417 wt_status_state_free_buffers(&state
);
419 if (wt_status_check_bisect(wt
, &state
) &&
421 struct strbuf ref
= STRBUF_INIT
;
422 strbuf_addf(&ref
, "refs/heads/%s", state
.branch
);
423 old
= strmap_put(¤t_checked_out_branches
,
427 strbuf_release(&ref
);
429 wt_status_state_free_buffers(&state
);
431 if (!sequencer_get_update_refs_state(get_worktree_git_dir(wt
),
433 struct string_list_item
*item
;
434 for_each_string_list_item(item
, &update_refs
) {
435 old
= strmap_put(¤t_checked_out_branches
,
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(¤t_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
)
462 if (!validate_branchname(name
, ref
))
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
);
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
[] =
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"
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
;
531 int explicit_tracking
= 0;
533 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
534 explicit_tracking
= 1;
537 if (repo_get_oid_mb(r
, start_name
, &oid
)) {
538 if (explicit_tracking
) {
539 int code
= die_message(_(upstream_missing
), start_name
);
540 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE
,
544 die(_("not a valid object name: '%s'"), start_name
);
547 switch (repo_dwim_ref(r
, start_name
, strlen(start_name
), &oid
,
550 /* Not branching from any existing branch */
551 if (explicit_tracking
)
552 die(_(upstream_not_branch
), start_name
);
555 /* Unique completion -- good, only if it is a real branch */
556 if (!starts_with(real_ref
, "refs/heads/") &&
557 validate_remote_tracking_branch(real_ref
)) {
558 if (explicit_tracking
)
559 die(_(upstream_not_branch
), start_name
);
561 FREE_AND_NULL(real_ref
);
565 die(_("ambiguous object name: '%s'"), start_name
);
569 if (!(commit
= lookup_commit_reference(r
, &oid
)))
570 die(_("not a valid branch point: '%s'"), start_name
);
572 *out_real_ref
= real_ref
;
576 oidcpy(out_oid
, &commit
->object
.oid
);
578 FREE_AND_NULL(real_ref
);
581 void create_branch(struct repository
*r
,
582 const char *name
, const char *start_name
,
583 int force
, int clobber_head_ok
, int reflog
,
584 int quiet
, enum branch_track track
, int dry_run
)
586 struct object_id oid
;
588 struct strbuf ref
= STRBUF_INIT
;
590 struct ref_transaction
*transaction
;
591 struct strbuf err
= STRBUF_INIT
;
594 if (track
== BRANCH_TRACK_OVERRIDE
)
595 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
596 if (clobber_head_ok
&& !force
)
597 BUG("'clobber_head_ok' can only be used with 'force'");
599 if (clobber_head_ok
?
600 validate_branchname(name
, &ref
) :
601 validate_new_branchname(name
, &ref
, force
)) {
605 dwim_branch_start(r
, start_name
, track
, &real_ref
, &oid
);
610 log_all_ref_updates
= LOG_REFS_NORMAL
;
613 msg
= xstrfmt("branch: Reset to %s", start_name
);
615 msg
= xstrfmt("branch: Created from %s", start_name
);
616 transaction
= ref_transaction_begin(&err
);
618 ref_transaction_update(transaction
, ref
.buf
,
619 &oid
, forcing
? NULL
: null_oid(),
621 ref_transaction_commit(transaction
, &err
))
623 ref_transaction_free(transaction
);
624 strbuf_release(&err
);
627 if (real_ref
&& track
)
628 setup_tracking(ref
.buf
+ 11, real_ref
, track
, quiet
);
631 strbuf_release(&ref
);
635 void dwim_and_setup_tracking(struct repository
*r
, const char *new_ref
,
636 const char *orig_ref
, enum branch_track track
,
640 dwim_branch_start(r
, orig_ref
, track
, &real_orig_ref
, NULL
);
641 setup_tracking(new_ref
, real_orig_ref
, track
, quiet
);
645 * Creates a branch in a submodule by calling
646 * create_branches_recursively() in a child process. The child process
647 * is necessary because install_branch_config_multiple_remotes() (which
648 * is called by setup_tracking()) does not support writing configs to
651 static int submodule_create_branch(struct repository
*r
,
652 const struct submodule
*submodule
,
653 const char *name
, const char *start_oid
,
654 const char *tracking_name
, int force
,
655 int reflog
, int quiet
,
656 enum branch_track track
, int dry_run
)
659 struct child_process child
= CHILD_PROCESS_INIT
;
660 struct strbuf child_err
= STRBUF_INIT
;
661 struct strbuf out_buf
= STRBUF_INIT
;
662 char *out_prefix
= xstrfmt("submodule '%s': ", submodule
->name
);
665 child
.stdout_to_stderr
= 1;
667 prepare_other_repo_env(&child
.env
, r
->gitdir
);
669 * submodule_create_branch() is indirectly invoked by "git
670 * branch", but we cannot invoke "git branch" in the child
671 * process. "git branch" accepts a branch name and start point,
672 * where the start point is assumed to provide both the OID
673 * (start_oid) and the branch to use for tracking
674 * (tracking_name). But when recursing through submodules,
675 * start_oid and tracking name need to be specified separately
676 * (see create_branches_recursively()).
678 strvec_pushl(&child
.args
, "submodule--helper", "create-branch", NULL
);
680 strvec_push(&child
.args
, "--dry-run");
682 strvec_push(&child
.args
, "--force");
684 strvec_push(&child
.args
, "--quiet");
686 strvec_push(&child
.args
, "--create-reflog");
689 case BRANCH_TRACK_NEVER
:
690 strvec_push(&child
.args
, "--no-track");
692 case BRANCH_TRACK_ALWAYS
:
693 case BRANCH_TRACK_EXPLICIT
:
694 strvec_push(&child
.args
, "--track=direct");
696 case BRANCH_TRACK_OVERRIDE
:
697 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
699 case BRANCH_TRACK_INHERIT
:
700 strvec_push(&child
.args
, "--track=inherit");
702 case BRANCH_TRACK_UNSPECIFIED
:
703 /* Default for "git checkout". Do not pass --track. */
704 case BRANCH_TRACK_REMOTE
:
705 /* Default for "git branch". Do not pass --track. */
706 case BRANCH_TRACK_SIMPLE
:
707 /* Config-driven only. Do not pass --track. */
711 strvec_pushl(&child
.args
, name
, start_oid
, tracking_name
, NULL
);
713 if ((ret
= start_command(&child
)))
715 ret
= finish_command(&child
);
716 strbuf_read(&child_err
, child
.err
, 0);
717 strbuf_add_lines(&out_buf
, out_prefix
, child_err
.buf
, child_err
.len
);
720 fprintf(stderr
, "%s", out_buf
.buf
);
722 printf("%s", out_buf
.buf
);
724 strbuf_release(&child_err
);
725 strbuf_release(&out_buf
);
729 void create_branches_recursively(struct repository
*r
, const char *name
,
730 const char *start_commitish
,
731 const char *tracking_name
, int force
,
732 int reflog
, int quiet
, enum branch_track track
,
736 char *branch_point
= NULL
;
737 struct object_id super_oid
;
738 struct submodule_entry_list submodule_entry_list
;
740 /* Perform dwim on start_commitish to get super_oid and branch_point. */
741 dwim_branch_start(r
, start_commitish
, BRANCH_TRACK_NEVER
,
742 &branch_point
, &super_oid
);
745 * If we were not given an explicit name to track, then assume we are at
746 * the top level and, just like the non-recursive case, the tracking
747 * name is the branch point.
750 tracking_name
= branch_point
;
752 submodules_of_tree(r
, &super_oid
, &submodule_entry_list
);
754 * Before creating any branches, first check that the branch can
755 * be created in every submodule.
757 for (i
= 0; i
< submodule_entry_list
.entry_nr
; i
++) {
758 if (!submodule_entry_list
.entries
[i
].repo
) {
759 int code
= die_message(
760 _("submodule '%s': unable to find submodule"),
761 submodule_entry_list
.entries
[i
].submodule
->name
);
762 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED
))
763 advise(_("You may try updating the submodules using 'git checkout --no-recurse-submodules %s && git submodule update --init'"),
768 if (submodule_create_branch(
769 submodule_entry_list
.entries
[i
].repo
,
770 submodule_entry_list
.entries
[i
].submodule
, name
,
771 oid_to_hex(&submodule_entry_list
.entries
[i
]
773 tracking_name
, force
, reflog
, quiet
, track
, 1))
774 die(_("submodule '%s': cannot create branch '%s'"),
775 submodule_entry_list
.entries
[i
].submodule
->name
,
779 create_branch(r
, name
, start_commitish
, force
, 0, reflog
, quiet
,
780 BRANCH_TRACK_NEVER
, dry_run
);
784 * NEEDSWORK If tracking was set up in the superproject but not the
785 * submodule, users might expect "git branch --recurse-submodules" to
786 * fail or give a warning, but this is not yet implemented because it is
787 * tedious to determine whether or not tracking was set up in the
791 setup_tracking(name
, tracking_name
, track
, quiet
);
793 for (i
= 0; i
< submodule_entry_list
.entry_nr
; i
++) {
794 if (submodule_create_branch(
795 submodule_entry_list
.entries
[i
].repo
,
796 submodule_entry_list
.entries
[i
].submodule
, name
,
797 oid_to_hex(&submodule_entry_list
.entries
[i
]
799 tracking_name
, force
, reflog
, quiet
, track
, 0))
800 die(_("submodule '%s': cannot create branch '%s'"),
801 submodule_entry_list
.entries
[i
].submodule
->name
,
803 repo_clear(submodule_entry_list
.entries
[i
].repo
);
807 void remove_merge_branch_state(struct repository
*r
)
809 unlink(git_path_merge_head(r
));
810 unlink(git_path_merge_rr(r
));
811 unlink(git_path_merge_msg(r
));
812 unlink(git_path_merge_mode(r
));
813 unlink(git_path_auto_merge(r
));
814 save_autostash(git_path_merge_autostash(r
));
817 void remove_branch_state(struct repository
*r
, int verbose
)
819 sequencer_post_commit_cleanup(r
, verbose
);
820 unlink(git_path_squash_msg(r
));
821 remove_merge_branch_state(r
);
824 void die_if_checked_out(const char *branch
, int ignore_current_worktree
)
826 struct worktree
**worktrees
= get_worktrees();
828 for (int i
= 0; worktrees
[i
]; i
++) {
829 if (worktrees
[i
]->is_current
&& ignore_current_worktree
)
832 if (is_shared_symref(worktrees
[i
], "HEAD", branch
)) {
833 skip_prefix(branch
, "refs/heads/", &branch
);
834 die(_("'%s' is already checked out at '%s'"),
835 branch
, worktrees
[i
]->path
);
839 free_worktrees(worktrees
);
842 int replace_each_worktree_head_symref(const char *oldref
, const char *newref
,
846 struct worktree
**worktrees
= get_worktrees();
849 for (i
= 0; worktrees
[i
]; i
++) {
850 struct ref_store
*refs
;
852 if (worktrees
[i
]->is_detached
)
854 if (!worktrees
[i
]->head_ref
)
856 if (strcmp(oldref
, worktrees
[i
]->head_ref
))
859 refs
= get_worktree_ref_store(worktrees
[i
]);
860 if (refs_create_symref(refs
, "HEAD", newref
, logmsg
))
861 ret
= error(_("HEAD of working tree %s is not updated"),
865 free_worktrees(worktrees
);