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