branch: allow a no-op "branch -M <current-branch> HEAD"
[git/git.git] / branch.c
CommitLineData
e496c003
DB
1#include "cache.h"
2#include "branch.h"
3#include "refs.h"
4#include "remote.h"
5#include "commit.h"
95eb88d8 6#include "sequencer.h"
e496c003
DB
7
8struct tracking {
9 struct refspec spec;
10 char *src;
11 const char *remote;
12 int matches;
13};
14
15static int find_tracked_branch(struct remote *remote, void *priv)
16{
17 struct tracking *tracking = priv;
18
19 if (!remote_find_tracking(remote, &tracking->spec)) {
20 if (++tracking->matches == 1) {
21 tracking->src = tracking->spec.src;
22 tracking->remote = remote->name;
23 } else {
24 free(tracking->spec.src);
25 if (tracking->src) {
26 free(tracking->src);
27 tracking->src = NULL;
28 }
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
a9f2c136
JH
51void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
52{
85e2233f
MM
53 const char *shortname = remote + 11;
54 int remote_is_branch = !prefixcmp(remote, "refs/heads/");
a9f2c136
JH
55 struct strbuf key = STRBUF_INIT;
56 int rebasing = should_setup_rebase(origin);
57
85e2233f
MM
58 if (remote_is_branch
59 && !strcmp(local, shortname)
60 && !origin) {
61 warning("Not setting branch %s as its own upstream.",
62 local);
63 return;
64 }
65
a9f2c136
JH
66 strbuf_addf(&key, "branch.%s.remote", local);
67 git_config_set(key.buf, origin ? origin : ".");
68
69 strbuf_reset(&key);
70 strbuf_addf(&key, "branch.%s.merge", local);
71 git_config_set(key.buf, remote);
72
73 if (rebasing) {
74 strbuf_reset(&key);
75 strbuf_addf(&key, "branch.%s.rebase", local);
76 git_config_set(key.buf, "true");
77 }
78
72f60083
JH
79 if (flag & BRANCH_CONFIG_VERBOSE) {
80 strbuf_reset(&key);
81
82 strbuf_addstr(&key, origin ? "remote" : "local");
83
84 /* Are we tracking a proper "branch"? */
85e2233f
MM
85 if (remote_is_branch) {
86 strbuf_addf(&key, " branch %s", shortname);
72f60083
JH
87 if (origin)
88 strbuf_addf(&key, " from %s", origin);
89 }
90 else
91 strbuf_addf(&key, " ref %s", remote);
92 printf("Branch %s set up to track %s%s.\n",
93 local, key.buf,
94 rebasing ? " by rebasing" : "");
95 }
a9f2c136
JH
96 strbuf_release(&key);
97}
98
e496c003
DB
99/*
100 * This is called when new_ref is branched off of orig_ref, and tries
101 * to infer the settings for branch.<new_ref>.{remote,merge} from the
102 * config.
103 */
9ed36cfa
JS
104static int setup_tracking(const char *new_ref, const char *orig_ref,
105 enum branch_track track)
e496c003 106{
e496c003
DB
107 struct tracking tracking;
108
109 if (strlen(new_ref) > 1024 - 7 - 7 - 1)
110 return error("Tracking not set up: name too long: %s",
111 new_ref);
112
113 memset(&tracking, 0, sizeof(tracking));
114 tracking.spec.dst = (char *)orig_ref;
9ed36cfa 115 if (for_each_remote(find_tracked_branch, &tracking))
e496c003
DB
116 return 1;
117
9ed36cfa
JS
118 if (!tracking.matches)
119 switch (track) {
120 case BRANCH_TRACK_ALWAYS:
121 case BRANCH_TRACK_EXPLICIT:
4fc50066 122 case BRANCH_TRACK_OVERRIDE:
9ed36cfa
JS
123 break;
124 default:
125 return 1;
126 }
127
e496c003
DB
128 if (tracking.matches > 1)
129 return error("Not tracking: ambiguous information for ref %s",
130 orig_ref);
131
a9f2c136
JH
132 install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
133 tracking.src ? tracking.src : orig_ref);
e496c003 134
a9f2c136 135 free(tracking.src);
e496c003
DB
136 return 0;
137}
138
fa799376
JH
139int validate_new_branchname(const char *name, struct strbuf *ref,
140 int force, int attr_only)
55c4a673 141{
55c4a673
CI
142 if (strbuf_check_branch_ref(ref, name))
143 die("'%s' is not a valid branch name.", name);
144
145 if (!ref_exists(ref->buf))
146 return 0;
fa799376 147 else if (!force && !attr_only)
587a9ee7 148 die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
55c4a673 149
fa799376
JH
150 if (!attr_only) {
151 const char *head;
152 unsigned char sha1[20];
55c4a673 153
fa799376
JH
154 head = resolve_ref("HEAD", sha1, 0, NULL);
155 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
156 die("Cannot force update the current branch.");
157 }
55c4a673
CI
158 return 1;
159}
160
e496c003
DB
161void create_branch(const char *head,
162 const char *name, const char *start_name,
9ed36cfa 163 int force, int reflog, enum branch_track track)
e496c003 164{
4fc50066 165 struct ref_lock *lock = NULL;
e496c003
DB
166 struct commit *commit;
167 unsigned char sha1[20];
8415d5c7
JH
168 char *real_ref, msg[PATH_MAX + 20];
169 struct strbuf ref = STRBUF_INIT;
e496c003 170 int forcing = 0;
4fc50066
IL
171 int dont_change_ref = 0;
172 int explicit_tracking = 0;
173
174 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
175 explicit_tracking = 1;
e496c003 176
fa799376
JH
177 if (validate_new_branchname(name, &ref, force,
178 track == BRANCH_TRACK_OVERRIDE)) {
55c4a673 179 if (!force)
4fc50066 180 dont_change_ref = 1;
55c4a673
CI
181 else
182 forcing = 1;
e496c003
DB
183 }
184
185 real_ref = NULL;
186 if (get_sha1(start_name, sha1))
187 die("Not a valid object name: '%s'.", start_name);
188
189 switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
190 case 0:
191 /* Not branching from any existing branch */
4fc50066 192 if (explicit_tracking)
9ed36cfa 193 die("Cannot setup tracking information; starting point is not a branch.");
e496c003
DB
194 break;
195 case 1:
21b5b1e8
JH
196 /* Unique completion -- good, only if it is a real branch */
197 if (prefixcmp(real_ref, "refs/heads/") &&
198 prefixcmp(real_ref, "refs/remotes/")) {
199 if (explicit_tracking)
200 die("Cannot setup tracking information; starting point is not a branch.");
201 else
202 real_ref = NULL;
203 }
e496c003
DB
204 break;
205 default:
206 die("Ambiguous object name: '%s'.", start_name);
207 break;
208 }
209
210 if ((commit = lookup_commit_reference(sha1)) == NULL)
211 die("Not a valid branch point: '%s'.", start_name);
212 hashcpy(sha1, commit->object.sha1);
213
4fc50066
IL
214 if (!dont_change_ref) {
215 lock = lock_any_ref_for_update(ref.buf, NULL, 0);
216 if (!lock)
217 die_errno("Failed to lock ref for update");
218 }
e496c003
DB
219
220 if (reflog)
221 log_all_ref_updates = 1;
222
223 if (forcing)
da288e25 224 snprintf(msg, sizeof msg, "branch: Reset to %s",
e496c003 225 start_name);
4fc50066 226 else if (!dont_change_ref)
e496c003
DB
227 snprintf(msg, sizeof msg, "branch: Created from %s",
228 start_name);
229
e496c003 230 if (real_ref && track)
e9d4f740 231 setup_tracking(ref.buf+11, real_ref, track);
e496c003 232
4fc50066
IL
233 if (!dont_change_ref)
234 if (write_ref_sha1(lock, sha1, msg) < 0)
235 die_errno("Failed to write ref");
e496c003 236
8415d5c7 237 strbuf_release(&ref);
9ed36cfa 238 free(real_ref);
e496c003 239}
c369e7b8
DB
240
241void remove_branch_state(void)
242{
d7e5c0cb 243 unlink(git_path("CHERRY_PICK_HEAD"));
82433cdf 244 unlink(git_path("REVERT_HEAD"));
c369e7b8 245 unlink(git_path("MERGE_HEAD"));
0af0ac7e 246 unlink(git_path("MERGE_RR"));
c369e7b8 247 unlink(git_path("MERGE_MSG"));
cf10f9fd 248 unlink(git_path("MERGE_MODE"));
c369e7b8 249 unlink(git_path("SQUASH_MSG"));
95eb88d8 250 remove_sequencer_state(0);
c369e7b8 251}