Merge branch 'fc/cherry-pick' into integration
[git] / branch.c
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "branch.h"
4 #include "refs.h"
5 #include "remote.h"
6 #include "commit.h"
7
8 struct tracking {
9         struct refspec spec;
10         char *src;
11         const char *remote;
12         int matches;
13 };
14
15 static 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
36 static int should_setup_rebase(const char *origin)
37 {
38         switch (autorebase) {
39         case AUTOREBASE_NEVER:
40                 return 0;
41         case AUTOREBASE_LOCAL:
42                 return origin == NULL;
43         case AUTOREBASE_REMOTE:
44                 return origin != NULL;
45         case AUTOREBASE_ALWAYS:
46                 return 1;
47         }
48         return 0;
49 }
50
51 void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
52 {
53         const char *shortname = NULL;
54         struct strbuf key = STRBUF_INIT;
55         int rebasing = should_setup_rebase(origin);
56
57         if (skip_prefix(remote, "refs/heads/", &shortname)
58             && !strcmp(local, shortname)
59             && !origin) {
60                 warning(_("Not setting branch %s as its own upstream."),
61                         local);
62                 return;
63         }
64
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         strbuf_release(&key);
78
79         if (flag & BRANCH_CONFIG_VERBOSE) {
80                 if (shortname) {
81                         if (origin)
82                                 printf_ln(rebasing ?
83                                           _("Branch %s set up to track remote branch %s from %s by rebasing.") :
84                                           _("Branch %s set up to track remote branch %s from %s."),
85                                           local, shortname, origin);
86                         else
87                                 printf_ln(rebasing ?
88                                           _("Branch %s set up to track local branch %s by rebasing.") :
89                                           _("Branch %s set up to track local branch %s."),
90                                           local, shortname);
91                 } else {
92                         if (origin)
93                                 printf_ln(rebasing ?
94                                           _("Branch %s set up to track remote ref %s by rebasing.") :
95                                           _("Branch %s set up to track remote ref %s."),
96                                           local, remote);
97                         else
98                                 printf_ln(rebasing ?
99                                           _("Branch %s set up to track local ref %s by rebasing.") :
100                                           _("Branch %s set up to track local ref %s."),
101                                           local, remote);
102                 }
103         }
104 }
105
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  */
111 static int setup_tracking(const char *new_ref, const char *orig_ref,
112                           enum branch_track track, int quiet)
113 {
114         struct tracking tracking;
115         int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
116
117         memset(&tracking, 0, sizeof(tracking));
118         tracking.spec.dst = (char *)orig_ref;
119         if (for_each_remote(find_tracked_branch, &tracking))
120                 return 1;
121
122         if (!tracking.matches)
123                 switch (track) {
124                 case BRANCH_TRACK_ALWAYS:
125                 case BRANCH_TRACK_EXPLICIT:
126                 case BRANCH_TRACK_OVERRIDE:
127                         break;
128                 default:
129                         return 1;
130                 }
131
132         if (tracking.matches > 1)
133                 return error(_("Not tracking: ambiguous information for ref %s"),
134                                 orig_ref);
135
136         install_branch_config(config_flags, new_ref, tracking.remote,
137                               tracking.src ? tracking.src : orig_ref);
138
139         free(tracking.src);
140         return 0;
141 }
142
143 void install_branch_publish(const char *name, const char *remote, const char *remote_ref)
144 {
145         struct strbuf key = STRBUF_INIT;
146
147         if (!remote && !strcmp(name, remote_ref + 11) &&
148                         starts_with(remote_ref, "refs/heads")) {
149                 warning(_("Not setting branch %s as its own publish branch."), name);
150                 return;
151         }
152
153         strbuf_addf(&key, "branch.%s.pushremote", name);
154         git_config_set(key.buf, remote ? remote : ".");
155
156         strbuf_reset(&key);
157         strbuf_addf(&key, "branch.%s.push", name);
158         git_config_set(key.buf, remote_ref);
159
160         strbuf_release(&key);
161 }
162
163 int setup_publish(const char *name, const char *ref)
164 {
165         struct tracking tracking;
166         const char *remote, *remote_ref;
167
168         memset(&tracking, 0, sizeof(tracking));
169         tracking.spec.dst = (char *)ref;
170         if (for_each_remote(find_tracked_branch, &tracking))
171                 return 1;
172
173         if (tracking.matches > 1)
174                 return error(_("Not tracking: ambiguous information for ref %s"),
175                                 ref);
176
177         remote = tracking.remote;
178         remote_ref = tracking.src ? tracking.src : ref;
179
180         install_branch_publish(name, remote, remote_ref);
181
182         free(tracking.src);
183
184         return 0;
185 }
186
187 int read_branch_desc(struct strbuf *buf, const char *branch_name)
188 {
189         char *v = NULL;
190         struct strbuf name = STRBUF_INIT;
191         strbuf_addf(&name, "branch.%s.description", branch_name);
192         if (git_config_get_string(name.buf, &v)) {
193                 strbuf_release(&name);
194                 return -1;
195         }
196         strbuf_addstr(buf, v);
197         free(v);
198         strbuf_release(&name);
199         return 0;
200 }
201
202 int validate_new_branchname(const char *name, struct strbuf *ref,
203                             int force, int attr_only)
204 {
205         if (strbuf_check_branch_ref(ref, name))
206                 die(_("'%s' is not a valid branch name."), name);
207
208         if (!ref_exists(ref->buf))
209                 return 0;
210         else if (!force && !attr_only)
211                 die(_("A branch named '%s' already exists."), ref->buf + strlen("refs/heads/"));
212
213         if (!attr_only) {
214                 const char *head;
215                 unsigned char sha1[20];
216
217                 head = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
218                 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
219                         die(_("Cannot force update the current branch."));
220         }
221         return 1;
222 }
223
224 static int check_tracking_branch(struct remote *remote, void *cb_data)
225 {
226         char *tracking_branch = cb_data;
227         struct refspec query;
228         memset(&query, 0, sizeof(struct refspec));
229         query.dst = tracking_branch;
230         return !remote_find_tracking(remote, &query);
231 }
232
233 static int validate_remote_tracking_branch(char *ref)
234 {
235         return !for_each_remote(check_tracking_branch, ref);
236 }
237
238 static const char upstream_not_branch[] =
239 N_("Cannot setup tracking information; starting point '%s' is not a branch.");
240 static const char upstream_missing[] =
241 N_("the requested upstream branch '%s' does not exist");
242 static const char upstream_advice[] =
243 N_("\n"
244 "If you are planning on basing your work on an upstream\n"
245 "branch that already exists at the remote, you may need to\n"
246 "run \"git fetch\" to retrieve it.\n"
247 "\n"
248 "If you are planning to push out a new local branch that\n"
249 "will track its remote counterpart, you may want to use\n"
250 "\"git push -u\" to set the upstream config as you push.");
251
252 void create_branch(const char *head,
253                    const char *name, const char *start_name,
254                    int force, int reflog, int clobber_head,
255                    int quiet, enum branch_track track)
256 {
257         struct commit *commit;
258         unsigned char sha1[20];
259         char *real_ref, msg[PATH_MAX + 20];
260         struct strbuf ref = STRBUF_INIT;
261         int forcing = 0;
262         int dont_change_ref = 0;
263         int explicit_tracking = 0;
264
265         if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
266                 explicit_tracking = 1;
267
268         if (validate_new_branchname(name, &ref, force,
269                                     track == BRANCH_TRACK_OVERRIDE ||
270                                     clobber_head)) {
271                 if (!force)
272                         dont_change_ref = 1;
273                 else
274                         forcing = 1;
275         }
276
277         real_ref = NULL;
278         if (get_sha1(start_name, sha1)) {
279                 if (explicit_tracking) {
280                         if (advice_set_upstream_failure) {
281                                 error(_(upstream_missing), start_name);
282                                 advise(_(upstream_advice));
283                                 exit(1);
284                         }
285                         die(_(upstream_missing), start_name);
286                 }
287                 die(_("Not a valid object name: '%s'."), start_name);
288         }
289
290         switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
291         case 0:
292                 /* Not branching from any existing branch */
293                 if (explicit_tracking)
294                         die(_(upstream_not_branch), start_name);
295                 break;
296         case 1:
297                 /* Unique completion -- good, only if it is a real branch */
298                 if (!starts_with(real_ref, "refs/heads/") &&
299                     validate_remote_tracking_branch(real_ref)) {
300                         if (explicit_tracking)
301                                 die(_(upstream_not_branch), start_name);
302                         else
303                                 real_ref = NULL;
304                 }
305                 break;
306         default:
307                 die(_("Ambiguous object name: '%s'."), start_name);
308                 break;
309         }
310
311         if ((commit = lookup_commit_reference(sha1)) == NULL)
312                 die(_("Not a valid branch point: '%s'."), start_name);
313         hashcpy(sha1, commit->object.sha1);
314
315         if (forcing)
316                 snprintf(msg, sizeof msg, "branch: Reset to %s",
317                          start_name);
318         else if (!dont_change_ref)
319                 snprintf(msg, sizeof msg, "branch: Created from %s",
320                          start_name);
321
322         if (reflog)
323                 log_all_ref_updates = 1;
324
325         if (!dont_change_ref) {
326                 struct ref_transaction *transaction;
327                 struct strbuf err = STRBUF_INIT;
328
329                 transaction = ref_transaction_begin(&err);
330                 if (!transaction ||
331                     ref_transaction_update(transaction, ref.buf,
332                                            sha1, forcing ? NULL : null_sha1,
333                                            0, msg, &err) ||
334                     ref_transaction_commit(transaction, &err))
335                         die("%s", err.buf);
336                 ref_transaction_free(transaction);
337                 strbuf_release(&err);
338         }
339
340         if (real_ref && track)
341                 setup_tracking(ref.buf + 11, real_ref, track, quiet);
342
343         strbuf_release(&ref);
344         free(real_ref);
345 }
346
347 void remove_branch_state(void)
348 {
349         unlink(git_path_cherry_pick_head());
350         unlink(git_path_revert_head());
351         unlink(git_path_merge_head());
352         unlink(git_path_merge_rr());
353         unlink(git_path_merge_msg());
354         unlink(git_path_merge_mode());
355         unlink(git_path_squash_msg());
356 }
357
358 static char *find_linked_symref(const char *symref, const char *branch,
359                                 const char *id)
360 {
361         struct strbuf sb = STRBUF_INIT;
362         struct strbuf path = STRBUF_INIT;
363         struct strbuf gitdir = STRBUF_INIT;
364         char *existing = NULL;
365
366         /*
367          * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside
368          * $GIT_DIR so resolve_ref_unsafe() won't work (it uses
369          * git_path). Parse the ref ourselves.
370          */
371         if (id)
372                 strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
373         else
374                 strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
375
376         if (!strbuf_readlink(&sb, path.buf, 0)) {
377                 if (!starts_with(sb.buf, "refs/") ||
378                     check_refname_format(sb.buf, 0))
379                         goto done;
380         } else if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
381             starts_with(sb.buf, "ref:")) {
382                 strbuf_remove(&sb, 0, strlen("ref:"));
383                 strbuf_trim(&sb);
384         } else
385                 goto done;
386         if (strcmp(sb.buf, branch))
387                 goto done;
388         if (id) {
389                 strbuf_reset(&path);
390                 strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
391                 if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
392                         goto done;
393                 strbuf_rtrim(&gitdir);
394         } else
395                 strbuf_addstr(&gitdir, get_git_common_dir());
396         strbuf_strip_suffix(&gitdir, ".git");
397
398         existing = strbuf_detach(&gitdir, NULL);
399 done:
400         strbuf_release(&path);
401         strbuf_release(&sb);
402         strbuf_release(&gitdir);
403
404         return existing;
405 }
406
407 char *find_shared_symref(const char *symref, const char *target)
408 {
409         struct strbuf path = STRBUF_INIT;
410         DIR *dir;
411         struct dirent *d;
412         char *existing;
413
414         if ((existing = find_linked_symref(symref, target, NULL)))
415                 return existing;
416
417         strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
418         dir = opendir(path.buf);
419         strbuf_release(&path);
420         if (!dir)
421                 return NULL;
422
423         while ((d = readdir(dir)) != NULL) {
424                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
425                         continue;
426                 existing = find_linked_symref(symref, target, d->d_name);
427                 if (existing)
428                         goto done;
429         }
430 done:
431         closedir(dir);
432
433         return existing;
434 }
435
436 void die_if_checked_out(const char *branch)
437 {
438         char *existing;
439
440         existing = find_shared_symref("HEAD", branch);
441         if (existing) {
442                 skip_prefix(branch, "refs/heads/", &branch);
443                 die(_("'%s' is already checked out at '%s'"), branch, existing);
444         }
445 }