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