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