refspec: rename struct refspec to struct refspec_item
[git/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
DB
7#include "remote.h"
8#include "commit.h"
ac6c561b 9#include "worktree.h"
e496c003
DB
10
11struct tracking {
0ad4a5ff 12 struct refspec_item spec;
e496c003
DB
13 char *src;
14 const char *remote;
15 int matches;
16};
17
18static int find_tracked_branch(struct remote *remote, void *priv)
19{
20 struct tracking *tracking = priv;
21
22 if (!remote_find_tracking(remote, &tracking->spec)) {
23 if (++tracking->matches == 1) {
24 tracking->src = tracking->spec.src;
25 tracking->remote = remote->name;
26 } else {
27 free(tracking->spec.src);
28 if (tracking->src) {
6a83d902 29 FREE_AND_NULL(tracking->src);
e496c003
DB
30 }
31 }
32 tracking->spec.src = NULL;
33 }
34
35 return 0;
36}
37
a9f2c136 38static int should_setup_rebase(const char *origin)
c998ae9b
DS
39{
40 switch (autorebase) {
41 case AUTOREBASE_NEVER:
42 return 0;
43 case AUTOREBASE_LOCAL:
a9f2c136 44 return origin == NULL;
c998ae9b 45 case AUTOREBASE_REMOTE:
a9f2c136 46 return origin != NULL;
c998ae9b
DS
47 case AUTOREBASE_ALWAYS:
48 return 1;
49 }
50 return 0;
51}
52
27852b2c
PS
53static const char tracking_advice[] =
54N_("\n"
55"After fixing the error cause you may try to fix up\n"
56"the remote tracking information by invoking\n"
57"\"git branch --set-upstream-to=%s%s%s\".");
58
59int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
a9f2c136 60{
cf4fff57 61 const char *shortname = NULL;
a9f2c136
JH
62 struct strbuf key = STRBUF_INIT;
63 int rebasing = should_setup_rebase(origin);
64
cf4fff57 65 if (skip_prefix(remote, "refs/heads/", &shortname)
85e2233f
MM
66 && !strcmp(local, shortname)
67 && !origin) {
bc554df8 68 warning(_("Not setting branch %s as its own upstream."),
85e2233f 69 local);
27852b2c 70 return 0;
85e2233f
MM
71 }
72
a9f2c136 73 strbuf_addf(&key, "branch.%s.remote", local);
30598ad0 74 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
27852b2c 75 goto out_err;
a9f2c136
JH
76
77 strbuf_reset(&key);
78 strbuf_addf(&key, "branch.%s.merge", local);
30598ad0 79 if (git_config_set_gently(key.buf, remote) < 0)
27852b2c 80 goto out_err;
a9f2c136
JH
81
82 if (rebasing) {
83 strbuf_reset(&key);
84 strbuf_addf(&key, "branch.%s.rebase", local);
30598ad0 85 if (git_config_set_gently(key.buf, "true") < 0)
27852b2c 86 goto out_err;
a9f2c136 87 }
d53a3503 88 strbuf_release(&key);
a9f2c136 89
72f60083 90 if (flag & BRANCH_CONFIG_VERBOSE) {
1d9aaed2 91 if (shortname) {
9fe0cf3a
A
92 if (origin)
93 printf_ln(rebasing ?
9c93ff7c
KS
94 _("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") :
95 _("Branch '%s' set up to track remote branch '%s' from '%s'."),
9fe0cf3a
A
96 local, shortname, origin);
97 else
98 printf_ln(rebasing ?
9c93ff7c
KS
99 _("Branch '%s' set up to track local branch '%s' by rebasing.") :
100 _("Branch '%s' set up to track local branch '%s'."),
9fe0cf3a
A
101 local, shortname);
102 } else {
103 if (origin)
104 printf_ln(rebasing ?
9c93ff7c
KS
105 _("Branch '%s' set up to track remote ref '%s' by rebasing.") :
106 _("Branch '%s' set up to track remote ref '%s'."),
9fe0cf3a
A
107 local, remote);
108 else
109 printf_ln(rebasing ?
9c93ff7c
KS
110 _("Branch '%s' set up to track local ref '%s' by rebasing.") :
111 _("Branch '%s' set up to track local ref '%s'."),
9fe0cf3a
A
112 local, remote);
113 }
72f60083 114 }
27852b2c
PS
115
116 return 0;
117
118out_err:
119 strbuf_release(&key);
120 error(_("Unable to write upstream branch configuration"));
121
122 advise(_(tracking_advice),
123 origin ? origin : "",
124 origin ? "/" : "",
125 shortname ? shortname : remote);
126
127 return -1;
a9f2c136
JH
128}
129
e496c003
DB
130/*
131 * This is called when new_ref is branched off of orig_ref, and tries
132 * to infer the settings for branch.<new_ref>.{remote,merge} from the
133 * config.
134 */
27852b2c
PS
135static void setup_tracking(const char *new_ref, const char *orig_ref,
136 enum branch_track track, int quiet)
e496c003 137{
e496c003 138 struct tracking tracking;
f9a482e6 139 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
e496c003 140
e496c003
DB
141 memset(&tracking, 0, sizeof(tracking));
142 tracking.spec.dst = (char *)orig_ref;
9ed36cfa 143 if (for_each_remote(find_tracked_branch, &tracking))
27852b2c 144 return;
e496c003 145
9ed36cfa
JS
146 if (!tracking.matches)
147 switch (track) {
148 case BRANCH_TRACK_ALWAYS:
149 case BRANCH_TRACK_EXPLICIT:
4fc50066 150 case BRANCH_TRACK_OVERRIDE:
9ed36cfa
JS
151 break;
152 default:
27852b2c 153 return;
9ed36cfa
JS
154 }
155
e496c003 156 if (tracking.matches > 1)
27852b2c
PS
157 die(_("Not tracking: ambiguous information for ref %s"),
158 orig_ref);
e496c003 159
27852b2c
PS
160 if (install_branch_config(config_flags, new_ref, tracking.remote,
161 tracking.src ? tracking.src : orig_ref) < 0)
162 exit(-1);
e496c003 163
a9f2c136 164 free(tracking.src);
e496c003
DB
165}
166
6f9a3321
JH
167int read_branch_desc(struct strbuf *buf, const char *branch_name)
168{
540b0f49 169 char *v = NULL;
6f9a3321
JH
170 struct strbuf name = STRBUF_INIT;
171 strbuf_addf(&name, "branch.%s.description", branch_name);
540b0f49
TA
172 if (git_config_get_string(name.buf, &v)) {
173 strbuf_release(&name);
174 return -1;
175 }
176 strbuf_addstr(buf, v);
177 free(v);
6f9a3321
JH
178 strbuf_release(&name);
179 return 0;
180}
181
bc1c9c0e
JH
182/*
183 * Check if 'name' can be a valid name for a branch; die otherwise.
184 * Return 1 if the named branch already exists; return 0 otherwise.
185 * Fill ref with the full refname for the branch.
186 */
187int validate_branchname(const char *name, struct strbuf *ref)
55c4a673 188{
55c4a673 189 if (strbuf_check_branch_ref(ref, name))
bc554df8 190 die(_("'%s' is not a valid branch name."), name);
55c4a673 191
bc1c9c0e
JH
192 return ref_exists(ref->buf);
193}
55c4a673 194
bc1c9c0e
JH
195/*
196 * Check if a branch 'name' can be created as a new branch; die otherwise.
197 * 'force' can be used when it is OK for the named branch already exists.
198 * Return 1 if the named branch already exists; return 0 otherwise.
199 * Fill ref with the full refname for the branch.
200 */
201int validate_new_branchname(const char *name, struct strbuf *ref, int force)
202{
203 const char *head;
204
205 if (!validate_branchname(name, ref))
55c4a673 206 return 0;
55c4a673 207
8280c4c1
JH
208 if (!force)
209 die(_("A branch named '%s' already exists."),
210 ref->buf + strlen("refs/heads/"));
211
212 head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
213 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
214 die(_("Cannot force update the current branch."));
55c4a673
CI
215
216 return 1;
217}
218
41c21f22
JH
219static int check_tracking_branch(struct remote *remote, void *cb_data)
220{
221 char *tracking_branch = cb_data;
0ad4a5ff
BW
222 struct refspec_item query;
223 memset(&query, 0, sizeof(struct refspec_item));
41c21f22 224 query.dst = tracking_branch;
1d7358c5 225 return !remote_find_tracking(remote, &query);
41c21f22
JH
226}
227
228static int validate_remote_tracking_branch(char *ref)
229{
230 return !for_each_remote(check_tracking_branch, ref);
231}
232
e2b6aa5f 233static const char upstream_not_branch[] =
1a15d00b 234N_("Cannot setup tracking information; starting point '%s' is not a branch.");
a5e91c72 235static const char upstream_missing[] =
caa2036b
JK
236N_("the requested upstream branch '%s' does not exist");
237static const char upstream_advice[] =
238N_("\n"
239"If you are planning on basing your work on an upstream\n"
240"branch that already exists at the remote, you may need to\n"
241"run \"git fetch\" to retrieve it.\n"
242"\n"
243"If you are planning to push out a new local branch that\n"
244"will track its remote counterpart, you may want to use\n"
245"\"git push -u\" to set the upstream config as you push.");
e2b6aa5f 246
4bd488ea 247void create_branch(const char *name, const char *start_name,
e2bbd0cc 248 int force, int clobber_head_ok, int reflog,
f9a482e6 249 int quiet, enum branch_track track)
e496c003 250{
e496c003 251 struct commit *commit;
48713bfa 252 struct object_id oid;
3818b258 253 char *real_ref;
8415d5c7 254 struct strbuf ref = STRBUF_INIT;
e496c003 255 int forcing = 0;
4fc50066
IL
256 int dont_change_ref = 0;
257 int explicit_tracking = 0;
258
259 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
260 explicit_tracking = 1;
e496c003 261
0faff988 262 if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
bc1c9c0e
JH
263 ? validate_branchname(name, &ref)
264 : validate_new_branchname(name, &ref, force)) {
55c4a673 265 if (!force)
4fc50066 266 dont_change_ref = 1;
55c4a673
CI
267 else
268 forcing = 1;
e496c003
DB
269 }
270
271 real_ref = NULL;
48713bfa 272 if (get_oid(start_name, &oid)) {
caa2036b
JK
273 if (explicit_tracking) {
274 if (advice_set_upstream_failure) {
275 error(_(upstream_missing), start_name);
276 advise(_(upstream_advice));
277 exit(1);
278 }
1a15d00b 279 die(_(upstream_missing), start_name);
caa2036b 280 }
bc554df8 281 die(_("Not a valid object name: '%s'."), start_name);
a5e91c72 282 }
e496c003 283
cca5fa64 284 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref)) {
e496c003
DB
285 case 0:
286 /* Not branching from any existing branch */
4fc50066 287 if (explicit_tracking)
1a15d00b 288 die(_(upstream_not_branch), start_name);
e496c003
DB
289 break;
290 case 1:
21b5b1e8 291 /* Unique completion -- good, only if it is a real branch */
59556548 292 if (!starts_with(real_ref, "refs/heads/") &&
41c21f22 293 validate_remote_tracking_branch(real_ref)) {
21b5b1e8 294 if (explicit_tracking)
1a15d00b 295 die(_(upstream_not_branch), start_name);
21b5b1e8
JH
296 else
297 real_ref = NULL;
298 }
e496c003
DB
299 break;
300 default:
bc554df8 301 die(_("Ambiguous object name: '%s'."), start_name);
e496c003
DB
302 break;
303 }
304
bc83266a 305 if ((commit = lookup_commit_reference(&oid)) == NULL)
bc554df8 306 die(_("Not a valid branch point: '%s'."), start_name);
48713bfa 307 oidcpy(&oid, &commit->object.oid);
e496c003 308
d43f990f 309 if (reflog)
341fb286 310 log_all_ref_updates = LOG_REFS_NORMAL;
d43f990f
RS
311
312 if (!dont_change_ref) {
313 struct ref_transaction *transaction;
314 struct strbuf err = STRBUF_INIT;
cddac452 315 char *msg;
3818b258
JK
316
317 if (forcing)
cddac452 318 msg = xstrfmt("branch: Reset to %s", start_name);
3818b258 319 else
cddac452 320 msg = xstrfmt("branch: Created from %s", start_name);
d43f990f
RS
321
322 transaction = ref_transaction_begin(&err);
323 if (!transaction ||
1d147bdf 324 ref_transaction_update(transaction, ref.buf,
89f3bbdd 325 &oid, forcing ? NULL : &null_oid,
1d147bdf 326 0, msg, &err) ||
db7516ab 327 ref_transaction_commit(transaction, &err))
d43f990f
RS
328 die("%s", err.buf);
329 ref_transaction_free(transaction);
330 strbuf_release(&err);
cddac452 331 free(msg);
d43f990f
RS
332 }
333
e496c003 334 if (real_ref && track)
82a0672f 335 setup_tracking(ref.buf + 11, real_ref, track, quiet);
e496c003 336
8415d5c7 337 strbuf_release(&ref);
9ed36cfa 338 free(real_ref);
e496c003 339}
c369e7b8
DB
340
341void remove_branch_state(void)
342{
f932729c
JK
343 unlink(git_path_cherry_pick_head());
344 unlink(git_path_revert_head());
345 unlink(git_path_merge_head());
346 unlink(git_path_merge_rr());
347 unlink(git_path_merge_msg());
348 unlink(git_path_merge_mode());
349 unlink(git_path_squash_msg());
c369e7b8 350}
ed89f84b 351
8d9fdd70 352void die_if_checked_out(const char *branch, int ignore_current_worktree)
41af6565 353{
d3b9ac07 354 const struct worktree *wt;
41af6565 355
d3b9ac07 356 wt = find_shared_symref("HEAD", branch);
8d9fdd70 357 if (!wt || (ignore_current_worktree && wt->is_current))
d3b9ac07
NTND
358 return;
359 skip_prefix(branch, "refs/heads/", &branch);
360 die(_("'%s' is already checked out at '%s'"),
361 branch, wt->path);
ed89f84b 362}
70999e9c 363
39ee4c6c
KM
364int replace_each_worktree_head_symref(const char *oldref, const char *newref,
365 const char *logmsg)
70999e9c
KY
366{
367 int ret = 0;
4fff1ef7 368 struct worktree **worktrees = get_worktrees(0);
70999e9c
KY
369 int i;
370
371 for (i = 0; worktrees[i]; i++) {
d026a256
NTND
372 struct ref_store *refs;
373
70999e9c
KY
374 if (worktrees[i]->is_detached)
375 continue;
31824d18
NTND
376 if (!worktrees[i]->head_ref)
377 continue;
378 if (strcmp(oldref, worktrees[i]->head_ref))
70999e9c
KY
379 continue;
380
d026a256
NTND
381 refs = get_worktree_ref_store(worktrees[i]);
382 if (refs_create_symref(refs, "HEAD", newref, logmsg))
383 ret = error(_("HEAD of working tree %s is not updated"),
384 worktrees[i]->path);
70999e9c
KY
385 }
386
387 free_worktrees(worktrees);
388 return ret;
389}