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