mingw: provide details about unsafe directories' ownership
[alt-git.git] / branch.c
blob4c8523c66ad07f956fc7ff8a81339d069dd586c8
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"
14 struct tracking {
15 struct refspec_item spec;
16 struct string_list *srcs;
17 const char *remote;
18 int matches;
21 struct find_tracked_branch_cb {
22 struct tracking *tracking;
23 struct string_list ambiguous_remotes;
26 static int find_tracked_branch(struct remote *remote, void *priv)
28 struct find_tracked_branch_cb *ftb = priv;
29 struct tracking *tracking = ftb->tracking;
31 if (!remote_find_tracking(remote, &tracking->spec)) {
32 switch (++tracking->matches) {
33 case 1:
34 string_list_append(tracking->srcs, tracking->spec.src);
35 tracking->remote = remote->name;
36 break;
37 case 2:
38 /* there are at least two remotes; backfill the first one */
39 string_list_append(&ftb->ambiguous_remotes, tracking->remote);
40 /* fall through */
41 default:
42 string_list_append(&ftb->ambiguous_remotes, remote->name);
43 free(tracking->spec.src);
44 string_list_clear(tracking->srcs, 0);
45 break;
47 /* remote_find_tracking() searches by src if present */
48 tracking->spec.src = NULL;
50 return 0;
53 static int should_setup_rebase(const char *origin)
55 switch (autorebase) {
56 case AUTOREBASE_NEVER:
57 return 0;
58 case AUTOREBASE_LOCAL:
59 return origin == NULL;
60 case AUTOREBASE_REMOTE:
61 return origin != NULL;
62 case AUTOREBASE_ALWAYS:
63 return 1;
65 return 0;
68 /**
69 * Install upstream tracking configuration for a branch; specifically, add
70 * `branch.<name>.remote` and `branch.<name>.merge` entries.
72 * `flag` contains integer flags for options; currently only
73 * BRANCH_CONFIG_VERBOSE is checked.
75 * `local` is the name of the branch whose configuration we're installing.
77 * `origin` is the name of the remote owning the upstream branches. NULL means
78 * the upstream branches are local to this repo.
80 * `remotes` is a list of refs that are upstream of local
82 static int install_branch_config_multiple_remotes(int flag, const char *local,
83 const char *origin, struct string_list *remotes)
85 const char *shortname = NULL;
86 struct strbuf key = STRBUF_INIT;
87 struct string_list_item *item;
88 int rebasing = should_setup_rebase(origin);
90 if (!remotes->nr)
91 BUG("must provide at least one remote for branch config");
92 if (rebasing && remotes->nr > 1)
93 die(_("cannot inherit upstream tracking configuration of "
94 "multiple refs when rebasing is requested"));
97 * If the new branch is trying to track itself, something has gone
98 * wrong. Warn the user and don't proceed any further.
100 if (!origin)
101 for_each_string_list_item(item, remotes)
102 if (skip_prefix(item->string, "refs/heads/", &shortname)
103 && !strcmp(local, shortname)) {
104 warning(_("not setting branch '%s' as its own upstream"),
105 local);
106 return 0;
109 strbuf_addf(&key, "branch.%s.remote", local);
110 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
111 goto out_err;
113 strbuf_reset(&key);
114 strbuf_addf(&key, "branch.%s.merge", local);
116 * We want to overwrite any existing config with all the branches in
117 * "remotes". Override any existing config, then write our branches. If
118 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
119 * we've written so far.
121 if (git_config_set_gently(key.buf, NULL) < 0)
122 goto out_err;
123 for_each_string_list_item(item, remotes)
124 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
125 goto out_err;
127 if (rebasing) {
128 strbuf_reset(&key);
129 strbuf_addf(&key, "branch.%s.rebase", local);
130 if (git_config_set_gently(key.buf, "true") < 0)
131 goto out_err;
133 strbuf_release(&key);
135 if (flag & BRANCH_CONFIG_VERBOSE) {
136 struct strbuf tmp_ref_name = STRBUF_INIT;
137 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
139 for_each_string_list_item(item, remotes) {
140 shortname = item->string;
141 skip_prefix(shortname, "refs/heads/", &shortname);
142 if (origin) {
143 strbuf_addf(&tmp_ref_name, "%s/%s",
144 origin, shortname);
145 string_list_append_nodup(
146 &friendly_ref_names,
147 strbuf_detach(&tmp_ref_name, NULL));
148 } else {
149 string_list_append(
150 &friendly_ref_names, shortname);
154 if (remotes->nr == 1) {
156 * Rebasing is only allowed in the case of a single
157 * upstream branch.
159 printf_ln(rebasing ?
160 _("branch '%s' set up to track '%s' by rebasing.") :
161 _("branch '%s' set up to track '%s'."),
162 local, friendly_ref_names.items[0].string);
163 } else {
164 printf_ln(_("branch '%s' set up to track:"), local);
165 for_each_string_list_item(item, &friendly_ref_names)
166 printf_ln(" %s", item->string);
169 string_list_clear(&friendly_ref_names, 0);
172 return 0;
174 out_err:
175 strbuf_release(&key);
176 error(_("unable to write upstream branch configuration"));
178 advise(_("\nAfter fixing the error cause you may try to fix up\n"
179 "the remote tracking information by invoking:"));
180 if (remotes->nr == 1)
181 advise(" git branch --set-upstream-to=%s%s%s",
182 origin ? origin : "",
183 origin ? "/" : "",
184 remotes->items[0].string);
185 else {
186 advise(" git config --add branch.\"%s\".remote %s",
187 local, origin ? origin : ".");
188 for_each_string_list_item(item, remotes)
189 advise(" git config --add branch.\"%s\".merge %s",
190 local, item->string);
193 return -1;
196 int install_branch_config(int flag, const char *local, const char *origin,
197 const char *remote)
199 int ret;
200 struct string_list remotes = STRING_LIST_INIT_DUP;
202 string_list_append(&remotes, remote);
203 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
204 string_list_clear(&remotes, 0);
205 return ret;
208 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
210 const char *bare_ref;
211 struct branch *branch;
212 int i;
214 bare_ref = orig_ref;
215 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
217 branch = branch_get(bare_ref);
218 if (!branch->remote_name) {
219 warning(_("asked to inherit tracking from '%s', but no remote is set"),
220 bare_ref);
221 return -1;
224 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
225 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
226 bare_ref);
227 return -1;
230 tracking->remote = xstrdup(branch->remote_name);
231 for (i = 0; i < branch->merge_nr; i++)
232 string_list_append(tracking->srcs, branch->merge_name[i]);
233 return 0;
237 * Used internally to set the branch.<new_ref>.{remote,merge} config
238 * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike
239 * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main"
240 * will not be expanded to "refs/remotes/origin/main", so it is not safe
241 * for 'orig_ref' to be raw user input.
243 static void setup_tracking(const char *new_ref, const char *orig_ref,
244 enum branch_track track, int quiet)
246 struct tracking tracking;
247 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
248 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
249 struct find_tracked_branch_cb ftb_cb = {
250 .tracking = &tracking,
251 .ambiguous_remotes = STRING_LIST_INIT_DUP,
254 if (!track)
255 BUG("asked to set up tracking, but tracking is disallowed");
257 memset(&tracking, 0, sizeof(tracking));
258 tracking.spec.dst = (char *)orig_ref;
259 tracking.srcs = &tracking_srcs;
260 if (track != BRANCH_TRACK_INHERIT)
261 for_each_remote(find_tracked_branch, &ftb_cb);
262 else if (inherit_tracking(&tracking, orig_ref))
263 goto cleanup;
265 if (!tracking.matches)
266 switch (track) {
267 /* If ref is not remote, still use local */
268 case BRANCH_TRACK_ALWAYS:
269 case BRANCH_TRACK_EXPLICIT:
270 case BRANCH_TRACK_OVERRIDE:
271 /* Remote matches not evaluated */
272 case BRANCH_TRACK_INHERIT:
273 break;
274 /* Otherwise, if no remote don't track */
275 default:
276 goto cleanup;
280 * This check does not apply to BRANCH_TRACK_INHERIT;
281 * that supports multiple entries in tracking_srcs but
282 * leaves tracking.matches at 0.
284 if (tracking.matches > 1) {
285 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
286 orig_ref);
287 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
288 struct strbuf remotes_advice = STRBUF_INIT;
289 struct string_list_item *item;
291 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
293 * TRANSLATORS: This is a line listing a remote with duplicate
294 * refspecs in the advice message below. For RTL languages you'll
295 * probably want to swap the "%s" and leading " " space around.
297 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
300 * TRANSLATORS: The second argument is a \n-delimited list of
301 * duplicate refspecs, composed above.
303 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
304 "tracking ref '%s':\n"
305 "%s"
306 "\n"
307 "This is typically a configuration error.\n"
308 "\n"
309 "To support setting up tracking branches, ensure that\n"
310 "different remotes' fetch refspecs map into different\n"
311 "tracking namespaces."), orig_ref,
312 remotes_advice.buf);
313 strbuf_release(&remotes_advice);
315 exit(status);
318 if (track == BRANCH_TRACK_SIMPLE) {
320 * Only track if remote branch name matches.
321 * Reaching into items[0].string is safe because
322 * we know there is at least one and not more than
323 * one entry (because only BRANCH_TRACK_INHERIT can
324 * produce more than one entry).
326 const char *tracked_branch;
327 if (!skip_prefix(tracking.srcs->items[0].string,
328 "refs/heads/", &tracked_branch) ||
329 strcmp(tracked_branch, new_ref))
330 return;
333 if (tracking.srcs->nr < 1)
334 string_list_append(tracking.srcs, orig_ref);
335 if (install_branch_config_multiple_remotes(config_flags, new_ref,
336 tracking.remote, tracking.srcs) < 0)
337 exit(1);
339 cleanup:
340 string_list_clear(&tracking_srcs, 0);
341 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
344 int read_branch_desc(struct strbuf *buf, const char *branch_name)
346 char *v = NULL;
347 struct strbuf name = STRBUF_INIT;
348 strbuf_addf(&name, "branch.%s.description", branch_name);
349 if (git_config_get_string(name.buf, &v)) {
350 strbuf_release(&name);
351 return -1;
353 strbuf_addstr(buf, v);
354 free(v);
355 strbuf_release(&name);
356 return 0;
360 * Check if 'name' can be a valid name for a branch; die otherwise.
361 * Return 1 if the named branch already exists; return 0 otherwise.
362 * Fill ref with the full refname for the branch.
364 int validate_branchname(const char *name, struct strbuf *ref)
366 if (strbuf_check_branch_ref(ref, name))
367 die(_("'%s' is not a valid branch name"), name);
369 return ref_exists(ref->buf);
373 * Check if a branch 'name' can be created as a new branch; die otherwise.
374 * 'force' can be used when it is OK for the named branch already exists.
375 * Return 1 if the named branch already exists; return 0 otherwise.
376 * Fill ref with the full refname for the branch.
378 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
380 struct worktree **worktrees;
381 const struct worktree *wt;
383 if (!validate_branchname(name, ref))
384 return 0;
386 if (!force)
387 die(_("a branch named '%s' already exists"),
388 ref->buf + strlen("refs/heads/"));
390 worktrees = get_worktrees();
391 wt = find_shared_symref(worktrees, "HEAD", ref->buf);
392 if (wt && !wt->is_bare)
393 die(_("cannot force update the branch '%s' "
394 "checked out at '%s'"),
395 ref->buf + strlen("refs/heads/"), wt->path);
396 free_worktrees(worktrees);
398 return 1;
401 static int check_tracking_branch(struct remote *remote, void *cb_data)
403 char *tracking_branch = cb_data;
404 struct refspec_item query;
405 memset(&query, 0, sizeof(struct refspec_item));
406 query.dst = tracking_branch;
407 return !remote_find_tracking(remote, &query);
410 static int validate_remote_tracking_branch(char *ref)
412 return !for_each_remote(check_tracking_branch, ref);
415 static const char upstream_not_branch[] =
416 N_("cannot set up tracking information; starting point '%s' is not a branch");
417 static const char upstream_missing[] =
418 N_("the requested upstream branch '%s' does not exist");
419 static const char upstream_advice[] =
420 N_("\n"
421 "If you are planning on basing your work on an upstream\n"
422 "branch that already exists at the remote, you may need to\n"
423 "run \"git fetch\" to retrieve it.\n"
424 "\n"
425 "If you are planning to push out a new local branch that\n"
426 "will track its remote counterpart, you may want to use\n"
427 "\"git push -u\" to set the upstream config as you push.");
430 * DWIMs a user-provided ref to determine the starting point for a
431 * branch and validates it, where:
433 * - r is the repository to validate the branch for
435 * - start_name is the ref that we would like to test. This is
436 * expanded with DWIM and assigned to out_real_ref.
438 * - track is the tracking mode of the new branch. If tracking is
439 * explicitly requested, start_name must be a branch (because
440 * otherwise start_name cannot be tracked)
442 * - out_oid is an out parameter containing the object_id of start_name
444 * - out_real_ref is an out parameter containing the full, 'real' form
445 * of start_name e.g. refs/heads/main instead of main
448 static void dwim_branch_start(struct repository *r, const char *start_name,
449 enum branch_track track, char **out_real_ref,
450 struct object_id *out_oid)
452 struct commit *commit;
453 struct object_id oid;
454 char *real_ref;
455 int explicit_tracking = 0;
457 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
458 explicit_tracking = 1;
460 real_ref = NULL;
461 if (get_oid_mb(start_name, &oid)) {
462 if (explicit_tracking) {
463 int code = die_message(_(upstream_missing), start_name);
464 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
465 _(upstream_advice));
466 exit(code);
468 die(_("not a valid object name: '%s'"), start_name);
471 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
472 case 0:
473 /* Not branching from any existing branch */
474 if (explicit_tracking)
475 die(_(upstream_not_branch), start_name);
476 break;
477 case 1:
478 /* Unique completion -- good, only if it is a real branch */
479 if (!starts_with(real_ref, "refs/heads/") &&
480 validate_remote_tracking_branch(real_ref)) {
481 if (explicit_tracking)
482 die(_(upstream_not_branch), start_name);
483 else
484 FREE_AND_NULL(real_ref);
486 break;
487 default:
488 die(_("ambiguous object name: '%s'"), start_name);
489 break;
492 if (!(commit = lookup_commit_reference(r, &oid)))
493 die(_("not a valid branch point: '%s'"), start_name);
494 if (out_real_ref) {
495 *out_real_ref = real_ref;
496 real_ref = NULL;
498 if (out_oid)
499 oidcpy(out_oid, &commit->object.oid);
501 FREE_AND_NULL(real_ref);
504 void create_branch(struct repository *r,
505 const char *name, const char *start_name,
506 int force, int clobber_head_ok, int reflog,
507 int quiet, enum branch_track track, int dry_run)
509 struct object_id oid;
510 char *real_ref;
511 struct strbuf ref = STRBUF_INIT;
512 int forcing = 0;
513 struct ref_transaction *transaction;
514 struct strbuf err = STRBUF_INIT;
515 char *msg;
517 if (track == BRANCH_TRACK_OVERRIDE)
518 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
519 if (clobber_head_ok && !force)
520 BUG("'clobber_head_ok' can only be used with 'force'");
522 if (clobber_head_ok ?
523 validate_branchname(name, &ref) :
524 validate_new_branchname(name, &ref, force)) {
525 forcing = 1;
528 dwim_branch_start(r, start_name, track, &real_ref, &oid);
529 if (dry_run)
530 goto cleanup;
532 if (reflog)
533 log_all_ref_updates = LOG_REFS_NORMAL;
535 if (forcing)
536 msg = xstrfmt("branch: Reset to %s", start_name);
537 else
538 msg = xstrfmt("branch: Created from %s", start_name);
539 transaction = ref_transaction_begin(&err);
540 if (!transaction ||
541 ref_transaction_update(transaction, ref.buf,
542 &oid, forcing ? NULL : null_oid(),
543 0, msg, &err) ||
544 ref_transaction_commit(transaction, &err))
545 die("%s", err.buf);
546 ref_transaction_free(transaction);
547 strbuf_release(&err);
548 free(msg);
550 if (real_ref && track)
551 setup_tracking(ref.buf + 11, real_ref, track, quiet);
553 cleanup:
554 strbuf_release(&ref);
555 free(real_ref);
558 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
559 const char *orig_ref, enum branch_track track,
560 int quiet)
562 char *real_orig_ref;
563 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
564 setup_tracking(new_ref, real_orig_ref, track, quiet);
568 * Creates a branch in a submodule by calling
569 * create_branches_recursively() in a child process. The child process
570 * is necessary because install_branch_config_multiple_remotes() (which
571 * is called by setup_tracking()) does not support writing configs to
572 * submodules.
574 static int submodule_create_branch(struct repository *r,
575 const struct submodule *submodule,
576 const char *name, const char *start_oid,
577 const char *tracking_name, int force,
578 int reflog, int quiet,
579 enum branch_track track, int dry_run)
581 int ret = 0;
582 struct child_process child = CHILD_PROCESS_INIT;
583 struct strbuf child_err = STRBUF_INIT;
584 struct strbuf out_buf = STRBUF_INIT;
585 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
586 child.git_cmd = 1;
587 child.err = -1;
588 child.stdout_to_stderr = 1;
590 prepare_other_repo_env(&child.env, r->gitdir);
592 * submodule_create_branch() is indirectly invoked by "git
593 * branch", but we cannot invoke "git branch" in the child
594 * process. "git branch" accepts a branch name and start point,
595 * where the start point is assumed to provide both the OID
596 * (start_oid) and the branch to use for tracking
597 * (tracking_name). But when recursing through submodules,
598 * start_oid and tracking name need to be specified separately
599 * (see create_branches_recursively()).
601 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
602 if (dry_run)
603 strvec_push(&child.args, "--dry-run");
604 if (force)
605 strvec_push(&child.args, "--force");
606 if (quiet)
607 strvec_push(&child.args, "--quiet");
608 if (reflog)
609 strvec_push(&child.args, "--create-reflog");
611 switch (track) {
612 case BRANCH_TRACK_NEVER:
613 strvec_push(&child.args, "--no-track");
614 break;
615 case BRANCH_TRACK_ALWAYS:
616 case BRANCH_TRACK_EXPLICIT:
617 strvec_push(&child.args, "--track=direct");
618 break;
619 case BRANCH_TRACK_OVERRIDE:
620 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
621 break;
622 case BRANCH_TRACK_INHERIT:
623 strvec_push(&child.args, "--track=inherit");
624 break;
625 case BRANCH_TRACK_UNSPECIFIED:
626 /* Default for "git checkout". Do not pass --track. */
627 case BRANCH_TRACK_REMOTE:
628 /* Default for "git branch". Do not pass --track. */
629 case BRANCH_TRACK_SIMPLE:
630 /* Config-driven only. Do not pass --track. */
631 break;
634 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
636 if ((ret = start_command(&child)))
637 return ret;
638 ret = finish_command(&child);
639 strbuf_read(&child_err, child.err, 0);
640 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
642 if (ret)
643 fprintf(stderr, "%s", out_buf.buf);
644 else
645 printf("%s", out_buf.buf);
647 strbuf_release(&child_err);
648 strbuf_release(&out_buf);
649 return ret;
652 void create_branches_recursively(struct repository *r, const char *name,
653 const char *start_commitish,
654 const char *tracking_name, int force,
655 int reflog, int quiet, enum branch_track track,
656 int dry_run)
658 int i = 0;
659 char *branch_point = NULL;
660 struct object_id super_oid;
661 struct submodule_entry_list submodule_entry_list;
663 /* Perform dwim on start_commitish to get super_oid and branch_point. */
664 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
665 &branch_point, &super_oid);
668 * If we were not given an explicit name to track, then assume we are at
669 * the top level and, just like the non-recursive case, the tracking
670 * name is the branch point.
672 if (!tracking_name)
673 tracking_name = branch_point;
675 submodules_of_tree(r, &super_oid, &submodule_entry_list);
677 * Before creating any branches, first check that the branch can
678 * be created in every submodule.
680 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
681 if (!submodule_entry_list.entries[i].repo) {
682 int code = die_message(
683 _("submodule '%s': unable to find submodule"),
684 submodule_entry_list.entries[i].submodule->name);
685 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
686 advise(_("You may try updating the submodules using 'git checkout %s && git submodule update --init'"),
687 start_commitish);
688 exit(code);
691 if (submodule_create_branch(
692 submodule_entry_list.entries[i].repo,
693 submodule_entry_list.entries[i].submodule, name,
694 oid_to_hex(&submodule_entry_list.entries[i]
695 .name_entry->oid),
696 tracking_name, force, reflog, quiet, track, 1))
697 die(_("submodule '%s': cannot create branch '%s'"),
698 submodule_entry_list.entries[i].submodule->name,
699 name);
702 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
703 BRANCH_TRACK_NEVER, dry_run);
704 if (dry_run)
705 return;
707 * NEEDSWORK If tracking was set up in the superproject but not the
708 * submodule, users might expect "git branch --recurse-submodules" to
709 * fail or give a warning, but this is not yet implemented because it is
710 * tedious to determine whether or not tracking was set up in the
711 * superproject.
713 if (track)
714 setup_tracking(name, tracking_name, track, quiet);
716 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
717 if (submodule_create_branch(
718 submodule_entry_list.entries[i].repo,
719 submodule_entry_list.entries[i].submodule, name,
720 oid_to_hex(&submodule_entry_list.entries[i]
721 .name_entry->oid),
722 tracking_name, force, reflog, quiet, track, 0))
723 die(_("submodule '%s': cannot create branch '%s'"),
724 submodule_entry_list.entries[i].submodule->name,
725 name);
726 repo_clear(submodule_entry_list.entries[i].repo);
730 void remove_merge_branch_state(struct repository *r)
732 unlink(git_path_merge_head(r));
733 unlink(git_path_merge_rr(r));
734 unlink(git_path_merge_msg(r));
735 unlink(git_path_merge_mode(r));
736 unlink(git_path_auto_merge(r));
737 save_autostash(git_path_merge_autostash(r));
740 void remove_branch_state(struct repository *r, int verbose)
742 sequencer_post_commit_cleanup(r, verbose);
743 unlink(git_path_squash_msg(r));
744 remove_merge_branch_state(r);
747 void die_if_checked_out(const char *branch, int ignore_current_worktree)
749 struct worktree **worktrees = get_worktrees();
750 const struct worktree *wt;
752 wt = find_shared_symref(worktrees, "HEAD", branch);
753 if (wt && (!ignore_current_worktree || !wt->is_current)) {
754 skip_prefix(branch, "refs/heads/", &branch);
755 die(_("'%s' is already checked out at '%s'"), branch, wt->path);
758 free_worktrees(worktrees);
761 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
762 const char *logmsg)
764 int ret = 0;
765 struct worktree **worktrees = get_worktrees();
766 int i;
768 for (i = 0; worktrees[i]; i++) {
769 struct ref_store *refs;
771 if (worktrees[i]->is_detached)
772 continue;
773 if (!worktrees[i]->head_ref)
774 continue;
775 if (strcmp(oldref, worktrees[i]->head_ref))
776 continue;
778 refs = get_worktree_ref_store(worktrees[i]);
779 if (refs_create_symref(refs, "HEAD", newref, logmsg))
780 ret = error(_("HEAD of working tree %s is not updated"),
781 worktrees[i]->path);
784 free_worktrees(worktrees);
785 return ret;