]>
Commit | Line | Data |
---|---|---|
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" |
e496c003 DB |
11 | |
12 | struct tracking { | |
0ad4a5ff | 13 | struct refspec_item spec; |
d3115660 | 14 | struct string_list *srcs; |
e496c003 DB |
15 | const char *remote; |
16 | int matches; | |
17 | }; | |
18 | ||
19 | static int find_tracked_branch(struct remote *remote, void *priv) | |
20 | { | |
21 | struct tracking *tracking = priv; | |
22 | ||
23 | if (!remote_find_tracking(remote, &tracking->spec)) { | |
24 | if (++tracking->matches == 1) { | |
d3115660 | 25 | string_list_append(tracking->srcs, tracking->spec.src); |
e496c003 DB |
26 | tracking->remote = remote->name; |
27 | } else { | |
28 | free(tracking->spec.src); | |
d3115660 | 29 | string_list_clear(tracking->srcs, 0); |
e496c003 DB |
30 | } |
31 | tracking->spec.src = NULL; | |
32 | } | |
33 | ||
34 | return 0; | |
35 | } | |
36 | ||
a9f2c136 | 37 | static int should_setup_rebase(const char *origin) |
c998ae9b DS |
38 | { |
39 | switch (autorebase) { | |
40 | case AUTOREBASE_NEVER: | |
41 | return 0; | |
42 | case AUTOREBASE_LOCAL: | |
a9f2c136 | 43 | return origin == NULL; |
c998ae9b | 44 | case AUTOREBASE_REMOTE: |
a9f2c136 | 45 | return origin != NULL; |
c998ae9b DS |
46 | case AUTOREBASE_ALWAYS: |
47 | return 1; | |
48 | } | |
49 | return 0; | |
50 | } | |
51 | ||
a3f40ec4 JS |
52 | /** |
53 | * Install upstream tracking configuration for a branch; specifically, add | |
54 | * `branch.<name>.remote` and `branch.<name>.merge` entries. | |
55 | * | |
56 | * `flag` contains integer flags for options; currently only | |
57 | * BRANCH_CONFIG_VERBOSE is checked. | |
58 | * | |
59 | * `local` is the name of the branch whose configuration we're installing. | |
60 | * | |
61 | * `origin` is the name of the remote owning the upstream branches. NULL means | |
62 | * the upstream branches are local to this repo. | |
63 | * | |
64 | * `remotes` is a list of refs that are upstream of local | |
65 | */ | |
66 | static int install_branch_config_multiple_remotes(int flag, const char *local, | |
67 | const char *origin, struct string_list *remotes) | |
a9f2c136 | 68 | { |
cf4fff57 | 69 | const char *shortname = NULL; |
a9f2c136 | 70 | struct strbuf key = STRBUF_INIT; |
a3f40ec4 | 71 | struct string_list_item *item; |
a9f2c136 JH |
72 | int rebasing = should_setup_rebase(origin); |
73 | ||
a3f40ec4 JS |
74 | if (!remotes->nr) |
75 | BUG("must provide at least one remote for branch config"); | |
76 | if (rebasing && remotes->nr > 1) | |
77 | die(_("cannot inherit upstream tracking configuration of " | |
78 | "multiple refs when rebasing is requested")); | |
79 | ||
80 | /* | |
81 | * If the new branch is trying to track itself, something has gone | |
82 | * wrong. Warn the user and don't proceed any further. | |
83 | */ | |
84 | if (!origin) | |
85 | for_each_string_list_item(item, remotes) | |
86 | if (skip_prefix(item->string, "refs/heads/", &shortname) | |
87 | && !strcmp(local, shortname)) { | |
88 | warning(_("not setting branch '%s' as its own upstream."), | |
89 | local); | |
90 | return 0; | |
91 | } | |
85e2233f | 92 | |
a9f2c136 | 93 | strbuf_addf(&key, "branch.%s.remote", local); |
30598ad0 | 94 | if (git_config_set_gently(key.buf, origin ? origin : ".") < 0) |
27852b2c | 95 | goto out_err; |
a9f2c136 JH |
96 | |
97 | strbuf_reset(&key); | |
98 | strbuf_addf(&key, "branch.%s.merge", local); | |
a3f40ec4 JS |
99 | /* |
100 | * We want to overwrite any existing config with all the branches in | |
101 | * "remotes". Override any existing config, then write our branches. If | |
102 | * more than one is provided, use CONFIG_REGEX_NONE to preserve what | |
103 | * we've written so far. | |
104 | */ | |
105 | if (git_config_set_gently(key.buf, NULL) < 0) | |
27852b2c | 106 | goto out_err; |
a3f40ec4 JS |
107 | for_each_string_list_item(item, remotes) |
108 | if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0) | |
109 | goto out_err; | |
a9f2c136 JH |
110 | |
111 | if (rebasing) { | |
112 | strbuf_reset(&key); | |
113 | strbuf_addf(&key, "branch.%s.rebase", local); | |
30598ad0 | 114 | if (git_config_set_gently(key.buf, "true") < 0) |
27852b2c | 115 | goto out_err; |
a9f2c136 | 116 | } |
d53a3503 | 117 | strbuf_release(&key); |
a9f2c136 | 118 | |
72f60083 | 119 | if (flag & BRANCH_CONFIG_VERBOSE) { |
a3f40ec4 JS |
120 | struct strbuf tmp_ref_name = STRBUF_INIT; |
121 | struct string_list friendly_ref_names = STRING_LIST_INIT_DUP; | |
122 | ||
123 | for_each_string_list_item(item, remotes) { | |
124 | shortname = item->string; | |
125 | skip_prefix(shortname, "refs/heads/", &shortname); | |
126 | if (origin) { | |
127 | strbuf_addf(&tmp_ref_name, "%s/%s", | |
128 | origin, shortname); | |
129 | string_list_append_nodup( | |
130 | &friendly_ref_names, | |
131 | strbuf_detach(&tmp_ref_name, NULL)); | |
132 | } else { | |
133 | string_list_append( | |
134 | &friendly_ref_names, shortname); | |
135 | } | |
136 | } | |
137 | ||
138 | if (remotes->nr == 1) { | |
139 | /* | |
140 | * Rebasing is only allowed in the case of a single | |
141 | * upstream branch. | |
142 | */ | |
143 | printf_ln(rebasing ? | |
144 | _("branch '%s' set up to track '%s' by rebasing.") : | |
145 | _("branch '%s' set up to track '%s'."), | |
146 | local, friendly_ref_names.items[0].string); | |
9fe0cf3a | 147 | } else { |
a3f40ec4 JS |
148 | printf_ln(_("branch '%s' set up to track:"), local); |
149 | for_each_string_list_item(item, &friendly_ref_names) | |
150 | printf_ln(" %s", item->string); | |
9fe0cf3a | 151 | } |
a3f40ec4 JS |
152 | |
153 | string_list_clear(&friendly_ref_names, 0); | |
72f60083 | 154 | } |
27852b2c PS |
155 | |
156 | return 0; | |
157 | ||
158 | out_err: | |
159 | strbuf_release(&key); | |
160 | error(_("Unable to write upstream branch configuration")); | |
161 | ||
a3f40ec4 JS |
162 | advise(_("\nAfter fixing the error cause you may try to fix up\n" |
163 | "the remote tracking information by invoking:")); | |
164 | if (remotes->nr == 1) | |
165 | advise(" git branch --set-upstream-to=%s%s%s", | |
166 | origin ? origin : "", | |
167 | origin ? "/" : "", | |
168 | remotes->items[0].string); | |
169 | else { | |
170 | advise(" git config --add branch.\"%s\".remote %s", | |
171 | local, origin ? origin : "."); | |
172 | for_each_string_list_item(item, remotes) | |
173 | advise(" git config --add branch.\"%s\".merge %s", | |
174 | local, item->string); | |
175 | } | |
27852b2c PS |
176 | |
177 | return -1; | |
a9f2c136 JH |
178 | } |
179 | ||
a3f40ec4 JS |
180 | int install_branch_config(int flag, const char *local, const char *origin, |
181 | const char *remote) | |
182 | { | |
183 | int ret; | |
184 | struct string_list remotes = STRING_LIST_INIT_DUP; | |
185 | ||
186 | string_list_append(&remotes, remote); | |
187 | ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes); | |
188 | string_list_clear(&remotes, 0); | |
189 | return ret; | |
190 | } | |
191 | ||
d3115660 JS |
192 | static int inherit_tracking(struct tracking *tracking, const char *orig_ref) |
193 | { | |
194 | const char *bare_ref; | |
195 | struct branch *branch; | |
196 | int i; | |
197 | ||
198 | bare_ref = orig_ref; | |
199 | skip_prefix(orig_ref, "refs/heads/", &bare_ref); | |
200 | ||
201 | branch = branch_get(bare_ref); | |
202 | if (!branch->remote_name) { | |
203 | warning(_("asked to inherit tracking from '%s', but no remote is set"), | |
204 | bare_ref); | |
205 | return -1; | |
206 | } | |
207 | ||
208 | if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) { | |
209 | warning(_("asked to inherit tracking from '%s', but no merge configuration is set"), | |
210 | bare_ref); | |
211 | return -1; | |
212 | } | |
213 | ||
214 | tracking->remote = xstrdup(branch->remote_name); | |
215 | for (i = 0; i < branch->merge_nr; i++) | |
216 | string_list_append(tracking->srcs, branch->merge_name[i]); | |
217 | return 0; | |
218 | } | |
219 | ||
e496c003 | 220 | /* |
e89f151d GC |
221 | * Used internally to set the branch.<new_ref>.{remote,merge} config |
222 | * settings so that branch 'new_ref' tracks 'orig_ref'. Unlike | |
223 | * dwim_and_setup_tracking(), this does not do DWIM, i.e. "origin/main" | |
224 | * will not be expanded to "refs/remotes/origin/main", so it is not safe | |
225 | * for 'orig_ref' to be raw user input. | |
e496c003 | 226 | */ |
27852b2c PS |
227 | static void setup_tracking(const char *new_ref, const char *orig_ref, |
228 | enum branch_track track, int quiet) | |
e496c003 | 229 | { |
e496c003 | 230 | struct tracking tracking; |
d3115660 | 231 | struct string_list tracking_srcs = STRING_LIST_INIT_DUP; |
f9a482e6 | 232 | int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE; |
e496c003 | 233 | |
e496c003 DB |
234 | memset(&tracking, 0, sizeof(tracking)); |
235 | tracking.spec.dst = (char *)orig_ref; | |
d3115660 JS |
236 | tracking.srcs = &tracking_srcs; |
237 | if (track != BRANCH_TRACK_INHERIT) | |
238 | for_each_remote(find_tracked_branch, &tracking); | |
239 | else if (inherit_tracking(&tracking, orig_ref)) | |
27852b2c | 240 | return; |
e496c003 | 241 | |
9ed36cfa JS |
242 | if (!tracking.matches) |
243 | switch (track) { | |
244 | case BRANCH_TRACK_ALWAYS: | |
245 | case BRANCH_TRACK_EXPLICIT: | |
4fc50066 | 246 | case BRANCH_TRACK_OVERRIDE: |
d3115660 | 247 | case BRANCH_TRACK_INHERIT: |
9ed36cfa JS |
248 | break; |
249 | default: | |
27852b2c | 250 | return; |
9ed36cfa JS |
251 | } |
252 | ||
e496c003 | 253 | if (tracking.matches > 1) |
27852b2c PS |
254 | die(_("Not tracking: ambiguous information for ref %s"), |
255 | orig_ref); | |
e496c003 | 256 | |
d3115660 JS |
257 | if (tracking.srcs->nr < 1) |
258 | string_list_append(tracking.srcs, orig_ref); | |
259 | if (install_branch_config_multiple_remotes(config_flags, new_ref, | |
260 | tracking.remote, tracking.srcs) < 0) | |
27852b2c | 261 | exit(-1); |
e496c003 | 262 | |
d3115660 | 263 | string_list_clear(tracking.srcs, 0); |
e496c003 DB |
264 | } |
265 | ||
6f9a3321 JH |
266 | int read_branch_desc(struct strbuf *buf, const char *branch_name) |
267 | { | |
540b0f49 | 268 | char *v = NULL; |
6f9a3321 JH |
269 | struct strbuf name = STRBUF_INIT; |
270 | strbuf_addf(&name, "branch.%s.description", branch_name); | |
540b0f49 TA |
271 | if (git_config_get_string(name.buf, &v)) { |
272 | strbuf_release(&name); | |
273 | return -1; | |
274 | } | |
275 | strbuf_addstr(buf, v); | |
276 | free(v); | |
6f9a3321 JH |
277 | strbuf_release(&name); |
278 | return 0; | |
279 | } | |
280 | ||
bc1c9c0e JH |
281 | /* |
282 | * Check if 'name' can be a valid name for a branch; die otherwise. | |
283 | * Return 1 if the named branch already exists; return 0 otherwise. | |
284 | * Fill ref with the full refname for the branch. | |
285 | */ | |
286 | int validate_branchname(const char *name, struct strbuf *ref) | |
55c4a673 | 287 | { |
55c4a673 | 288 | if (strbuf_check_branch_ref(ref, name)) |
bc554df8 | 289 | die(_("'%s' is not a valid branch name."), name); |
55c4a673 | 290 | |
bc1c9c0e JH |
291 | return ref_exists(ref->buf); |
292 | } | |
55c4a673 | 293 | |
bc1c9c0e JH |
294 | /* |
295 | * Check if a branch 'name' can be created as a new branch; die otherwise. | |
296 | * 'force' can be used when it is OK for the named branch already exists. | |
297 | * Return 1 if the named branch already exists; return 0 otherwise. | |
298 | * Fill ref with the full refname for the branch. | |
299 | */ | |
300 | int validate_new_branchname(const char *name, struct strbuf *ref, int force) | |
301 | { | |
302 | const char *head; | |
303 | ||
304 | if (!validate_branchname(name, ref)) | |
55c4a673 | 305 | return 0; |
55c4a673 | 306 | |
8280c4c1 JH |
307 | if (!force) |
308 | die(_("A branch named '%s' already exists."), | |
309 | ref->buf + strlen("refs/heads/")); | |
310 | ||
311 | head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); | |
312 | if (!is_bare_repository() && head && !strcmp(head, ref->buf)) | |
313 | die(_("Cannot force update the current branch.")); | |
55c4a673 CI |
314 | |
315 | return 1; | |
316 | } | |
317 | ||
41c21f22 JH |
318 | static int check_tracking_branch(struct remote *remote, void *cb_data) |
319 | { | |
320 | char *tracking_branch = cb_data; | |
0ad4a5ff BW |
321 | struct refspec_item query; |
322 | memset(&query, 0, sizeof(struct refspec_item)); | |
41c21f22 | 323 | query.dst = tracking_branch; |
1d7358c5 | 324 | return !remote_find_tracking(remote, &query); |
41c21f22 JH |
325 | } |
326 | ||
327 | static int validate_remote_tracking_branch(char *ref) | |
328 | { | |
329 | return !for_each_remote(check_tracking_branch, ref); | |
330 | } | |
331 | ||
e2b6aa5f | 332 | static const char upstream_not_branch[] = |
1a15d00b | 333 | N_("Cannot setup tracking information; starting point '%s' is not a branch."); |
a5e91c72 | 334 | static const char upstream_missing[] = |
caa2036b JK |
335 | N_("the requested upstream branch '%s' does not exist"); |
336 | static const char upstream_advice[] = | |
337 | N_("\n" | |
338 | "If you are planning on basing your work on an upstream\n" | |
339 | "branch that already exists at the remote, you may need to\n" | |
340 | "run \"git fetch\" to retrieve it.\n" | |
341 | "\n" | |
342 | "If you are planning to push out a new local branch that\n" | |
343 | "will track its remote counterpart, you may want to use\n" | |
344 | "\"git push -u\" to set the upstream config as you push."); | |
e2b6aa5f | 345 | |
e89f151d GC |
346 | /** |
347 | * DWIMs a user-provided ref to determine the starting point for a | |
348 | * branch and validates it, where: | |
349 | * | |
350 | * - r is the repository to validate the branch for | |
351 | * | |
352 | * - start_name is the ref that we would like to test. This is | |
353 | * expanded with DWIM and assigned to out_real_ref. | |
354 | * | |
355 | * - track is the tracking mode of the new branch. If tracking is | |
356 | * explicitly requested, start_name must be a branch (because | |
357 | * otherwise start_name cannot be tracked) | |
358 | * | |
359 | * - out_oid is an out parameter containing the object_id of start_name | |
360 | * | |
361 | * - out_real_ref is an out parameter containing the full, 'real' form | |
362 | * of start_name e.g. refs/heads/main instead of main | |
363 | * | |
364 | */ | |
365 | static void dwim_branch_start(struct repository *r, const char *start_name, | |
366 | enum branch_track track, char **out_real_ref, | |
367 | struct object_id *out_oid) | |
e496c003 | 368 | { |
e496c003 | 369 | struct commit *commit; |
48713bfa | 370 | struct object_id oid; |
3818b258 | 371 | char *real_ref; |
4fc50066 IL |
372 | int explicit_tracking = 0; |
373 | ||
374 | if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE) | |
375 | explicit_tracking = 1; | |
e496c003 | 376 | |
e496c003 | 377 | real_ref = NULL; |
e3d6539d | 378 | if (get_oid_mb(start_name, &oid)) { |
caa2036b | 379 | if (explicit_tracking) { |
ed9bff08 | 380 | if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) { |
caa2036b JK |
381 | error(_(upstream_missing), start_name); |
382 | advise(_(upstream_advice)); | |
383 | exit(1); | |
384 | } | |
1a15d00b | 385 | die(_(upstream_missing), start_name); |
caa2036b | 386 | } |
bc554df8 | 387 | die(_("Not a valid object name: '%s'."), start_name); |
a5e91c72 | 388 | } |
e496c003 | 389 | |
f24c30e0 | 390 | switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) { |
e496c003 DB |
391 | case 0: |
392 | /* Not branching from any existing branch */ | |
4fc50066 | 393 | if (explicit_tracking) |
1a15d00b | 394 | die(_(upstream_not_branch), start_name); |
e496c003 DB |
395 | break; |
396 | case 1: | |
21b5b1e8 | 397 | /* Unique completion -- good, only if it is a real branch */ |
59556548 | 398 | if (!starts_with(real_ref, "refs/heads/") && |
41c21f22 | 399 | validate_remote_tracking_branch(real_ref)) { |
21b5b1e8 | 400 | if (explicit_tracking) |
1a15d00b | 401 | die(_(upstream_not_branch), start_name); |
21b5b1e8 | 402 | else |
d895804b | 403 | FREE_AND_NULL(real_ref); |
21b5b1e8 | 404 | } |
e496c003 DB |
405 | break; |
406 | default: | |
bc554df8 | 407 | die(_("Ambiguous object name: '%s'."), start_name); |
e496c003 DB |
408 | break; |
409 | } | |
410 | ||
4edce172 | 411 | if ((commit = lookup_commit_reference(r, &oid)) == NULL) |
bc554df8 | 412 | die(_("Not a valid branch point: '%s'."), start_name); |
e89f151d GC |
413 | if (out_real_ref) { |
414 | *out_real_ref = real_ref; | |
415 | real_ref = NULL; | |
416 | } | |
417 | if (out_oid) | |
418 | oidcpy(out_oid, &commit->object.oid); | |
419 | ||
420 | FREE_AND_NULL(real_ref); | |
421 | } | |
422 | ||
423 | void create_branch(struct repository *r, | |
424 | const char *name, const char *start_name, | |
425 | int force, int clobber_head_ok, int reflog, | |
426 | int quiet, enum branch_track track) | |
427 | { | |
428 | struct object_id oid; | |
429 | char *real_ref; | |
430 | struct strbuf ref = STRBUF_INIT; | |
431 | int forcing = 0; | |
bc0893cf GC |
432 | struct ref_transaction *transaction; |
433 | struct strbuf err = STRBUF_INIT; | |
434 | char *msg; | |
435 | ||
436 | if (track == BRANCH_TRACK_OVERRIDE) | |
437 | BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?"); | |
438 | if (clobber_head_ok && !force) | |
439 | BUG("'clobber_head_ok' can only be used with 'force'"); | |
440 | ||
441 | if (clobber_head_ok ? | |
442 | validate_branchname(name, &ref) : | |
443 | validate_new_branchname(name, &ref, force)) { | |
444 | forcing = 1; | |
e89f151d GC |
445 | } |
446 | ||
447 | dwim_branch_start(r, start_name, track, &real_ref, &oid); | |
e496c003 | 448 | |
d43f990f | 449 | if (reflog) |
341fb286 | 450 | log_all_ref_updates = LOG_REFS_NORMAL; |
d43f990f | 451 | |
bc0893cf GC |
452 | if (forcing) |
453 | msg = xstrfmt("branch: Reset to %s", start_name); | |
454 | else | |
455 | msg = xstrfmt("branch: Created from %s", start_name); | |
456 | transaction = ref_transaction_begin(&err); | |
457 | if (!transaction || | |
458 | ref_transaction_update(transaction, ref.buf, | |
459 | &oid, forcing ? NULL : null_oid(), | |
460 | 0, msg, &err) || | |
461 | ref_transaction_commit(transaction, &err)) | |
462 | die("%s", err.buf); | |
463 | ref_transaction_free(transaction); | |
464 | strbuf_release(&err); | |
465 | free(msg); | |
d43f990f | 466 | |
e496c003 | 467 | if (real_ref && track) |
82a0672f | 468 | setup_tracking(ref.buf + 11, real_ref, track, quiet); |
e496c003 | 469 | |
8415d5c7 | 470 | strbuf_release(&ref); |
9ed36cfa | 471 | free(real_ref); |
e496c003 | 472 | } |
c369e7b8 | 473 | |
e89f151d GC |
474 | void dwim_and_setup_tracking(struct repository *r, const char *new_ref, |
475 | const char *orig_ref, enum branch_track track, | |
476 | int quiet) | |
477 | { | |
478 | char *real_orig_ref; | |
479 | dwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL); | |
480 | setup_tracking(new_ref, real_orig_ref, track, quiet); | |
481 | } | |
482 | ||
b6433555 | 483 | void remove_merge_branch_state(struct repository *r) |
c369e7b8 | 484 | { |
4edce172 NTND |
485 | unlink(git_path_merge_head(r)); |
486 | unlink(git_path_merge_rr(r)); | |
487 | unlink(git_path_merge_msg(r)); | |
488 | unlink(git_path_merge_mode(r)); | |
5291828d | 489 | unlink(git_path_auto_merge(r)); |
a03b5553 | 490 | save_autostash(git_path_merge_autostash(r)); |
b6433555 NTND |
491 | } |
492 | ||
f496b064 | 493 | void remove_branch_state(struct repository *r, int verbose) |
b6433555 | 494 | { |
f496b064 | 495 | sequencer_post_commit_cleanup(r, verbose); |
4edce172 | 496 | unlink(git_path_squash_msg(r)); |
b6433555 | 497 | remove_merge_branch_state(r); |
c369e7b8 | 498 | } |
ed89f84b | 499 | |
8d9fdd70 | 500 | void die_if_checked_out(const char *branch, int ignore_current_worktree) |
41af6565 | 501 | { |
d3b9ac07 | 502 | const struct worktree *wt; |
41af6565 | 503 | |
d3b9ac07 | 504 | wt = find_shared_symref("HEAD", branch); |
8d9fdd70 | 505 | if (!wt || (ignore_current_worktree && wt->is_current)) |
d3b9ac07 NTND |
506 | return; |
507 | skip_prefix(branch, "refs/heads/", &branch); | |
508 | die(_("'%s' is already checked out at '%s'"), | |
509 | branch, wt->path); | |
ed89f84b | 510 | } |
70999e9c | 511 | |
39ee4c6c KM |
512 | int replace_each_worktree_head_symref(const char *oldref, const char *newref, |
513 | const char *logmsg) | |
70999e9c KY |
514 | { |
515 | int ret = 0; | |
03f2465b | 516 | struct worktree **worktrees = get_worktrees(); |
70999e9c KY |
517 | int i; |
518 | ||
519 | for (i = 0; worktrees[i]; i++) { | |
d026a256 NTND |
520 | struct ref_store *refs; |
521 | ||
70999e9c KY |
522 | if (worktrees[i]->is_detached) |
523 | continue; | |
31824d18 NTND |
524 | if (!worktrees[i]->head_ref) |
525 | continue; | |
526 | if (strcmp(oldref, worktrees[i]->head_ref)) | |
70999e9c KY |
527 | continue; |
528 | ||
d026a256 NTND |
529 | refs = get_worktree_ref_store(worktrees[i]); |
530 | if (refs_create_symref(refs, "HEAD", newref, logmsg)) | |
531 | ret = error(_("HEAD of working tree %s is not updated"), | |
532 | worktrees[i]->path); | |
70999e9c KY |
533 | } |
534 | ||
535 | free_worktrees(worktrees); | |
536 | return ret; | |
537 | } |