]> git.ipfire.org Git - thirdparty/git.git/blame - branch.c
branch: use branch_checked_out() when deleting refs
[thirdparty/git.git] / branch.c
CommitLineData
303d1d0b 1#include "git-compat-util.h"
e496c003 2#include "cache.h"
b2141fc1 3#include "config.h"
e496c003
DB
4#include "branch.h"
5#include "refs.h"
ec0cb496 6#include "refspec.h"
e496c003 7#include "remote.h"
b07d9bfd 8#include "sequencer.h"
e496c003 9#include "commit.h"
ac6c561b 10#include "worktree.h"
961b130d
GC
11#include "submodule-config.h"
12#include "run-command.h"
31ad6b61 13#include "strmap.h"
e496c003
DB
14
15struct tracking {
0ad4a5ff 16 struct refspec_item spec;
d3115660 17 struct string_list *srcs;
e496c003
DB
18 const char *remote;
19 int matches;
20};
21
e4921d87
TK
22struct find_tracked_branch_cb {
23 struct tracking *tracking;
24 struct string_list ambiguous_remotes;
25};
26
e496c003
DB
27static int find_tracked_branch(struct remote *remote, void *priv)
28{
e4921d87
TK
29 struct find_tracked_branch_cb *ftb = priv;
30 struct tracking *tracking = ftb->tracking;
e496c003
DB
31
32 if (!remote_find_tracking(remote, &tracking->spec)) {
e4921d87
TK
33 switch (++tracking->matches) {
34 case 1:
d3115660 35 string_list_append(tracking->srcs, tracking->spec.src);
e496c003 36 tracking->remote = remote->name;
e4921d87
TK
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);
e496c003 44 free(tracking->spec.src);
d3115660 45 string_list_clear(tracking->srcs, 0);
e4921d87 46 break;
e496c003
DB
47 }
48 tracking->spec.src = NULL;
49 }
50
51 return 0;
52}
53
a9f2c136 54static int should_setup_rebase(const char *origin)
c998ae9b
DS
55{
56 switch (autorebase) {
57 case AUTOREBASE_NEVER:
58 return 0;
59 case AUTOREBASE_LOCAL:
a9f2c136 60 return origin == NULL;
c998ae9b 61 case AUTOREBASE_REMOTE:
a9f2c136 62 return origin != NULL;
c998ae9b
DS
63 case AUTOREBASE_ALWAYS:
64 return 1;
65 }
66 return 0;
67}
68
a3f40ec4
JS
69/**
70 * Install upstream tracking configuration for a branch; specifically, add
71 * `branch.<name>.remote` and `branch.<name>.merge` entries.
72 *
73 * `flag` contains integer flags for options; currently only
74 * BRANCH_CONFIG_VERBOSE is checked.
75 *
76 * `local` is the name of the branch whose configuration we're installing.
77 *
78 * `origin` is the name of the remote owning the upstream branches. NULL means
79 * the upstream branches are local to this repo.
80 *
81 * `remotes` is a list of refs that are upstream of local
82 */
83static int install_branch_config_multiple_remotes(int flag, const char *local,
84 const char *origin, struct string_list *remotes)
a9f2c136 85{
cf4fff57 86 const char *shortname = NULL;
a9f2c136 87 struct strbuf key = STRBUF_INIT;
a3f40ec4 88 struct string_list_item *item;
a9f2c136
JH
89 int rebasing = should_setup_rebase(origin);
90
a3f40ec4
JS
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"));
96
97 /*
98 * If the new branch is trying to track itself, something has gone
99 * wrong. Warn the user and don't proceed any further.
100 */
101 if (!origin)
102 for_each_string_list_item(item, remotes)
103 if (skip_prefix(item->string, "refs/heads/", &shortname)
104 && !strcmp(local, shortname)) {
0669bdf4 105 warning(_("not setting branch '%s' as its own upstream"),
a3f40ec4
JS
106 local);
107 return 0;
108 }
85e2233f 109
a9f2c136 110 strbuf_addf(&key, "branch.%s.remote", local);
30598ad0 111 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
27852b2c 112 goto out_err;
a9f2c136
JH
113
114 strbuf_reset(&key);
115 strbuf_addf(&key, "branch.%s.merge", local);
a3f40ec4
JS
116 /*
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.
121 */
122 if (git_config_set_gently(key.buf, NULL) < 0)
27852b2c 123 goto out_err;
a3f40ec4
JS
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;
a9f2c136
JH
127
128 if (rebasing) {
129 strbuf_reset(&key);
130 strbuf_addf(&key, "branch.%s.rebase", local);
30598ad0 131 if (git_config_set_gently(key.buf, "true") < 0)
27852b2c 132 goto out_err;
a9f2c136 133 }
d53a3503 134 strbuf_release(&key);
a9f2c136 135
72f60083 136 if (flag & BRANCH_CONFIG_VERBOSE) {
a3f40ec4
JS
137 struct strbuf tmp_ref_name = STRBUF_INIT;
138 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
139
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);
152 }
153 }
154
155 if (remotes->nr == 1) {
156 /*
157 * Rebasing is only allowed in the case of a single
158 * upstream branch.
159 */
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);
9fe0cf3a 164 } else {
a3f40ec4
JS
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);
9fe0cf3a 168 }
a3f40ec4
JS
169
170 string_list_clear(&friendly_ref_names, 0);
72f60083 171 }
27852b2c
PS
172
173 return 0;
174
175out_err:
176 strbuf_release(&key);
7435e7e2 177 error(_("unable to write upstream branch configuration"));
27852b2c 178
a3f40ec4
JS
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);
192 }
27852b2c
PS
193
194 return -1;
a9f2c136
JH
195}
196
a3f40ec4
JS
197int install_branch_config(int flag, const char *local, const char *origin,
198 const char *remote)
199{
200 int ret;
201 struct string_list remotes = STRING_LIST_INIT_DUP;
202
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;
207}
208
d3115660
JS
209static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
210{
211 const char *bare_ref;
212 struct branch *branch;
213 int i;
214
215 bare_ref = orig_ref;
216 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
217
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;
223 }
224
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;
229 }
230
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;
235}
236
e496c003 237/*
e89f151d
GC
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.
e496c003 243 */
27852b2c
PS
244static void setup_tracking(const char *new_ref, const char *orig_ref,
245 enum branch_track track, int quiet)
e496c003 246{
e496c003 247 struct tracking tracking;
d3115660 248 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
f9a482e6 249 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
e4921d87
TK
250 struct find_tracked_branch_cb ftb_cb = {
251 .tracking = &tracking,
252 .ambiguous_remotes = STRING_LIST_INIT_DUP,
253 };
e496c003 254
75388bf5
GC
255 if (!track)
256 BUG("asked to set up tracking, but tracking is disallowed");
257
e496c003
DB
258 memset(&tracking, 0, sizeof(tracking));
259 tracking.spec.dst = (char *)orig_ref;
d3115660
JS
260 tracking.srcs = &tracking_srcs;
261 if (track != BRANCH_TRACK_INHERIT)
e4921d87 262 for_each_remote(find_tracked_branch, &ftb_cb);
d3115660 263 else if (inherit_tracking(&tracking, orig_ref))
679e3693 264 goto cleanup;
e496c003 265
9ed36cfa
JS
266 if (!tracking.matches)
267 switch (track) {
268 case BRANCH_TRACK_ALWAYS:
269 case BRANCH_TRACK_EXPLICIT:
4fc50066 270 case BRANCH_TRACK_OVERRIDE:
d3115660 271 case BRANCH_TRACK_INHERIT:
9ed36cfa
JS
272 break;
273 default:
679e3693 274 goto cleanup;
9ed36cfa
JS
275 }
276
e4921d87
TK
277 if (tracking.matches > 1) {
278 int status = die_message(_("not tracking: ambiguous information for ref '%s'"),
279 orig_ref);
280 if (advice_enabled(ADVICE_AMBIGUOUS_FETCH_REFSPEC)) {
281 struct strbuf remotes_advice = STRBUF_INIT;
282 struct string_list_item *item;
283
284 for_each_string_list_item(item, &ftb_cb.ambiguous_remotes)
285 /*
286 * TRANSLATORS: This is a line listing a remote with duplicate
287 * refspecs in the advice message below. For RTL languages you'll
288 * probably want to swap the "%s" and leading " " space around.
289 */
290 strbuf_addf(&remotes_advice, _(" %s\n"), item->string);
291
292 /*
293 * TRANSLATORS: The second argument is a \n-delimited list of
294 * duplicate refspecs, composed above.
295 */
296 advise(_("There are multiple remotes whose fetch refspecs map to the remote\n"
297 "tracking ref '%s':\n"
298 "%s"
299 "\n"
300 "This is typically a configuration error.\n"
301 "\n"
302 "To support setting up tracking branches, ensure that\n"
303 "different remotes' fetch refspecs map into different\n"
304 "tracking namespaces."), orig_ref,
305 remotes_advice.buf);
306 strbuf_release(&remotes_advice);
307 }
308 exit(status);
309 }
e496c003 310
d3115660
JS
311 if (tracking.srcs->nr < 1)
312 string_list_append(tracking.srcs, orig_ref);
313 if (install_branch_config_multiple_remotes(config_flags, new_ref,
314 tracking.remote, tracking.srcs) < 0)
5391e948 315 exit(1);
e496c003 316
679e3693
GC
317cleanup:
318 string_list_clear(&tracking_srcs, 0);
e4921d87 319 string_list_clear(&ftb_cb.ambiguous_remotes, 0);
e496c003
DB
320}
321
6f9a3321
JH
322int read_branch_desc(struct strbuf *buf, const char *branch_name)
323{
540b0f49 324 char *v = NULL;
6f9a3321
JH
325 struct strbuf name = STRBUF_INIT;
326 strbuf_addf(&name, "branch.%s.description", branch_name);
540b0f49
TA
327 if (git_config_get_string(name.buf, &v)) {
328 strbuf_release(&name);
329 return -1;
330 }
331 strbuf_addstr(buf, v);
332 free(v);
6f9a3321
JH
333 strbuf_release(&name);
334 return 0;
335}
336
bc1c9c0e
JH
337/*
338 * Check if 'name' can be a valid name for a branch; die otherwise.
339 * Return 1 if the named branch already exists; return 0 otherwise.
340 * Fill ref with the full refname for the branch.
341 */
342int validate_branchname(const char *name, struct strbuf *ref)
55c4a673 343{
55c4a673 344 if (strbuf_check_branch_ref(ref, name))
7435e7e2 345 die(_("'%s' is not a valid branch name"), name);
55c4a673 346
bc1c9c0e
JH
347 return ref_exists(ref->buf);
348}
55c4a673 349
31ad6b61
DS
350static int initialized_checked_out_branches;
351static struct strmap current_checked_out_branches = STRMAP_INIT;
352
353static void prepare_checked_out_branches(void)
354{
355 int i = 0;
356 struct worktree **worktrees;
357
358 if (initialized_checked_out_branches)
359 return;
360 initialized_checked_out_branches = 1;
361
362 worktrees = get_worktrees();
363
364 while (worktrees[i]) {
d2ba271a 365 struct wt_status_state state = { 0 };
31ad6b61
DS
366 struct worktree *wt = worktrees[i++];
367
368 if (wt->is_bare)
369 continue;
370
371 if (wt->head_ref)
372 strmap_put(&current_checked_out_branches,
373 wt->head_ref,
374 xstrdup(wt->path));
d2ba271a
DS
375
376 if (wt_status_check_rebase(wt, &state) &&
377 (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
378 state.branch) {
379 struct strbuf ref = STRBUF_INIT;
380 strbuf_addf(&ref, "refs/heads/%s", state.branch);
381 strmap_put(&current_checked_out_branches,
382 ref.buf,
383 xstrdup(wt->path));
384 strbuf_release(&ref);
385 }
386 wt_status_state_free_buffers(&state);
387
388 if (wt_status_check_bisect(wt, &state) &&
389 state.branch) {
390 struct strbuf ref = STRBUF_INIT;
391 strbuf_addf(&ref, "refs/heads/%s", state.branch);
392 strmap_put(&current_checked_out_branches,
393 ref.buf,
394 xstrdup(wt->path));
395 strbuf_release(&ref);
396 }
397 wt_status_state_free_buffers(&state);
31ad6b61
DS
398 }
399
400 free_worktrees(worktrees);
401}
402
403const char *branch_checked_out(const char *refname)
404{
405 prepare_checked_out_branches();
406 return strmap_get(&current_checked_out_branches, refname);
407}
408
bc1c9c0e
JH
409/*
410 * Check if a branch 'name' can be created as a new branch; die otherwise.
411 * 'force' can be used when it is OK for the named branch already exists.
412 * Return 1 if the named branch already exists; return 0 otherwise.
413 * Fill ref with the full refname for the branch.
414 */
415int validate_new_branchname(const char *name, struct strbuf *ref, int force)
416{
d2ba271a 417 const char *path;
bc1c9c0e 418 if (!validate_branchname(name, ref))
55c4a673 419 return 0;
55c4a673 420
8280c4c1 421 if (!force)
7435e7e2 422 die(_("a branch named '%s' already exists"),
8280c4c1
JH
423 ref->buf + strlen("refs/heads/"));
424
d2ba271a 425 if ((path = branch_checked_out(ref->buf)))
68d924e1 426 die(_("cannot force update the branch '%s' "
593a2a5d 427 "checked out at '%s'"),
d2ba271a 428 ref->buf + strlen("refs/heads/"), path);
55c4a673
CI
429
430 return 1;
431}
432
41c21f22
JH
433static int check_tracking_branch(struct remote *remote, void *cb_data)
434{
435 char *tracking_branch = cb_data;
0ad4a5ff
BW
436 struct refspec_item query;
437 memset(&query, 0, sizeof(struct refspec_item));
41c21f22 438 query.dst = tracking_branch;
1d7358c5 439 return !remote_find_tracking(remote, &query);
41c21f22
JH
440}
441
442static int validate_remote_tracking_branch(char *ref)
443{
444 return !for_each_remote(check_tracking_branch, ref);
445}
446
e2b6aa5f 447static const char upstream_not_branch[] =
7435e7e2 448N_("cannot set up tracking information; starting point '%s' is not a branch");
a5e91c72 449static const char upstream_missing[] =
caa2036b
JK
450N_("the requested upstream branch '%s' does not exist");
451static const char upstream_advice[] =
452N_("\n"
453"If you are planning on basing your work on an upstream\n"
454"branch that already exists at the remote, you may need to\n"
455"run \"git fetch\" to retrieve it.\n"
456"\n"
457"If you are planning to push out a new local branch that\n"
458"will track its remote counterpart, you may want to use\n"
459"\"git push -u\" to set the upstream config as you push.");
e2b6aa5f 460
e89f151d
GC
461/**
462 * DWIMs a user-provided ref to determine the starting point for a
463 * branch and validates it, where:
464 *
465 * - r is the repository to validate the branch for
466 *
467 * - start_name is the ref that we would like to test. This is
468 * expanded with DWIM and assigned to out_real_ref.
469 *
470 * - track is the tracking mode of the new branch. If tracking is
471 * explicitly requested, start_name must be a branch (because
472 * otherwise start_name cannot be tracked)
473 *
474 * - out_oid is an out parameter containing the object_id of start_name
475 *
476 * - out_real_ref is an out parameter containing the full, 'real' form
477 * of start_name e.g. refs/heads/main instead of main
478 *
479 */
480static void dwim_branch_start(struct repository *r, const char *start_name,
481 enum branch_track track, char **out_real_ref,
482 struct object_id *out_oid)
e496c003 483{
e496c003 484 struct commit *commit;
48713bfa 485 struct object_id oid;
3818b258 486 char *real_ref;
4fc50066
IL
487 int explicit_tracking = 0;
488
489 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
490 explicit_tracking = 1;
e496c003 491
e496c003 492 real_ref = NULL;
e3d6539d 493 if (get_oid_mb(start_name, &oid)) {
caa2036b 494 if (explicit_tracking) {
66966012
GC
495 int code = die_message(_(upstream_missing), start_name);
496 advise_if_enabled(ADVICE_SET_UPSTREAM_FAILURE,
497 _(upstream_advice));
498 exit(code);
caa2036b 499 }
7435e7e2 500 die(_("not a valid object name: '%s'"), start_name);
a5e91c72 501 }
e496c003 502
f24c30e0 503 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
e496c003
DB
504 case 0:
505 /* Not branching from any existing branch */
4fc50066 506 if (explicit_tracking)
1a15d00b 507 die(_(upstream_not_branch), start_name);
e496c003
DB
508 break;
509 case 1:
21b5b1e8 510 /* Unique completion -- good, only if it is a real branch */
59556548 511 if (!starts_with(real_ref, "refs/heads/") &&
41c21f22 512 validate_remote_tracking_branch(real_ref)) {
21b5b1e8 513 if (explicit_tracking)
1a15d00b 514 die(_(upstream_not_branch), start_name);
21b5b1e8 515 else
d895804b 516 FREE_AND_NULL(real_ref);
21b5b1e8 517 }
e496c003
DB
518 break;
519 default:
7435e7e2 520 die(_("ambiguous object name: '%s'"), start_name);
e496c003
DB
521 break;
522 }
523
afe8a907 524 if (!(commit = lookup_commit_reference(r, &oid)))
7435e7e2 525 die(_("not a valid branch point: '%s'"), start_name);
e89f151d
GC
526 if (out_real_ref) {
527 *out_real_ref = real_ref;
528 real_ref = NULL;
529 }
530 if (out_oid)
531 oidcpy(out_oid, &commit->object.oid);
532
533 FREE_AND_NULL(real_ref);
534}
535
536void create_branch(struct repository *r,
537 const char *name, const char *start_name,
538 int force, int clobber_head_ok, int reflog,
3f3e7608 539 int quiet, enum branch_track track, int dry_run)
e89f151d
GC
540{
541 struct object_id oid;
542 char *real_ref;
543 struct strbuf ref = STRBUF_INIT;
544 int forcing = 0;
bc0893cf
GC
545 struct ref_transaction *transaction;
546 struct strbuf err = STRBUF_INIT;
547 char *msg;
548
549 if (track == BRANCH_TRACK_OVERRIDE)
550 BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
551 if (clobber_head_ok && !force)
552 BUG("'clobber_head_ok' can only be used with 'force'");
553
554 if (clobber_head_ok ?
555 validate_branchname(name, &ref) :
556 validate_new_branchname(name, &ref, force)) {
557 forcing = 1;
e89f151d
GC
558 }
559
560 dwim_branch_start(r, start_name, track, &real_ref, &oid);
3f3e7608
GC
561 if (dry_run)
562 goto cleanup;
e496c003 563
d43f990f 564 if (reflog)
341fb286 565 log_all_ref_updates = LOG_REFS_NORMAL;
d43f990f 566
bc0893cf
GC
567 if (forcing)
568 msg = xstrfmt("branch: Reset to %s", start_name);
569 else
570 msg = xstrfmt("branch: Created from %s", start_name);
571 transaction = ref_transaction_begin(&err);
572 if (!transaction ||
573 ref_transaction_update(transaction, ref.buf,
574 &oid, forcing ? NULL : null_oid(),
575 0, msg, &err) ||
576 ref_transaction_commit(transaction, &err))
577 die("%s", err.buf);
578 ref_transaction_free(transaction);
579 strbuf_release(&err);
580 free(msg);
d43f990f 581
e496c003 582 if (real_ref && track)
82a0672f 583 setup_tracking(ref.buf + 11, real_ref, track, quiet);
e496c003 584
3f3e7608 585cleanup:
8415d5c7 586 strbuf_release(&ref);
9ed36cfa 587 free(real_ref);
e496c003 588}
c369e7b8 589
e89f151d
GC
590void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
591 const char *orig_ref, enum branch_track track,
592 int quiet)
593{
594 char *real_orig_ref;
595 dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);
596 setup_tracking(new_ref, real_orig_ref, track, quiet);
597}
598
961b130d
GC
599/**
600 * Creates a branch in a submodule by calling
601 * create_branches_recursively() in a child process. The child process
602 * is necessary because install_branch_config_multiple_remotes() (which
603 * is called by setup_tracking()) does not support writing configs to
604 * submodules.
605 */
606static int submodule_create_branch(struct repository *r,
607 const struct submodule *submodule,
608 const char *name, const char *start_oid,
609 const char *tracking_name, int force,
610 int reflog, int quiet,
611 enum branch_track track, int dry_run)
612{
613 int ret = 0;
614 struct child_process child = CHILD_PROCESS_INIT;
615 struct strbuf child_err = STRBUF_INIT;
616 struct strbuf out_buf = STRBUF_INIT;
617 char *out_prefix = xstrfmt("submodule '%s': ", submodule->name);
618 child.git_cmd = 1;
619 child.err = -1;
620 child.stdout_to_stderr = 1;
621
622 prepare_other_repo_env(&child.env_array, r->gitdir);
623 /*
624 * submodule_create_branch() is indirectly invoked by "git
625 * branch", but we cannot invoke "git branch" in the child
626 * process. "git branch" accepts a branch name and start point,
627 * where the start point is assumed to provide both the OID
628 * (start_oid) and the branch to use for tracking
629 * (tracking_name). But when recursing through submodules,
630 * start_oid and tracking name need to be specified separately
631 * (see create_branches_recursively()).
632 */
633 strvec_pushl(&child.args, "submodule--helper", "create-branch", NULL);
634 if (dry_run)
635 strvec_push(&child.args, "--dry-run");
636 if (force)
637 strvec_push(&child.args, "--force");
638 if (quiet)
639 strvec_push(&child.args, "--quiet");
640 if (reflog)
641 strvec_push(&child.args, "--create-reflog");
75388bf5
GC
642
643 switch (track) {
644 case BRANCH_TRACK_NEVER:
645 strvec_push(&child.args, "--no-track");
646 break;
647 case BRANCH_TRACK_ALWAYS:
648 case BRANCH_TRACK_EXPLICIT:
649 strvec_push(&child.args, "--track=direct");
650 break;
651 case BRANCH_TRACK_OVERRIDE:
652 BUG("BRANCH_TRACK_OVERRIDE cannot be used when creating a branch.");
653 break;
654 case BRANCH_TRACK_INHERIT:
655 strvec_push(&child.args, "--track=inherit");
656 break;
657 case BRANCH_TRACK_UNSPECIFIED:
1f888282 658 /* Default for "git checkout". Do not pass --track. */
75388bf5 659 case BRANCH_TRACK_REMOTE:
1f888282 660 /* Default for "git branch". Do not pass --track. */
75388bf5
GC
661 break;
662 }
961b130d
GC
663
664 strvec_pushl(&child.args, name, start_oid, tracking_name, NULL);
665
666 if ((ret = start_command(&child)))
667 return ret;
668 ret = finish_command(&child);
669 strbuf_read(&child_err, child.err, 0);
670 strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
671
672 if (ret)
673 fprintf(stderr, "%s", out_buf.buf);
674 else
675 printf("%s", out_buf.buf);
676
677 strbuf_release(&child_err);
678 strbuf_release(&out_buf);
679 return ret;
680}
681
682void create_branches_recursively(struct repository *r, const char *name,
683 const char *start_commitish,
684 const char *tracking_name, int force,
685 int reflog, int quiet, enum branch_track track,
686 int dry_run)
687{
688 int i = 0;
689 char *branch_point = NULL;
690 struct object_id super_oid;
691 struct submodule_entry_list submodule_entry_list;
692
693 /* Perform dwim on start_commitish to get super_oid and branch_point. */
694 dwim_branch_start(r, start_commitish, BRANCH_TRACK_NEVER,
695 &branch_point, &super_oid);
696
697 /*
698 * If we were not given an explicit name to track, then assume we are at
699 * the top level and, just like the non-recursive case, the tracking
700 * name is the branch point.
701 */
702 if (!tracking_name)
703 tracking_name = branch_point;
704
705 submodules_of_tree(r, &super_oid, &submodule_entry_list);
706 /*
707 * Before creating any branches, first check that the branch can
708 * be created in every submodule.
709 */
710 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
e6bf70d1 711 if (!submodule_entry_list.entries[i].repo) {
cfbda6ba
GC
712 int code = die_message(
713 _("submodule '%s': unable to find submodule"),
714 submodule_entry_list.entries[i].submodule->name);
961b130d
GC
715 if (advice_enabled(ADVICE_SUBMODULES_NOT_UPDATED))
716 advise(_("You may try updating the submodules using 'git checkout %s && git submodule update --init'"),
717 start_commitish);
cfbda6ba 718 exit(code);
961b130d
GC
719 }
720
721 if (submodule_create_branch(
722 submodule_entry_list.entries[i].repo,
723 submodule_entry_list.entries[i].submodule, name,
724 oid_to_hex(&submodule_entry_list.entries[i]
725 .name_entry->oid),
726 tracking_name, force, reflog, quiet, track, 1))
727 die(_("submodule '%s': cannot create branch '%s'"),
728 submodule_entry_list.entries[i].submodule->name,
729 name);
730 }
731
732 create_branch(the_repository, name, start_commitish, force, 0, reflog, quiet,
733 BRANCH_TRACK_NEVER, dry_run);
734 if (dry_run)
735 return;
736 /*
737 * NEEDSWORK If tracking was set up in the superproject but not the
738 * submodule, users might expect "git branch --recurse-submodules" to
739 * fail or give a warning, but this is not yet implemented because it is
740 * tedious to determine whether or not tracking was set up in the
741 * superproject.
742 */
75388bf5
GC
743 if (track)
744 setup_tracking(name, tracking_name, track, quiet);
961b130d
GC
745
746 for (i = 0; i < submodule_entry_list.entry_nr; i++) {
747 if (submodule_create_branch(
748 submodule_entry_list.entries[i].repo,
749 submodule_entry_list.entries[i].submodule, name,
750 oid_to_hex(&submodule_entry_list.entries[i]
751 .name_entry->oid),
752 tracking_name, force, reflog, quiet, track, 0))
753 die(_("submodule '%s': cannot create branch '%s'"),
754 submodule_entry_list.entries[i].submodule->name,
755 name);
756 repo_clear(submodule_entry_list.entries[i].repo);
757 }
758}
759
b6433555 760void remove_merge_branch_state(struct repository *r)
c369e7b8 761{
4edce172
NTND
762 unlink(git_path_merge_head(r));
763 unlink(git_path_merge_rr(r));
764 unlink(git_path_merge_msg(r));
765 unlink(git_path_merge_mode(r));
5291828d 766 unlink(git_path_auto_merge(r));
a03b5553 767 save_autostash(git_path_merge_autostash(r));
b6433555
NTND
768}
769
f496b064 770void remove_branch_state(struct repository *r, int verbose)
b6433555 771{
f496b064 772 sequencer_post_commit_cleanup(r, verbose);
4edce172 773 unlink(git_path_squash_msg(r));
b6433555 774 remove_merge_branch_state(r);
c369e7b8 775}
ed89f84b 776
8d9fdd70 777void die_if_checked_out(const char *branch, int ignore_current_worktree)
41af6565 778{
c8dd491f 779 struct worktree **worktrees = get_worktrees();
d3b9ac07 780 const struct worktree *wt;
41af6565 781
c8dd491f
AK
782 wt = find_shared_symref(worktrees, "HEAD", branch);
783 if (wt && (!ignore_current_worktree || !wt->is_current)) {
784 skip_prefix(branch, "refs/heads/", &branch);
785 die(_("'%s' is already checked out at '%s'"), branch, wt->path);
786 }
787
788 free_worktrees(worktrees);
ed89f84b 789}
70999e9c 790
39ee4c6c
KM
791int replace_each_worktree_head_symref(const char *oldref, const char *newref,
792 const char *logmsg)
70999e9c
KY
793{
794 int ret = 0;
03f2465b 795 struct worktree **worktrees = get_worktrees();
70999e9c
KY
796 int i;
797
798 for (i = 0; worktrees[i]; i++) {
d026a256
NTND
799 struct ref_store *refs;
800
70999e9c
KY
801 if (worktrees[i]->is_detached)
802 continue;
31824d18
NTND
803 if (!worktrees[i]->head_ref)
804 continue;
805 if (strcmp(oldref, worktrees[i]->head_ref))
70999e9c
KY
806 continue;
807
d026a256
NTND
808 refs = get_worktree_ref_store(worktrees[i]);
809 if (refs_create_symref(refs, "HEAD", newref, logmsg))
810 ret = error(_("HEAD of working tree %s is not updated"),
811 worktrees[i]->path);
70999e9c
KY
812 }
813
814 free_worktrees(worktrees);
815 return ret;
816}