Prevent force-updating of the current branch
[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"
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
a9f2c136 35static int should_setup_rebase(const char *origin)
c998ae9b
DS
36{
37 switch (autorebase) {
38 case AUTOREBASE_NEVER:
39 return 0;
40 case AUTOREBASE_LOCAL:
a9f2c136 41 return origin == NULL;
c998ae9b 42 case AUTOREBASE_REMOTE:
a9f2c136 43 return origin != NULL;
c998ae9b
DS
44 case AUTOREBASE_ALWAYS:
45 return 1;
46 }
47 return 0;
48}
49
a9f2c136
JH
50void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
51{
85e2233f
MM
52 const char *shortname = remote + 11;
53 int remote_is_branch = !prefixcmp(remote, "refs/heads/");
a9f2c136
JH
54 struct strbuf key = STRBUF_INIT;
55 int rebasing = should_setup_rebase(origin);
56
85e2233f
MM
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
a9f2c136
JH
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 }
77
72f60083
JH
78 if (flag & BRANCH_CONFIG_VERBOSE) {
79 strbuf_reset(&key);
80
81 strbuf_addstr(&key, origin ? "remote" : "local");
82
83 /* Are we tracking a proper "branch"? */
85e2233f
MM
84 if (remote_is_branch) {
85 strbuf_addf(&key, " branch %s", shortname);
72f60083
JH
86 if (origin)
87 strbuf_addf(&key, " from %s", origin);
88 }
89 else
90 strbuf_addf(&key, " ref %s", remote);
91 printf("Branch %s set up to track %s%s.\n",
92 local, key.buf,
93 rebasing ? " by rebasing" : "");
94 }
a9f2c136
JH
95 strbuf_release(&key);
96}
97
e496c003
DB
98/*
99 * This is called when new_ref is branched off of orig_ref, and tries
100 * to infer the settings for branch.<new_ref>.{remote,merge} from the
101 * config.
102 */
9ed36cfa
JS
103static int setup_tracking(const char *new_ref, const char *orig_ref,
104 enum branch_track track)
e496c003 105{
e496c003
DB
106 struct tracking tracking;
107
108 if (strlen(new_ref) > 1024 - 7 - 7 - 1)
109 return error("Tracking not set up: name too long: %s",
110 new_ref);
111
112 memset(&tracking, 0, sizeof(tracking));
113 tracking.spec.dst = (char *)orig_ref;
9ed36cfa 114 if (for_each_remote(find_tracked_branch, &tracking))
e496c003
DB
115 return 1;
116
9ed36cfa
JS
117 if (!tracking.matches)
118 switch (track) {
119 case BRANCH_TRACK_ALWAYS:
120 case BRANCH_TRACK_EXPLICIT:
4fc50066 121 case BRANCH_TRACK_OVERRIDE:
9ed36cfa
JS
122 break;
123 default:
124 return 1;
125 }
126
e496c003
DB
127 if (tracking.matches > 1)
128 return error("Not tracking: ambiguous information for ref %s",
129 orig_ref);
130
a9f2c136
JH
131 install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
132 tracking.src ? tracking.src : orig_ref);
e496c003 133
a9f2c136 134 free(tracking.src);
e496c003
DB
135 return 0;
136}
137
55c4a673
CI
138int validate_new_branchname(const char *name, struct strbuf *ref, int force)
139{
140 const char *head;
141 unsigned char sha1[20];
142
143 if (strbuf_check_branch_ref(ref, name))
144 die("'%s' is not a valid branch name.", name);
145
146 if (!ref_exists(ref->buf))
147 return 0;
148 else if (!force)
149 die("A branch named '%s' already exists.", name);
150
151 head = resolve_ref("HEAD", sha1, 0, NULL);
152 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
153 die("Cannot force update the current branch.");
154
155 return 1;
156}
157
e496c003
DB
158void create_branch(const char *head,
159 const char *name, const char *start_name,
9ed36cfa 160 int force, int reflog, enum branch_track track)
e496c003 161{
4fc50066 162 struct ref_lock *lock = NULL;
e496c003
DB
163 struct commit *commit;
164 unsigned char sha1[20];
8415d5c7
JH
165 char *real_ref, msg[PATH_MAX + 20];
166 struct strbuf ref = STRBUF_INIT;
e496c003 167 int forcing = 0;
4fc50066
IL
168 int dont_change_ref = 0;
169 int explicit_tracking = 0;
170
171 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
172 explicit_tracking = 1;
e496c003 173
55c4a673
CI
174 if (validate_new_branchname(name, &ref, force || track == BRANCH_TRACK_OVERRIDE)) {
175 if (!force)
4fc50066 176 dont_change_ref = 1;
55c4a673
CI
177 else
178 forcing = 1;
e496c003
DB
179 }
180
181 real_ref = NULL;
182 if (get_sha1(start_name, sha1))
183 die("Not a valid object name: '%s'.", start_name);
184
185 switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
186 case 0:
187 /* Not branching from any existing branch */
4fc50066 188 if (explicit_tracking)
9ed36cfa 189 die("Cannot setup tracking information; starting point is not a branch.");
e496c003
DB
190 break;
191 case 1:
21b5b1e8
JH
192 /* Unique completion -- good, only if it is a real branch */
193 if (prefixcmp(real_ref, "refs/heads/") &&
194 prefixcmp(real_ref, "refs/remotes/")) {
195 if (explicit_tracking)
196 die("Cannot setup tracking information; starting point is not a branch.");
197 else
198 real_ref = NULL;
199 }
e496c003
DB
200 break;
201 default:
202 die("Ambiguous object name: '%s'.", start_name);
203 break;
204 }
205
206 if ((commit = lookup_commit_reference(sha1)) == NULL)
207 die("Not a valid branch point: '%s'.", start_name);
208 hashcpy(sha1, commit->object.sha1);
209
4fc50066
IL
210 if (!dont_change_ref) {
211 lock = lock_any_ref_for_update(ref.buf, NULL, 0);
212 if (!lock)
213 die_errno("Failed to lock ref for update");
214 }
e496c003
DB
215
216 if (reflog)
217 log_all_ref_updates = 1;
218
219 if (forcing)
da288e25 220 snprintf(msg, sizeof msg, "branch: Reset to %s",
e496c003 221 start_name);
4fc50066 222 else if (!dont_change_ref)
e496c003
DB
223 snprintf(msg, sizeof msg, "branch: Created from %s",
224 start_name);
225
e496c003 226 if (real_ref && track)
9ed36cfa 227 setup_tracking(name, real_ref, track);
e496c003 228
4fc50066
IL
229 if (!dont_change_ref)
230 if (write_ref_sha1(lock, sha1, msg) < 0)
231 die_errno("Failed to write ref");
e496c003 232
8415d5c7 233 strbuf_release(&ref);
9ed36cfa 234 free(real_ref);
e496c003 235}
c369e7b8
DB
236
237void remove_branch_state(void)
238{
d7e5c0cb 239 unlink(git_path("CHERRY_PICK_HEAD"));
c369e7b8 240 unlink(git_path("MERGE_HEAD"));
0af0ac7e 241 unlink(git_path("MERGE_RR"));
c369e7b8 242 unlink(git_path("MERGE_MSG"));
cf10f9fd 243 unlink(git_path("MERGE_MODE"));
c369e7b8
DB
244 unlink(git_path("SQUASH_MSG"));
245}