Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 292 lines (253 sloc) 7.462 kb
e496c00 Move create_branch into a library file
Daniel Barkalow authored
1 #include "cache.h"
2 #include "branch.h"
3 #include "refs.h"
4 #include "remote.h"
5 #include "commit.h"
6
7 struct tracking {
8 struct refspec spec;
9 char *src;
10 const char *remote;
11 int matches;
12 };
13
14 static 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
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
35 static int should_setup_rebase(const char *origin)
c998ae9 @dustin Allow tracking branches to set up rebase by default.
dustin authored
36 {
37 switch (autorebase) {
38 case AUTOREBASE_NEVER:
39 return 0;
40 case AUTOREBASE_LOCAL:
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
41 return origin == NULL;
c998ae9 @dustin Allow tracking branches to set up rebase by default.
dustin authored
42 case AUTOREBASE_REMOTE:
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
43 return origin != NULL;
c998ae9 @dustin Allow tracking branches to set up rebase by default.
dustin authored
44 case AUTOREBASE_ALWAYS:
45 return 1;
46 }
47 return 0;
48 }
49
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
50 void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
51 {
85e2233 @moy branch: warn and refuse to set a branch as a tracking branch of itself.
moy authored
52 const char *shortname = remote + 11;
53 int remote_is_branch = !prefixcmp(remote, "refs/heads/");
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
54 struct strbuf key = STRBUF_INIT;
55 int rebasing = should_setup_rebase(origin);
56
85e2233 @moy branch: warn and refuse to set a branch as a tracking branch of itself.
moy authored
57 if (remote_is_branch
58 && !strcmp(local, shortname)
59 && !origin) {
60 warning("Not setting branch %s as its own upstream.",
61 local);
62 return;
63 }
64
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
65 strbuf_addf(&key, "branch.%s.remote", local);
66 git_config_set(key.buf, origin ? origin : ".");
67
68 strbuf_reset(&key);
69 strbuf_addf(&key, "branch.%s.merge", local);
70 git_config_set(key.buf, remote);
71
72 if (rebasing) {
73 strbuf_reset(&key);
74 strbuf_addf(&key, "branch.%s.rebase", local);
75 git_config_set(key.buf, "true");
76 }
d53a350 @pclouds Remove i18n legos in notifying new branch tracking setup
pclouds authored
77 strbuf_release(&key);
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
78
72f6008 @gitster Improve "git branch --tracking" output
authored
79 if (flag & BRANCH_CONFIG_VERBOSE) {
d53a350 @pclouds Remove i18n legos in notifying new branch tracking setup
pclouds authored
80 if (remote_is_branch && origin)
81 printf(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",
84 local, shortname, origin);
85 else if (remote_is_branch && !origin)
86 printf(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",
89 local, shortname);
90 else if (!remote_is_branch && origin)
91 printf(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",
94 local, remote);
95 else if (!remote_is_branch && !origin)
96 printf(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",
99 local, remote);
72f6008 @gitster Improve "git branch --tracking" output
authored
100 else
d53a350 @pclouds Remove i18n legos in notifying new branch tracking setup
pclouds authored
101 die("BUG: impossible combination of %d and %p",
102 remote_is_branch, origin);
72f6008 @gitster Improve "git branch --tracking" output
authored
103 }
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
104 }
105
e496c00 Move create_branch into a library file
Daniel Barkalow authored
106 /*
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 */
9ed36cf @jaysoffian branch: optionally setup branch.*.merge from upstream local branches
jaysoffian authored
111 static int setup_tracking(const char *new_ref, const char *orig_ref,
f9a482e @peff checkout: suppress tracking message with "-q"
peff authored
112 enum branch_track track, int quiet)
e496c00 Move create_branch into a library file
Daniel Barkalow authored
113 {
114 struct tracking tracking;
f9a482e @peff checkout: suppress tracking message with "-q"
peff authored
115 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
e496c00 Move create_branch into a library file
Daniel Barkalow authored
116
117 if (strlen(new_ref) > 1024 - 7 - 7 - 1)
118 return error("Tracking not set up: name too long: %s",
119 new_ref);
120
121 memset(&tracking, 0, sizeof(tracking));
122 tracking.spec.dst = (char *)orig_ref;
9ed36cf @jaysoffian branch: optionally setup branch.*.merge from upstream local branches
jaysoffian authored
123 if (for_each_remote(find_tracked_branch, &tracking))
e496c00 Move create_branch into a library file
Daniel Barkalow authored
124 return 1;
125
9ed36cf @jaysoffian branch: optionally setup branch.*.merge from upstream local branches
jaysoffian authored
126 if (!tracking.matches)
127 switch (track) {
128 case BRANCH_TRACK_ALWAYS:
129 case BRANCH_TRACK_EXPLICIT:
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
130 case BRANCH_TRACK_OVERRIDE:
9ed36cf @jaysoffian branch: optionally setup branch.*.merge from upstream local branches
jaysoffian authored
131 break;
132 default:
133 return 1;
134 }
135
e496c00 Move create_branch into a library file
Daniel Barkalow authored
136 if (tracking.matches > 1)
137 return error("Not tracking: ambiguous information for ref %s",
138 orig_ref);
139
f9a482e @peff checkout: suppress tracking message with "-q"
peff authored
140 install_branch_config(config_flags, new_ref, tracking.remote,
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
141 tracking.src ? tracking.src : orig_ref);
e496c00 Move create_branch into a library file
Daniel Barkalow authored
142
a9f2c13 @gitster Make git-clone respect branch.autosetuprebase
authored
143 free(tracking.src);
e496c00 Move create_branch into a library file
Daniel Barkalow authored
144 return 0;
145 }
146
6f9a332 @gitster branch: add read_branch_desc() helper function
authored
147 struct branch_desc_cb {
148 const char *config_name;
149 const char *value;
150 };
151
152 static int read_branch_desc_cb(const char *var, const char *value, void *cb)
153 {
154 struct branch_desc_cb *desc = cb;
155 if (strcmp(desc->config_name, var))
156 return 0;
157 free((char *)desc->value);
158 return git_config_string(&desc->value, var, value);
159 }
160
161 int read_branch_desc(struct strbuf *buf, const char *branch_name)
162 {
163 struct branch_desc_cb cb;
164 struct strbuf name = STRBUF_INIT;
165 strbuf_addf(&name, "branch.%s.description", branch_name);
166 cb.config_name = name.buf;
167 cb.value = NULL;
168 if (git_config(read_branch_desc_cb, &cb) < 0) {
169 strbuf_release(&name);
170 return -1;
171 }
172 if (cb.value)
173 strbuf_addstr(buf, cb.value);
174 strbuf_release(&name);
175 return 0;
176 }
177
fa79937 @gitster branch --set-upstream: regression fix
authored
178 int validate_new_branchname(const char *name, struct strbuf *ref,
179 int force, int attr_only)
55c4a67 @ConradIrwin Prevent force-updating of the current branch
ConradIrwin authored
180 {
181 if (strbuf_check_branch_ref(ref, name))
182 die("'%s' is not a valid branch name.", name);
183
184 if (!ref_exists(ref->buf))
185 return 0;
fa79937 @gitster branch --set-upstream: regression fix
authored
186 else if (!force && !attr_only)
587a9ee @ConradIrwin Show interpreted branch name in error messages
ConradIrwin authored
187 die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
55c4a67 @ConradIrwin Prevent force-updating of the current branch
ConradIrwin authored
188
fa79937 @gitster branch --set-upstream: regression fix
authored
189 if (!attr_only) {
190 const char *head;
191 unsigned char sha1[20];
55c4a67 @ConradIrwin Prevent force-updating of the current branch
ConradIrwin authored
192
8cad474 @pclouds Rename resolve_ref() to resolve_ref_unsafe()
pclouds authored
193 head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
fa79937 @gitster branch --set-upstream: regression fix
authored
194 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
195 die("Cannot force update the current branch.");
196 }
55c4a67 @ConradIrwin Prevent force-updating of the current branch
ConradIrwin authored
197 return 1;
198 }
199
e496c00 Move create_branch into a library file
Daniel Barkalow authored
200 void create_branch(const char *head,
201 const char *name, const char *start_name,
39bd6f7 @jrn Allow checkout -B <current-branch> to update the current branch
jrn authored
202 int force, int reflog, int clobber_head,
f9a482e @peff checkout: suppress tracking message with "-q"
peff authored
203 int quiet, enum branch_track track)
e496c00 Move create_branch into a library file
Daniel Barkalow authored
204 {
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
205 struct ref_lock *lock = NULL;
e496c00 Move create_branch into a library file
Daniel Barkalow authored
206 struct commit *commit;
207 unsigned char sha1[20];
8415d5c @gitster Teach the "@{-1} syntax to "git branch"
authored
208 char *real_ref, msg[PATH_MAX + 20];
209 struct strbuf ref = STRBUF_INIT;
e496c00 Move create_branch into a library file
Daniel Barkalow authored
210 int forcing = 0;
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
211 int dont_change_ref = 0;
212 int explicit_tracking = 0;
213
214 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
215 explicit_tracking = 1;
e496c00 Move create_branch into a library file
Daniel Barkalow authored
216
fa79937 @gitster branch --set-upstream: regression fix
authored
217 if (validate_new_branchname(name, &ref, force,
39bd6f7 @jrn Allow checkout -B <current-branch> to update the current branch
jrn authored
218 track == BRANCH_TRACK_OVERRIDE ||
219 clobber_head)) {
55c4a67 @ConradIrwin Prevent force-updating of the current branch
ConradIrwin authored
220 if (!force)
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
221 dont_change_ref = 1;
55c4a67 @ConradIrwin Prevent force-updating of the current branch
ConradIrwin authored
222 else
223 forcing = 1;
e496c00 Move create_branch into a library file
Daniel Barkalow authored
224 }
225
226 real_ref = NULL;
227 if (get_sha1(start_name, sha1))
228 die("Not a valid object name: '%s'.", start_name);
229
230 switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
231 case 0:
232 /* Not branching from any existing branch */
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
233 if (explicit_tracking)
9ed36cf @jaysoffian branch: optionally setup branch.*.merge from upstream local branches
jaysoffian authored
234 die("Cannot setup tracking information; starting point is not a branch.");
e496c00 Move create_branch into a library file
Daniel Barkalow authored
235 break;
236 case 1:
21b5b1e @jherland branch/checkout --track: Ensure that upstream branch is indeed a branch
jherland authored
237 /* Unique completion -- good, only if it is a real branch */
238 if (prefixcmp(real_ref, "refs/heads/") &&
239 prefixcmp(real_ref, "refs/remotes/")) {
240 if (explicit_tracking)
241 die("Cannot setup tracking information; starting point is not a branch.");
242 else
243 real_ref = NULL;
244 }
e496c00 Move create_branch into a library file
Daniel Barkalow authored
245 break;
246 default:
247 die("Ambiguous object name: '%s'.", start_name);
248 break;
249 }
250
251 if ((commit = lookup_commit_reference(sha1)) == NULL)
252 die("Not a valid branch point: '%s'.", start_name);
253 hashcpy(sha1, commit->object.sha1);
254
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
255 if (!dont_change_ref) {
256 lock = lock_any_ref_for_update(ref.buf, NULL, 0);
257 if (!lock)
258 die_errno("Failed to lock ref for update");
259 }
e496c00 Move create_branch into a library file
Daniel Barkalow authored
260
261 if (reflog)
262 log_all_ref_updates = 1;
263
264 if (forcing)
da288e2 @gitster Merge branch 'rc/maint-reflog-msg-for-forced-branch'
authored
265 snprintf(msg, sizeof msg, "branch: Reset to %s",
e496c00 Move create_branch into a library file
Daniel Barkalow authored
266 start_name);
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
267 else if (!dont_change_ref)
e496c00 Move create_branch into a library file
Daniel Barkalow authored
268 snprintf(msg, sizeof msg, "branch: Created from %s",
269 start_name);
270
271 if (real_ref && track)
f9a482e @peff checkout: suppress tracking message with "-q"
peff authored
272 setup_tracking(ref.buf+11, real_ref, track, quiet);
e496c00 Move create_branch into a library file
Daniel Barkalow authored
273
4fc5006 Add branch --set-upstream
Ilari Liusvaara authored
274 if (!dont_change_ref)
275 if (write_ref_sha1(lock, sha1, msg) < 0)
276 die_errno("Failed to write ref");
e496c00 Move create_branch into a library file
Daniel Barkalow authored
277
8415d5c @gitster Teach the "@{-1} syntax to "git branch"
authored
278 strbuf_release(&ref);
9ed36cf @jaysoffian branch: optionally setup branch.*.merge from upstream local branches
jaysoffian authored
279 free(real_ref);
e496c00 Move create_branch into a library file
Daniel Barkalow authored
280 }
c369e7b Move code to clean up after a branch change to branch.c
Daniel Barkalow authored
281
282 void remove_branch_state(void)
283 {
d7e5c0c @jaysoffian Introduce CHERRY_PICK_HEAD
jaysoffian authored
284 unlink(git_path("CHERRY_PICK_HEAD"));
82433cd @jrn revert: write REVERT_HEAD pseudoref during conflicted revert
jrn authored
285 unlink(git_path("REVERT_HEAD"));
c369e7b Move code to clean up after a branch change to branch.c
Daniel Barkalow authored
286 unlink(git_path("MERGE_HEAD"));
0af0ac7 @dscho Move MERGE_RR from .git/rr-cache/ into .git/
dscho authored
287 unlink(git_path("MERGE_RR"));
c369e7b Move code to clean up after a branch change to branch.c
Daniel Barkalow authored
288 unlink(git_path("MERGE_MSG"));
cf10f9f builtin-commit: use reduce_heads() only when appropriate
Miklos Vajna authored
289 unlink(git_path("MERGE_MODE"));
c369e7b Move code to clean up after a branch change to branch.c
Daniel Barkalow authored
290 unlink(git_path("SQUASH_MSG"));
291 }
Something went wrong with that request. Please try again.