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