]> git.ipfire.org Git - thirdparty/git.git/blame - branch.c
Merge branch 'js/branch-track-inherit' into gc/branch-recurse-submodules
[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)) {
88 warning(_("not setting branch '%s' as its own upstream."),
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);
160 error(_("Unable to write upstream branch configuration"));
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)
27852b2c
PS
252 die(_("Not tracking: ambiguous information for ref %s"),
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))
bc554df8 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{
300 const char *head;
301
302 if (!validate_branchname(name, ref))
55c4a673 303 return 0;
55c4a673 304
8280c4c1
JH
305 if (!force)
306 die(_("A branch named '%s' already exists."),
307 ref->buf + strlen("refs/heads/"));
308
309 head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
310 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
311 die(_("Cannot force update the current branch."));
55c4a673
CI
312
313 return 1;
314}
315
41c21f22
JH
316static int check_tracking_branch(struct remote *remote, void *cb_data)
317{
318 char *tracking_branch = cb_data;
0ad4a5ff
BW
319 struct refspec_item query;
320 memset(&query, 0, sizeof(struct refspec_item));
41c21f22 321 query.dst = tracking_branch;
1d7358c5 322 return !remote_find_tracking(remote, &query);
41c21f22
JH
323}
324
325static int validate_remote_tracking_branch(char *ref)
326{
327 return !for_each_remote(check_tracking_branch, ref);
328}
329
e2b6aa5f 330static const char upstream_not_branch[] =
1a15d00b 331N_("Cannot setup tracking information; starting point '%s' is not a branch.");
a5e91c72 332static const char upstream_missing[] =
caa2036b
JK
333N_("the requested upstream branch '%s' does not exist");
334static const char upstream_advice[] =
335N_("\n"
336"If you are planning on basing your work on an upstream\n"
337"branch that already exists at the remote, you may need to\n"
338"run \"git fetch\" to retrieve it.\n"
339"\n"
340"If you are planning to push out a new local branch that\n"
341"will track its remote counterpart, you may want to use\n"
342"\"git push -u\" to set the upstream config as you push.");
e2b6aa5f 343
4edce172
NTND
344void create_branch(struct repository *r,
345 const char *name, const char *start_name,
e2bbd0cc 346 int force, int clobber_head_ok, int reflog,
f9a482e6 347 int quiet, enum branch_track track)
e496c003 348{
e496c003 349 struct commit *commit;
48713bfa 350 struct object_id oid;
3818b258 351 char *real_ref;
8415d5c7 352 struct strbuf ref = STRBUF_INIT;
e496c003 353 int forcing = 0;
4fc50066
IL
354 int dont_change_ref = 0;
355 int explicit_tracking = 0;
356
357 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
358 explicit_tracking = 1;
e496c003 359
0faff988 360 if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
bc1c9c0e
JH
361 ? validate_branchname(name, &ref)
362 : validate_new_branchname(name, &ref, force)) {
55c4a673 363 if (!force)
4fc50066 364 dont_change_ref = 1;
55c4a673
CI
365 else
366 forcing = 1;
e496c003
DB
367 }
368
369 real_ref = NULL;
e3d6539d 370 if (get_oid_mb(start_name, &oid)) {
caa2036b 371 if (explicit_tracking) {
ed9bff08 372 if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) {
caa2036b
JK
373 error(_(upstream_missing), start_name);
374 advise(_(upstream_advice));
375 exit(1);
376 }
1a15d00b 377 die(_(upstream_missing), start_name);
caa2036b 378 }
bc554df8 379 die(_("Not a valid object name: '%s'."), start_name);
a5e91c72 380 }
e496c003 381
f24c30e0 382 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
e496c003
DB
383 case 0:
384 /* Not branching from any existing branch */
4fc50066 385 if (explicit_tracking)
1a15d00b 386 die(_(upstream_not_branch), start_name);
e496c003
DB
387 break;
388 case 1:
21b5b1e8 389 /* Unique completion -- good, only if it is a real branch */
59556548 390 if (!starts_with(real_ref, "refs/heads/") &&
41c21f22 391 validate_remote_tracking_branch(real_ref)) {
21b5b1e8 392 if (explicit_tracking)
1a15d00b 393 die(_(upstream_not_branch), start_name);
21b5b1e8 394 else
d895804b 395 FREE_AND_NULL(real_ref);
21b5b1e8 396 }
e496c003
DB
397 break;
398 default:
bc554df8 399 die(_("Ambiguous object name: '%s'."), start_name);
e496c003
DB
400 break;
401 }
402
4edce172 403 if ((commit = lookup_commit_reference(r, &oid)) == NULL)
bc554df8 404 die(_("Not a valid branch point: '%s'."), start_name);
48713bfa 405 oidcpy(&oid, &commit->object.oid);
e496c003 406
d43f990f 407 if (reflog)
341fb286 408 log_all_ref_updates = LOG_REFS_NORMAL;
d43f990f
RS
409
410 if (!dont_change_ref) {
411 struct ref_transaction *transaction;
412 struct strbuf err = STRBUF_INIT;
cddac452 413 char *msg;
3818b258
JK
414
415 if (forcing)
cddac452 416 msg = xstrfmt("branch: Reset to %s", start_name);
3818b258 417 else
cddac452 418 msg = xstrfmt("branch: Created from %s", start_name);
d43f990f
RS
419
420 transaction = ref_transaction_begin(&err);
421 if (!transaction ||
1d147bdf 422 ref_transaction_update(transaction, ref.buf,
14228447 423 &oid, forcing ? NULL : null_oid(),
1d147bdf 424 0, msg, &err) ||
db7516ab 425 ref_transaction_commit(transaction, &err))
d43f990f
RS
426 die("%s", err.buf);
427 ref_transaction_free(transaction);
428 strbuf_release(&err);
cddac452 429 free(msg);
d43f990f
RS
430 }
431
e496c003 432 if (real_ref && track)
82a0672f 433 setup_tracking(ref.buf + 11, real_ref, track, quiet);
e496c003 434
8415d5c7 435 strbuf_release(&ref);
9ed36cfa 436 free(real_ref);
e496c003 437}
c369e7b8 438
b6433555 439void remove_merge_branch_state(struct repository *r)
c369e7b8 440{
4edce172
NTND
441 unlink(git_path_merge_head(r));
442 unlink(git_path_merge_rr(r));
443 unlink(git_path_merge_msg(r));
444 unlink(git_path_merge_mode(r));
5291828d 445 unlink(git_path_auto_merge(r));
a03b5553 446 save_autostash(git_path_merge_autostash(r));
b6433555
NTND
447}
448
f496b064 449void remove_branch_state(struct repository *r, int verbose)
b6433555 450{
f496b064 451 sequencer_post_commit_cleanup(r, verbose);
4edce172 452 unlink(git_path_squash_msg(r));
b6433555 453 remove_merge_branch_state(r);
c369e7b8 454}
ed89f84b 455
8d9fdd70 456void die_if_checked_out(const char *branch, int ignore_current_worktree)
41af6565 457{
d3b9ac07 458 const struct worktree *wt;
41af6565 459
d3b9ac07 460 wt = find_shared_symref("HEAD", branch);
8d9fdd70 461 if (!wt || (ignore_current_worktree && wt->is_current))
d3b9ac07
NTND
462 return;
463 skip_prefix(branch, "refs/heads/", &branch);
464 die(_("'%s' is already checked out at '%s'"),
465 branch, wt->path);
ed89f84b 466}
70999e9c 467
39ee4c6c
KM
468int replace_each_worktree_head_symref(const char *oldref, const char *newref,
469 const char *logmsg)
70999e9c
KY
470{
471 int ret = 0;
03f2465b 472 struct worktree **worktrees = get_worktrees();
70999e9c
KY
473 int i;
474
475 for (i = 0; worktrees[i]; i++) {
d026a256
NTND
476 struct ref_store *refs;
477
70999e9c
KY
478 if (worktrees[i]->is_detached)
479 continue;
31824d18
NTND
480 if (!worktrees[i]->head_ref)
481 continue;
482 if (strcmp(oldref, worktrees[i]->head_ref))
70999e9c
KY
483 continue;
484
d026a256
NTND
485 refs = get_worktree_ref_store(worktrees[i]);
486 if (refs_create_symref(refs, "HEAD", newref, logmsg))
487 ret = error(_("HEAD of working tree %s is not updated"),
488 worktrees[i]->path);
70999e9c
KY
489 }
490
491 free_worktrees(worktrees);
492 return ret;
493}