1#include "cache.h"
2#include "branch.h"
3#include "refs.h"
4#include "remote.h"
5#include "commit.h"
6
7struct tracking {
8 struct refspec spec;
9 char *src;
10 const char *remote;
11 int matches;
12};
13
14static int find_tracked_branch(struct remote *remote, void *priv)
15{
16 struct tracking *tracking = priv;
17
18 if (!remote_find_tracking(remote, &tracking->spec)) {
19 if (++tracking->matches == 1) {
20 tracking->src = tracking->spec.src;
21 tracking->remote = remote->name;
22 } else {
23 free(tracking->spec.src);
24 if (tracking->src) {
25 free(tracking->src);
26 tracking->src = NULL;
27 }
28 }
29 tracking->spec.src = NULL;
30 }
31
32 return 0;
33}
34
35static int should_setup_rebase(const char *origin)
36{
37 switch (autorebase) {
38 case AUTOREBASE_NEVER:
39 return 0;
40 case AUTOREBASE_LOCAL:
41 return origin == NULL;
42 case AUTOREBASE_REMOTE:
43 return origin != NULL;
44 case AUTOREBASE_ALWAYS:
45 return 1;
46 }
47 return 0;
48}
49
50void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
51{
52 struct strbuf key = STRBUF_INIT;
53 int rebasing = should_setup_rebase(origin);
54
55 strbuf_addf(&key, "branch.%s.remote", local);
56 git_config_set(key.buf, origin ? origin : ".");
57
58 strbuf_reset(&key);
59 strbuf_addf(&key, "branch.%s.merge", local);
60 git_config_set(key.buf, remote);
61
62 if (rebasing) {
63 strbuf_reset(&key);
64 strbuf_addf(&key, "branch.%s.rebase", local);
65 git_config_set(key.buf, "true");
66 }
67
68 if (flag & BRANCH_CONFIG_VERBOSE) {
69 strbuf_reset(&key);
70
71 strbuf_addstr(&key, origin ? "remote" : "local");
72
73 /* Are we tracking a proper "branch"? */
74 if (!prefixcmp(remote, "refs/heads/")) {
75 strbuf_addf(&key, " branch %s", remote + 11);
76 if (origin)
77 strbuf_addf(&key, " from %s", origin);
78 }
79 else
80 strbuf_addf(&key, " ref %s", remote);
81 printf("Branch %s set up to track %s%s.\n",
82 local, key.buf,
83 rebasing ? " by rebasing" : "");
84 }
85 strbuf_release(&key);
86}
87
88/*
89 * This is called when new_ref is branched off of orig_ref, and tries
90 * to infer the settings for branch.<new_ref>.{remote,merge} from the
91 * config.
92 */
93static int setup_tracking(const char *new_ref, const char *orig_ref,
94 enum branch_track track)
95{
96 struct tracking tracking;
97
98 if (strlen(new_ref) > 1024 - 7 - 7 - 1)
99 return error("Tracking not set up: name too long: %s",
100 new_ref);
101
102 memset(&tracking, 0, sizeof(tracking));
103 tracking.spec.dst = (char *)orig_ref;
104 if (for_each_remote(find_tracked_branch, &tracking))
105 return 1;
106
107 if (!tracking.matches)
108 switch (track) {
109 case BRANCH_TRACK_ALWAYS:
110 case BRANCH_TRACK_EXPLICIT:
111 break;
112 default:
113 return 1;
114 }
115
116 if (tracking.matches > 1)
117 return error("Not tracking: ambiguous information for ref %s",
118 orig_ref);
119
120 install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
121 tracking.src ? tracking.src : orig_ref);
122
123 free(tracking.src);
124 return 0;
125}
126
127void create_branch(const char *head,
128 const char *name, const char *start_name,
129 int force, int reflog, enum branch_track track)
130{
131 struct ref_lock *lock;
132 struct commit *commit;
133 unsigned char sha1[20];
134 char *real_ref, msg[PATH_MAX + 20];
135 struct strbuf ref = STRBUF_INIT;
136 int forcing = 0;
137 int len;
138
139 len = strlen(name);
140 if (interpret_nth_last_branch(name, &ref) != len) {
141 strbuf_reset(&ref);
142 strbuf_add(&ref, name, len);
143 }
144 strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
145
146 if (check_ref_format(ref.buf))
147 die("'%s' is not a valid branch name.", name);
148
149 if (resolve_ref(ref.buf, sha1, 1, NULL)) {
150 if (!force)
151 die("A branch named '%s' already exists.", name);
152 else if (!is_bare_repository() && !strcmp(head, name))
153 die("Cannot force update the current branch.");
154 forcing = 1;
155 }
156
157 real_ref = NULL;
158 if (get_sha1(start_name, sha1))
159 die("Not a valid object name: '%s'.", start_name);
160
161 switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
162 case 0:
163 /* Not branching from any existing branch */
164 if (track == BRANCH_TRACK_EXPLICIT)
165 die("Cannot setup tracking information; starting point is not a branch.");
166 break;
167 case 1:
168 /* Unique completion -- good, only if it is a real ref */
169 if (track == BRANCH_TRACK_EXPLICIT && !strcmp(real_ref, "HEAD"))
170 die("Cannot setup tracking information; starting point is not a branch.");
171 break;
172 default:
173 die("Ambiguous object name: '%s'.", start_name);
174 break;
175 }
176
177 if ((commit = lookup_commit_reference(sha1)) == NULL)
178 die("Not a valid branch point: '%s'.", start_name);
179 hashcpy(sha1, commit->object.sha1);
180
181 lock = lock_any_ref_for_update(ref.buf, NULL, 0);
182 if (!lock)
183 die("Failed to lock ref for update: %s.", strerror(errno));
184
185 if (reflog)
186 log_all_ref_updates = 1;
187
188 if (forcing)
189 snprintf(msg, sizeof msg, "branch: Reset from %s",
190 start_name);
191 else
192 snprintf(msg, sizeof msg, "branch: Created from %s",
193 start_name);
194
195 if (real_ref && track)
196 setup_tracking(name, real_ref, track);
197
198 if (write_ref_sha1(lock, sha1, msg) < 0)
199 die("Failed to write ref: %s.", strerror(errno));
200
201 strbuf_release(&ref);
202 free(real_ref);
203}
204
205void remove_branch_state(void)
206{
207 unlink(git_path("MERGE_HEAD"));
208 unlink(git_path("MERGE_RR"));
209 unlink(git_path("MERGE_MSG"));
210 unlink(git_path("MERGE_MODE"));
211 unlink(git_path("SQUASH_MSG"));
212}