]> git.ipfire.org Git - thirdparty/git.git/blame - branch.c
t5312: prepare for reftable
[thirdparty/git.git] / branch.c
CommitLineData
303d1d0b 1#include "git-compat-util.h"
e496c003 2#include "cache.h"
b2141fc1 3#include "config.h"
e496c003
DB
4#include "branch.h"
5#include "refs.h"
ec0cb496 6#include "refspec.h"
e496c003 7#include "remote.h"
b07d9bfd 8#include "sequencer.h"
e496c003 9#include "commit.h"
ac6c561b 10#include "worktree.h"
e496c003
DB
11
12struct tracking {
0ad4a5ff 13 struct refspec_item spec;
d3115660 14 struct string_list *srcs;
e496c003
DB
15 const char *remote;
16 int matches;
17};
18
19static int find_tracked_branch(struct remote *remote, void *priv)
20{
21 struct tracking *tracking = priv;
22
23 if (!remote_find_tracking(remote, &tracking->spec)) {
24 if (++tracking->matches == 1) {
d3115660 25 string_list_append(tracking->srcs, tracking->spec.src);
e496c003
DB
26 tracking->remote = remote->name;
27 } else {
28 free(tracking->spec.src);
d3115660 29 string_list_clear(tracking->srcs, 0);
e496c003
DB
30 }
31 tracking->spec.src = NULL;
32 }
33
34 return 0;
35}
36
a9f2c136 37static int should_setup_rebase(const char *origin)
c998ae9b
DS
38{
39 switch (autorebase) {
40 case AUTOREBASE_NEVER:
41 return 0;
42 case AUTOREBASE_LOCAL:
a9f2c136 43 return origin == NULL;
c998ae9b 44 case AUTOREBASE_REMOTE:
a9f2c136 45 return origin != NULL;
c998ae9b
DS
46 case AUTOREBASE_ALWAYS:
47 return 1;
48 }
49 return 0;
50}
51
a3f40ec4
JS
52/**
53 * Install upstream tracking configuration for a branch; specifically, add
54 * `branch.<name>.remote` and `branch.<name>.merge` entries.
55 *
56 * `flag` contains integer flags for options; currently only
57 * BRANCH_CONFIG_VERBOSE is checked.
58 *
59 * `local` is the name of the branch whose configuration we're installing.
60 *
61 * `origin` is the name of the remote owning the upstream branches. NULL means
62 * the upstream branches are local to this repo.
63 *
64 * `remotes` is a list of refs that are upstream of local
65 */
66static int install_branch_config_multiple_remotes(int flag, const char *local,
67 const char *origin, struct string_list *remotes)
a9f2c136 68{
cf4fff57 69 const char *shortname = NULL;
a9f2c136 70 struct strbuf key = STRBUF_INIT;
a3f40ec4 71 struct string_list_item *item;
a9f2c136
JH
72 int rebasing = should_setup_rebase(origin);
73
a3f40ec4
JS
74 if (!remotes->nr)
75 BUG("must provide at least one remote for branch config");
76 if (rebasing && remotes->nr > 1)
77 die(_("cannot inherit upstream tracking configuration of "
78 "multiple refs when rebasing is requested"));
79
80 /*
81 * If the new branch is trying to track itself, something has gone
82 * wrong. Warn the user and don't proceed any further.
83 */
84 if (!origin)
85 for_each_string_list_item(item, remotes)
86 if (skip_prefix(item->string, "refs/heads/", &shortname)
87 && !strcmp(local, shortname)) {
0669bdf4 88 warning(_("not setting branch '%s' as its own upstream"),
a3f40ec4
JS
89 local);
90 return 0;
91 }
85e2233f 92
a9f2c136 93 strbuf_addf(&key, "branch.%s.remote", local);
30598ad0 94 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
27852b2c 95 goto out_err;
a9f2c136
JH
96
97 strbuf_reset(&key);
98 strbuf_addf(&key, "branch.%s.merge", local);
a3f40ec4
JS
99 /*
100 * We want to overwrite any existing config with all the branches in
101 * "remotes". Override any existing config, then write our branches. If
102 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
103 * we've written so far.
104 */
105 if (git_config_set_gently(key.buf, NULL) < 0)
27852b2c 106 goto out_err;
a3f40ec4
JS
107 for_each_string_list_item(item, remotes)
108 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
109 goto out_err;
a9f2c136
JH
110
111 if (rebasing) {
112 strbuf_reset(&key);
113 strbuf_addf(&key, "branch.%s.rebase", local);
30598ad0 114 if (git_config_set_gently(key.buf, "true") < 0)
27852b2c 115 goto out_err;
a9f2c136 116 }
d53a3503 117 strbuf_release(&key);
a9f2c136 118
72f60083 119 if (flag & BRANCH_CONFIG_VERBOSE) {
a3f40ec4
JS
120 struct strbuf tmp_ref_name = STRBUF_INIT;
121 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
122
123 for_each_string_list_item(item, remotes) {
124 shortname = item->string;
125 skip_prefix(shortname, "refs/heads/", &shortname);
126 if (origin) {
127 strbuf_addf(&tmp_ref_name, "%s/%s",
128 origin, shortname);
129 string_list_append_nodup(
130 &friendly_ref_names,
131 strbuf_detach(&tmp_ref_name, NULL));
132 } else {
133 string_list_append(
134 &friendly_ref_names, shortname);
135 }
136 }
137
138 if (remotes->nr == 1) {
139 /*
140 * Rebasing is only allowed in the case of a single
141 * upstream branch.
142 */
143 printf_ln(rebasing ?
144 _("branch '%s' set up to track '%s' by rebasing.") :
145 _("branch '%s' set up to track '%s'."),
146 local, friendly_ref_names.items[0].string);
9fe0cf3a 147 } else {
a3f40ec4
JS
148 printf_ln(_("branch '%s' set up to track:"), local);
149 for_each_string_list_item(item, &friendly_ref_names)
150 printf_ln(" %s", item->string);
9fe0cf3a 151 }
a3f40ec4
JS
152
153 string_list_clear(&friendly_ref_names, 0);
72f60083 154 }
27852b2c
PS
155
156 return 0;
157
158out_err:
159 strbuf_release(&key);
7435e7e2 160 error(_("unable to write upstream branch configuration"));
27852b2c 161
a3f40ec4
JS
162 advise(_("\nAfter fixing the error cause you may try to fix up\n"
163 "the remote tracking information by invoking:"));
164 if (remotes->nr == 1)
165 advise(" git branch --set-upstream-to=%s%s%s",
166 origin ? origin : "",
167 origin ? "/" : "",
168 remotes->items[0].string);
169 else {
170 advise(" git config --add branch.\"%s\".remote %s",
171 local, origin ? origin : ".");
172 for_each_string_list_item(item, remotes)
173 advise(" git config --add branch.\"%s\".merge %s",
174 local, item->string);
175 }
27852b2c
PS
176
177 return -1;
a9f2c136
JH
178}
179
a3f40ec4
JS
180int install_branch_config(int flag, const char *local, const char *origin,
181 const char *remote)
182{
183 int ret;
184 struct string_list remotes = STRING_LIST_INIT_DUP;
185
186 string_list_append(&remotes, remote);
187 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
188 string_list_clear(&remotes, 0);
189 return ret;
190}
191
d3115660
JS
192static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
193{
194 const char *bare_ref;
195 struct branch *branch;
196 int i;
197
198 bare_ref = orig_ref;
199 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
200
201 branch = branch_get(bare_ref);
202 if (!branch->remote_name) {
203 warning(_("asked to inherit tracking from '%s', but no remote is set"),
204 bare_ref);
205 return -1;
206 }
207
208 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
209 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
210 bare_ref);
211 return -1;
212 }
213
214 tracking->remote = xstrdup(branch->remote_name);
215 for (i = 0; i < branch->merge_nr; i++)
216 string_list_append(tracking->srcs, branch->merge_name[i]);
217 return 0;
218}
219
e496c003
DB
220/*
221 * This is called when new_ref is branched off of orig_ref, and tries
222 * to infer the settings for branch.<new_ref>.{remote,merge} from the
223 * config.
224 */
27852b2c
PS
225static void setup_tracking(const char *new_ref, const char *orig_ref,
226 enum branch_track track, int quiet)
e496c003 227{
e496c003 228 struct tracking tracking;
d3115660 229 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
f9a482e6 230 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
e496c003 231
e496c003
DB
232 memset(&tracking, 0, sizeof(tracking));
233 tracking.spec.dst = (char *)orig_ref;
d3115660
JS
234 tracking.srcs = &tracking_srcs;
235 if (track != BRANCH_TRACK_INHERIT)
236 for_each_remote(find_tracked_branch, &tracking);
237 else if (inherit_tracking(&tracking, orig_ref))
27852b2c 238 return;
e496c003 239
9ed36cfa
JS
240 if (!tracking.matches)
241 switch (track) {
242 case BRANCH_TRACK_ALWAYS:
243 case BRANCH_TRACK_EXPLICIT:
4fc50066 244 case BRANCH_TRACK_OVERRIDE:
d3115660 245 case BRANCH_TRACK_INHERIT:
9ed36cfa
JS
246 break;
247 default:
27852b2c 248 return;
9ed36cfa
JS
249 }
250
e496c003 251 if (tracking.matches > 1)
7435e7e2 252 die(_("not tracking: ambiguous information for ref %s"),
27852b2c 253 orig_ref);
e496c003 254
d3115660
JS
255 if (tracking.srcs->nr < 1)
256 string_list_append(tracking.srcs, orig_ref);
257 if (install_branch_config_multiple_remotes(config_flags, new_ref,
258 tracking.remote, tracking.srcs) < 0)
27852b2c 259 exit(-1);
e496c003 260
d3115660 261 string_list_clear(tracking.srcs, 0);
e496c003
DB
262}
263
6f9a3321
JH
264int read_branch_desc(struct strbuf *buf, const char *branch_name)
265{
540b0f49 266 char *v = NULL;
6f9a3321
JH
267 struct strbuf name = STRBUF_INIT;
268 strbuf_addf(&name, "branch.%s.description", branch_name);
540b0f49
TA
269 if (git_config_get_string(name.buf, &v)) {
270 strbuf_release(&name);
271 return -1;
272 }
273 strbuf_addstr(buf, v);
274 free(v);
6f9a3321
JH
275 strbuf_release(&name);
276 return 0;
277}
278
bc1c9c0e
JH
279/*
280 * Check if 'name' can be a valid name for a branch; die otherwise.
281 * Return 1 if the named branch already exists; return 0 otherwise.
282 * Fill ref with the full refname for the branch.
283 */
284int validate_branchname(const char *name, struct strbuf *ref)
55c4a673 285{
55c4a673 286 if (strbuf_check_branch_ref(ref, name))
7435e7e2 287 die(_("'%s' is not a valid branch name"), name);
55c4a673 288
bc1c9c0e
JH
289 return ref_exists(ref->buf);
290}
55c4a673 291
bc1c9c0e
JH
292/*
293 * Check if a branch 'name' can be created as a new branch; die otherwise.
294 * 'force' can be used when it is OK for the named branch already exists.
295 * Return 1 if the named branch already exists; return 0 otherwise.
296 * Fill ref with the full refname for the branch.
297 */
298int validate_new_branchname(const char *name, struct strbuf *ref, int force)
299{
593a2a5d
AK
300 struct worktree **worktrees;
301 const struct worktree *wt;
bc1c9c0e
JH
302
303 if (!validate_branchname(name, ref))
55c4a673 304 return 0;
55c4a673 305
8280c4c1 306 if (!force)
7435e7e2 307 die(_("a branch named '%s' already exists"),
8280c4c1
JH
308 ref->buf + strlen("refs/heads/"));
309
593a2a5d
AK
310 worktrees = get_worktrees();
311 wt = find_shared_symref(worktrees, "HEAD", ref->buf);
312 if (wt && !wt->is_bare)
68d924e1 313 die(_("cannot force update the branch '%s' "
593a2a5d
AK
314 "checked out at '%s'"),
315 ref->buf + strlen("refs/heads/"), wt->path);
316 free_worktrees(worktrees);
55c4a673
CI
317
318 return 1;
319}
320
41c21f22
JH
321static int check_tracking_branch(struct remote *remote, void *cb_data)
322{
323 char *tracking_branch = cb_data;
0ad4a5ff
BW
324 struct refspec_item query;
325 memset(&query, 0, sizeof(struct refspec_item));
41c21f22 326 query.dst = tracking_branch;
1d7358c5 327 return !remote_find_tracking(remote, &query);
41c21f22
JH
328}
329
330static int validate_remote_tracking_branch(char *ref)
331{
332 return !for_each_remote(check_tracking_branch, ref);
333}
334
e2b6aa5f 335static const char upstream_not_branch[] =
7435e7e2 336N_("cannot set up tracking information; starting point '%s' is not a branch");
a5e91c72 337static const char upstream_missing[] =
caa2036b
JK
338N_("the requested upstream branch '%s' does not exist");
339static const char upstream_advice[] =
340N_("\n"
341"If you are planning on basing your work on an upstream\n"
342"branch that already exists at the remote, you may need to\n"
343"run \"git fetch\" to retrieve it.\n"
344"\n"
345"If you are planning to push out a new local branch that\n"
346"will track its remote counterpart, you may want to use\n"
347"\"git push -u\" to set the upstream config as you push.");
e2b6aa5f 348
4edce172
NTND
349void create_branch(struct repository *r,
350 const char *name, const char *start_name,
e2bbd0cc 351 int force, int clobber_head_ok, int reflog,
f9a482e6 352 int quiet, enum branch_track track)
e496c003 353{
e496c003 354 struct commit *commit;
48713bfa 355 struct object_id oid;
3818b258 356 char *real_ref;
8415d5c7 357 struct strbuf ref = STRBUF_INIT;
e496c003 358 int forcing = 0;
4fc50066
IL
359 int dont_change_ref = 0;
360 int explicit_tracking = 0;
361
362 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
363 explicit_tracking = 1;
e496c003 364
0faff988 365 if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
bc1c9c0e
JH
366 ? validate_branchname(name, &ref)
367 : validate_new_branchname(name, &ref, force)) {
55c4a673 368 if (!force)
4fc50066 369 dont_change_ref = 1;
55c4a673
CI
370 else
371 forcing = 1;
e496c003
DB
372 }
373
374 real_ref = NULL;
e3d6539d 375 if (get_oid_mb(start_name, &oid)) {
caa2036b 376 if (explicit_tracking) {
ed9bff08 377 if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) {
caa2036b
JK
378 error(_(upstream_missing), start_name);
379 advise(_(upstream_advice));
380 exit(1);
381 }
1a15d00b 382 die(_(upstream_missing), start_name);
caa2036b 383 }
7435e7e2 384 die(_("not a valid object name: '%s'"), start_name);
a5e91c72 385 }
e496c003 386
f24c30e0 387 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
e496c003
DB
388 case 0:
389 /* Not branching from any existing branch */
4fc50066 390 if (explicit_tracking)
1a15d00b 391 die(_(upstream_not_branch), start_name);
e496c003
DB
392 break;
393 case 1:
21b5b1e8 394 /* Unique completion -- good, only if it is a real branch */
59556548 395 if (!starts_with(real_ref, "refs/heads/") &&
41c21f22 396 validate_remote_tracking_branch(real_ref)) {
21b5b1e8 397 if (explicit_tracking)
1a15d00b 398 die(_(upstream_not_branch), start_name);
21b5b1e8 399 else
d895804b 400 FREE_AND_NULL(real_ref);
21b5b1e8 401 }
e496c003
DB
402 break;
403 default:
7435e7e2 404 die(_("ambiguous object name: '%s'"), start_name);
e496c003
DB
405 break;
406 }
407
4edce172 408 if ((commit = lookup_commit_reference(r, &oid)) == NULL)
7435e7e2 409 die(_("not a valid branch point: '%s'"), start_name);
48713bfa 410 oidcpy(&oid, &commit->object.oid);
e496c003 411
d43f990f 412 if (reflog)
341fb286 413 log_all_ref_updates = LOG_REFS_NORMAL;
d43f990f
RS
414
415 if (!dont_change_ref) {
416 struct ref_transaction *transaction;
417 struct strbuf err = STRBUF_INIT;
cddac452 418 char *msg;
3818b258
JK
419
420 if (forcing)
cddac452 421 msg = xstrfmt("branch: Reset to %s", start_name);
3818b258 422 else
cddac452 423 msg = xstrfmt("branch: Created from %s", start_name);
d43f990f
RS
424
425 transaction = ref_transaction_begin(&err);
426 if (!transaction ||
1d147bdf 427 ref_transaction_update(transaction, ref.buf,
14228447 428 &oid, forcing ? NULL : null_oid(),
1d147bdf 429 0, msg, &err) ||
db7516ab 430 ref_transaction_commit(transaction, &err))
d43f990f
RS
431 die("%s", err.buf);
432 ref_transaction_free(transaction);
433 strbuf_release(&err);
cddac452 434 free(msg);
d43f990f
RS
435 }
436
e496c003 437 if (real_ref && track)
82a0672f 438 setup_tracking(ref.buf + 11, real_ref, track, quiet);
e496c003 439
8415d5c7 440 strbuf_release(&ref);
9ed36cfa 441 free(real_ref);
e496c003 442}
c369e7b8 443
b6433555 444void remove_merge_branch_state(struct repository *r)
c369e7b8 445{
4edce172
NTND
446 unlink(git_path_merge_head(r));
447 unlink(git_path_merge_rr(r));
448 unlink(git_path_merge_msg(r));
449 unlink(git_path_merge_mode(r));
5291828d 450 unlink(git_path_auto_merge(r));
a03b5553 451 save_autostash(git_path_merge_autostash(r));
b6433555
NTND
452}
453
f496b064 454void remove_branch_state(struct repository *r, int verbose)
b6433555 455{
f496b064 456 sequencer_post_commit_cleanup(r, verbose);
4edce172 457 unlink(git_path_squash_msg(r));
b6433555 458 remove_merge_branch_state(r);
c369e7b8 459}
ed89f84b 460
8d9fdd70 461void die_if_checked_out(const char *branch, int ignore_current_worktree)
41af6565 462{
c8dd491f 463 struct worktree **worktrees = get_worktrees();
d3b9ac07 464 const struct worktree *wt;
41af6565 465
c8dd491f
AK
466 wt = find_shared_symref(worktrees, "HEAD", branch);
467 if (wt && (!ignore_current_worktree || !wt->is_current)) {
468 skip_prefix(branch, "refs/heads/", &branch);
469 die(_("'%s' is already checked out at '%s'"), branch, wt->path);
470 }
471
472 free_worktrees(worktrees);
ed89f84b 473}
70999e9c 474
39ee4c6c
KM
475int replace_each_worktree_head_symref(const char *oldref, const char *newref,
476 const char *logmsg)
70999e9c
KY
477{
478 int ret = 0;
03f2465b 479 struct worktree **worktrees = get_worktrees();
70999e9c
KY
480 int i;
481
482 for (i = 0; worktrees[i]; i++) {
d026a256
NTND
483 struct ref_store *refs;
484
70999e9c
KY
485 if (worktrees[i]->is_detached)
486 continue;
31824d18
NTND
487 if (!worktrees[i]->head_ref)
488 continue;
489 if (strcmp(oldref, worktrees[i]->head_ref))
70999e9c
KY
490 continue;
491
d026a256
NTND
492 refs = get_worktree_ref_store(worktrees[i]);
493 if (refs_create_symref(refs, "HEAD", newref, logmsg))
494 ret = error(_("HEAD of working tree %s is not updated"),
495 worktrees[i]->path);
70999e9c
KY
496 }
497
498 free_worktrees(worktrees);
499 return ret;
500}