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