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