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