1#include "cache.h"
2#include "branch.h"
3#include "refs.h"
4#include "remote.h"
5#include "commit.h"
67
struct tracking {
8struct refspec spec;
9char *src;
10const char *remote;
11int matches;
12};
1314
static int find_tracked_branch(struct remote *remote, void *priv)
15{
16struct tracking *tracking = priv;
1718
if (!remote_find_tracking(remote, &tracking->spec)) {
19if (++tracking->matches == 1) {
20tracking->src = tracking->spec.src;
21tracking->remote = remote->name;
22} else {
23free(tracking->spec.src);
24if (tracking->src) {
25free(tracking->src);
26tracking->src = NULL;
27}
28}
29tracking->spec.src = NULL;
30}
3132
return 0;
33}
3435
static int should_setup_rebase(const char *origin)
36{
37switch (autorebase) {
38case AUTOREBASE_NEVER:
39return 0;
40case AUTOREBASE_LOCAL:
41return origin == NULL;
42case AUTOREBASE_REMOTE:
43return origin != NULL;
44case AUTOREBASE_ALWAYS:
45return 1;
46}
47return 0;
48}
4950
void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
51{
52const char *shortname = remote + 11;
53int remote_is_branch = !prefixcmp(remote, "refs/heads/");
54struct strbuf key = STRBUF_INIT;
55int rebasing = should_setup_rebase(origin);
5657
if (remote_is_branch
58&& !strcmp(local, shortname)
59&& !origin) {
60warning("Not setting branch %s as its own upstream.",
61local);
62return;
63}
6465
strbuf_addf(&key, "branch.%s.remote", local);
66git_config_set(key.buf, origin ? origin : ".");
6768
strbuf_reset(&key);
69strbuf_addf(&key, "branch.%s.merge", local);
70git_config_set(key.buf, remote);
7172
if (rebasing) {
73strbuf_reset(&key);
74strbuf_addf(&key, "branch.%s.rebase", local);
75git_config_set(key.buf, "true");
76}
77strbuf_release(&key);
7879
if (flag & BRANCH_CONFIG_VERBOSE) {
80if (remote_is_branch && origin)
81printf(rebasing ?
82"Branch %s set up to track remote branch %s from %s by rebasing.\n" :
83"Branch %s set up to track remote branch %s from %s.\n",
84local, shortname, origin);
85else if (remote_is_branch && !origin)
86printf(rebasing ?
87"Branch %s set up to track local branch %s by rebasing.\n" :
88"Branch %s set up to track local branch %s.\n",
89local, shortname);
90else if (!remote_is_branch && origin)
91printf(rebasing ?
92"Branch %s set up to track remote ref %s by rebasing.\n" :
93"Branch %s set up to track remote ref %s.\n",
94local, remote);
95else if (!remote_is_branch && !origin)
96printf(rebasing ?
97"Branch %s set up to track local ref %s by rebasing.\n" :
98"Branch %s set up to track local ref %s.\n",
99local, remote);
100else
101die("BUG: impossible combination of %d and %p",
102remote_is_branch, origin);
103}
104}
105106
/*
107* This is called when new_ref is branched off of orig_ref, and tries
108* to infer the settings for branch.<new_ref>.{remote,merge} from the
109* config.
110*/
111static int setup_tracking(const char *new_ref, const char *orig_ref,
112enum branch_track track, int quiet)
113{
114struct tracking tracking;
115int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
116117
if (strlen(new_ref) > 1024 - 7 - 7 - 1)
118return error("Tracking not set up: name too long: %s",
119new_ref);
120121
memset(&tracking, 0, sizeof(tracking));
122tracking.spec.dst = (char *)orig_ref;
123if (for_each_remote(find_tracked_branch, &tracking))
124return 1;
125126
if (!tracking.matches)
127switch (track) {
128case BRANCH_TRACK_ALWAYS:
129case BRANCH_TRACK_EXPLICIT:
130case BRANCH_TRACK_OVERRIDE:
131break;
132default:
133return 1;
134}
135136
if (tracking.matches > 1)
137return error("Not tracking: ambiguous information for ref %s",
138orig_ref);
139140
install_branch_config(config_flags, new_ref, tracking.remote,
141tracking.src ? tracking.src : orig_ref);
142143
free(tracking.src);
144return 0;
145}
146147
struct branch_desc_cb {
148const char *config_name;
149const char *value;
150};
151152
static int read_branch_desc_cb(const char *var, const char *value, void *cb)
153{
154struct branch_desc_cb *desc = cb;
155if (strcmp(desc->config_name, var))
156return 0;
157free((char *)desc->value);
158return git_config_string(&desc->value, var, value);
159}
160161
int read_branch_desc(struct strbuf *buf, const char *branch_name)
162{
163struct branch_desc_cb cb;
164struct strbuf name = STRBUF_INIT;
165strbuf_addf(&name, "branch.%s.description", branch_name);
166cb.config_name = name.buf;
167cb.value = NULL;
168if (git_config(read_branch_desc_cb, &cb) < 0) {
169strbuf_release(&name);
170return -1;
171}
172if (cb.value)
173strbuf_addstr(buf, cb.value);
174strbuf_release(&name);
175return 0;
176}
177178
int validate_new_branchname(const char *name, struct strbuf *ref,
179int force, int attr_only)
180{
181if (strbuf_check_branch_ref(ref, name))
182die("'%s' is not a valid branch name.", name);
183184
if (!ref_exists(ref->buf))
185return 0;
186else if (!force && !attr_only)
187die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
188189
if (!attr_only) {
190const char *head;
191unsigned char sha1[20];
192193
head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
194if (!is_bare_repository() && head && !strcmp(head, ref->buf))
195die("Cannot force update the current branch.");
196}
197return 1;
198}
199200
static const char upstream_not_branch[] =
201N_("Cannot setup tracking information; starting point '%s' is not a branch.");
202static const char upstream_missing[] =
203N_("Cannot setup tracking information; starting point '%s' does not exist");
204205
void create_branch(const char *head,
206const char *name, const char *start_name,
207int force, int reflog, int clobber_head,
208int quiet, enum branch_track track)
209{
210struct ref_lock *lock = NULL;
211struct commit *commit;
212unsigned char sha1[20];
213char *real_ref, msg[PATH_MAX + 20];
214struct strbuf ref = STRBUF_INIT;
215int forcing = 0;
216int dont_change_ref = 0;
217int explicit_tracking = 0;
218219
if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
220explicit_tracking = 1;
221222
if (validate_new_branchname(name, &ref, force,
223track == BRANCH_TRACK_OVERRIDE ||
224clobber_head)) {
225if (!force)
226dont_change_ref = 1;
227else
228forcing = 1;
229}
230231
real_ref = NULL;
232if (get_sha1(start_name, sha1)) {
233if (explicit_tracking)
234die(_(upstream_missing), start_name);
235die("Not a valid object name: '%s'.", start_name);
236}
237238
switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
239case 0:
240/* Not branching from any existing branch */
241if (explicit_tracking)
242die(_(upstream_not_branch), start_name);
243break;
244case 1:
245/* Unique completion -- good, only if it is a real branch */
246if (prefixcmp(real_ref, "refs/heads/") &&
247prefixcmp(real_ref, "refs/remotes/")) {
248if (explicit_tracking)
249die(_(upstream_not_branch), start_name);
250else
251real_ref = NULL;
252}
253break;
254default:
255die("Ambiguous object name: '%s'.", start_name);
256break;
257}
258259
if ((commit = lookup_commit_reference(sha1)) == NULL)
260die("Not a valid branch point: '%s'.", start_name);
261hashcpy(sha1, commit->object.sha1);
262263
if (!dont_change_ref) {
264lock = lock_any_ref_for_update(ref.buf, NULL, 0);
265if (!lock)
266die_errno("Failed to lock ref for update");
267}
268269
if (reflog)
270log_all_ref_updates = 1;
271272
if (forcing)
273snprintf(msg, sizeof msg, "branch: Reset to %s",
274start_name);
275else if (!dont_change_ref)
276snprintf(msg, sizeof msg, "branch: Created from %s",
277start_name);
278279
if (real_ref && track)
280setup_tracking(ref.buf+11, real_ref, track, quiet);
281282
if (!dont_change_ref)
283if (write_ref_sha1(lock, sha1, msg) < 0)
284die_errno("Failed to write ref");
285286
strbuf_release(&ref);
287free(real_ref);
288}
289290
void remove_branch_state(void)
291{
292unlink(git_path("CHERRY_PICK_HEAD"));
293unlink(git_path("REVERT_HEAD"));
294unlink(git_path("MERGE_HEAD"));
295unlink(git_path("MERGE_RR"));
296unlink(git_path("MERGE_MSG"));
297unlink(git_path("MERGE_MODE"));
298unlink(git_path("SQUASH_MSG"));
299}