1/*
2 * Builtin "git branch"
3 *
4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
6 */
7
8#include "cache.h"
9#include "color.h"
10#include "refs.h"
11#include "commit.h"
12#include "builtin.h"
13#include "remote.h"
14#include "parse-options.h"
15#include "branch.h"
16
17static const char * const builtin_branch_usage[] = {
18 "git-branch [options] [-r | -a]",
19 "git-branch [options] [-l] [-f] <branchname> [<start-point>]",
20 "git-branch [options] [-r] (-d | -D) <branchname>",
21 "git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
22 NULL
23};
24
25#define REF_UNKNOWN_TYPE 0x00
26#define REF_LOCAL_BRANCH 0x01
27#define REF_REMOTE_BRANCH 0x02
28#define REF_TAG 0x04
29
30static const char *head;
31static unsigned char head_sha1[20];
32
33static int branch_track = 1;
34
35static int branch_use_color = -1;
36static char branch_colors[][COLOR_MAXLEN] = {
37 "\033[m", /* reset */
38 "", /* PLAIN (normal) */
39 "\033[31m", /* REMOTE (red) */
40 "", /* LOCAL (normal) */
41 "\033[32m", /* CURRENT (green) */
42};
43enum color_branch {
44 COLOR_BRANCH_RESET = 0,
45 COLOR_BRANCH_PLAIN = 1,
46 COLOR_BRANCH_REMOTE = 2,
47 COLOR_BRANCH_LOCAL = 3,
48 COLOR_BRANCH_CURRENT = 4,
49};
50
51static int parse_branch_color_slot(const char *var, int ofs)
52{
53 if (!strcasecmp(var+ofs, "plain"))
54 return COLOR_BRANCH_PLAIN;
55 if (!strcasecmp(var+ofs, "reset"))
56 return COLOR_BRANCH_RESET;
57 if (!strcasecmp(var+ofs, "remote"))
58 return COLOR_BRANCH_REMOTE;
59 if (!strcasecmp(var+ofs, "local"))
60 return COLOR_BRANCH_LOCAL;
61 if (!strcasecmp(var+ofs, "current"))
62 return COLOR_BRANCH_CURRENT;
63 die("bad config variable '%s'", var);
64}
65
66static int git_branch_config(const char *var, const char *value)
67{
68 if (!strcmp(var, "color.branch")) {
69 branch_use_color = git_config_colorbool(var, value, -1);
70 return 0;
71 }
72 if (!prefixcmp(var, "color.branch.")) {
73 int slot = parse_branch_color_slot(var, 13);
74 if (!value)
75 return config_error_nonbool(var);
76 color_parse(value, var, branch_colors[slot]);
77 return 0;
78 }
79 if (!strcmp(var, "branch.autosetupmerge")) {
80 branch_track = git_config_bool(var, value);
81 return 0;
82 }
83 return git_color_default_config(var, value);
84}
85
86static const char *branch_get_color(enum color_branch ix)
87{
88 if (branch_use_color > 0)
89 return branch_colors[ix];
90 return "";
91}
92
93static int delete_branches(int argc, const char **argv, int force, int kinds)
94{
95 struct commit *rev, *head_rev = head_rev;
96 unsigned char sha1[20];
97 char *name = NULL;
98 const char *fmt, *remote;
99 char section[PATH_MAX];
100 int i;
101 int ret = 0;
102
103 switch (kinds) {
104 case REF_REMOTE_BRANCH:
105 fmt = "refs/remotes/%s";
106 remote = "remote ";
107 force = 1;
108 break;
109 case REF_LOCAL_BRANCH:
110 fmt = "refs/heads/%s";
111 remote = "";
112 break;
113 default:
114 die("cannot use -a with -d");
115 }
116
117 if (!force) {
118 head_rev = lookup_commit_reference(head_sha1);
119 if (!head_rev)
120 die("Couldn't look up commit object for HEAD");
121 }
122 for (i = 0; i < argc; i++) {
123 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) {
124 error("Cannot delete the branch '%s' "
125 "which you are currently on.", argv[i]);
126 ret = 1;
127 continue;
128 }
129
130 if (name)
131 free(name);
132
133 name = xstrdup(mkpath(fmt, argv[i]));
134 if (!resolve_ref(name, sha1, 1, NULL)) {
135 error("%sbranch '%s' not found.",
136 remote, argv[i]);
137 ret = 1;
138 continue;
139 }
140
141 rev = lookup_commit_reference(sha1);
142 if (!rev) {
143 error("Couldn't look up commit object for '%s'", name);
144 ret = 1;
145 continue;
146 }
147
148 /* This checks whether the merge bases of branch and
149 * HEAD contains branch -- which means that the HEAD
150 * contains everything in both.
151 */
152
153 if (!force &&
154 !in_merge_bases(rev, &head_rev, 1)) {
155 error("The branch '%s' is not an ancestor of "
156 "your current HEAD.\n"
157 "If you are sure you want to delete it, "
158 "run 'git branch -D %s'.", argv[i], argv[i]);
159 ret = 1;
160 continue;
161 }
162
163 if (delete_ref(name, sha1)) {
164 error("Error deleting %sbranch '%s'", remote,
165 argv[i]);
166 ret = 1;
167 } else {
168 printf("Deleted %sbranch %s.\n", remote, argv[i]);
169 snprintf(section, sizeof(section), "branch.%s",
170 argv[i]);
171 if (git_config_rename_section(section, NULL) < 0)
172 warning("Update of config-file failed");
173 }
174 }
175
176 if (name)
177 free(name);
178
179 return(ret);
180}
181
182struct ref_item {
183 char *name;
184 unsigned int kind;
185 unsigned char sha1[20];
186};
187
188struct ref_list {
189 int index, alloc, maxwidth;
190 struct ref_item *list;
191 struct commit_list *with_commit;
192 int kinds;
193};
194
195static int has_commit(const unsigned char *sha1, struct commit_list *with_commit)
196{
197 struct commit *commit;
198
199 if (!with_commit)
200 return 1;
201 commit = lookup_commit_reference_gently(sha1, 1);
202 if (!commit)
203 return 0;
204 while (with_commit) {
205 struct commit *other;
206
207 other = with_commit->item;
208 with_commit = with_commit->next;
209 if (in_merge_bases(other, &commit, 1))
210 return 1;
211 }
212 return 0;
213}
214
215static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
216{
217 struct ref_list *ref_list = (struct ref_list*)(cb_data);
218 struct ref_item *newitem;
219 int kind = REF_UNKNOWN_TYPE;
220 int len;
221
222 /* Detect kind */
223 if (!prefixcmp(refname, "refs/heads/")) {
224 kind = REF_LOCAL_BRANCH;
225 refname += 11;
226 } else if (!prefixcmp(refname, "refs/remotes/")) {
227 kind = REF_REMOTE_BRANCH;
228 refname += 13;
229 } else if (!prefixcmp(refname, "refs/tags/")) {
230 kind = REF_TAG;
231 refname += 10;
232 }
233
234 /* Filter with with_commit if specified */
235 if (!has_commit(sha1, ref_list->with_commit))
236 return 0;
237
238 /* Don't add types the caller doesn't want */
239 if ((kind & ref_list->kinds) == 0)
240 return 0;
241
242 /* Resize buffer */
243 if (ref_list->index >= ref_list->alloc) {
244 ref_list->alloc = alloc_nr(ref_list->alloc);
245 ref_list->list = xrealloc(ref_list->list,
246 ref_list->alloc * sizeof(struct ref_item));
247 }
248
249 /* Record the new item */
250 newitem = &(ref_list->list[ref_list->index++]);
251 newitem->name = xstrdup(refname);
252 newitem->kind = kind;
253 hashcpy(newitem->sha1, sha1);
254 len = strlen(newitem->name);
255 if (len > ref_list->maxwidth)
256 ref_list->maxwidth = len;
257
258 return 0;
259}
260
261static void free_ref_list(struct ref_list *ref_list)
262{
263 int i;
264
265 for (i = 0; i < ref_list->index; i++)
266 free(ref_list->list[i].name);
267 free(ref_list->list);
268}
269
270static int ref_cmp(const void *r1, const void *r2)
271{
272 struct ref_item *c1 = (struct ref_item *)(r1);
273 struct ref_item *c2 = (struct ref_item *)(r2);
274
275 if (c1->kind != c2->kind)
276 return c1->kind - c2->kind;
277 return strcmp(c1->name, c2->name);
278}
279
280static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
281 int abbrev, int current)
282{
283 char c;
284 int color;
285 struct commit *commit;
286
287 switch (item->kind) {
288 case REF_LOCAL_BRANCH:
289 color = COLOR_BRANCH_LOCAL;
290 break;
291 case REF_REMOTE_BRANCH:
292 color = COLOR_BRANCH_REMOTE;
293 break;
294 default:
295 color = COLOR_BRANCH_PLAIN;
296 break;
297 }
298
299 c = ' ';
300 if (current) {
301 c = '*';
302 color = COLOR_BRANCH_CURRENT;
303 }
304
305 if (verbose) {
306 struct strbuf subject;
307 const char *sub = " **** invalid ref ****";
308
309 strbuf_init(&subject, 0);
310
311 commit = lookup_commit(item->sha1);
312 if (commit && !parse_commit(commit)) {
313 pretty_print_commit(CMIT_FMT_ONELINE, commit,
314 &subject, 0, NULL, NULL, 0, 0);
315 sub = subject.buf;
316 }
317 printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color),
318 maxwidth, item->name,
319 branch_get_color(COLOR_BRANCH_RESET),
320 find_unique_abbrev(item->sha1, abbrev), sub);
321 strbuf_release(&subject);
322 } else {
323 printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
324 branch_get_color(COLOR_BRANCH_RESET));
325 }
326}
327
328static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
329{
330 int i;
331 struct ref_list ref_list;
332
333 memset(&ref_list, 0, sizeof(ref_list));
334 ref_list.kinds = kinds;
335 ref_list.with_commit = with_commit;
336 for_each_ref(append_ref, &ref_list);
337
338 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
339
340 detached = (detached && (kinds & REF_LOCAL_BRANCH));
341 if (detached && has_commit(head_sha1, with_commit)) {
342 struct ref_item item;
343 item.name = xstrdup("(no branch)");
344 item.kind = REF_LOCAL_BRANCH;
345 hashcpy(item.sha1, head_sha1);
346 if (strlen(item.name) > ref_list.maxwidth)
347 ref_list.maxwidth = strlen(item.name);
348 print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
349 free(item.name);
350 }
351
352 for (i = 0; i < ref_list.index; i++) {
353 int current = !detached &&
354 (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
355 !strcmp(ref_list.list[i].name, head);
356 print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
357 abbrev, current);
358 }
359
360 free_ref_list(&ref_list);
361}
362
363static void rename_branch(const char *oldname, const char *newname, int force)
364{
365 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
366 unsigned char sha1[20];
367 char oldsection[PATH_MAX], newsection[PATH_MAX];
368
369 if (!oldname)
370 die("cannot rename the current branch while not on any.");
371
372 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
373 die("Old branchname too long");
374
375 if (check_ref_format(oldref))
376 die("Invalid branch name: %s", oldref);
377
378 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
379 die("New branchname too long");
380
381 if (check_ref_format(newref))
382 die("Invalid branch name: %s", newref);
383
384 if (resolve_ref(newref, sha1, 1, NULL) && !force)
385 die("A branch named '%s' already exists.", newname);
386
387 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
388 oldref, newref);
389
390 if (rename_ref(oldref, newref, logmsg))
391 die("Branch rename failed");
392
393 /* no need to pass logmsg here as HEAD didn't really move */
394 if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
395 die("Branch renamed to %s, but HEAD is not updated!", newname);
396
397 snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
398 snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
399 if (git_config_rename_section(oldsection, newsection) < 0)
400 die("Branch is renamed, but update of config-file failed");
401}
402
403static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
404{
405 unsigned char sha1[20];
406 struct commit *commit;
407
408 if (!arg)
409 return -1;
410 if (get_sha1(arg, sha1))
411 die("malformed object name %s", arg);
412 commit = lookup_commit_reference(sha1);
413 if (!commit)
414 die("no such commit %s", arg);
415 commit_list_insert(commit, opt->value);
416 return 0;
417}
418
419int cmd_branch(int argc, const char **argv, const char *prefix)
420{
421 int delete = 0, rename = 0, force_create = 0;
422 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
423 int reflog = 0, track;
424 int kinds = REF_LOCAL_BRANCH;
425 struct commit_list *with_commit = NULL;
426
427 struct option options[] = {
428 OPT_GROUP("Generic options"),
429 OPT__VERBOSE(&verbose),
430 OPT_BOOLEAN( 0 , "track", &track, "set up tracking mode (see git-pull(1))"),
431 OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"),
432 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
433 REF_REMOTE_BRANCH),
434 OPT_CALLBACK(0, "contains", &with_commit, "commit",
435 "print only branches that contain the commit",
436 opt_parse_with_commit),
437 {
438 OPTION_CALLBACK, 0, "with", &with_commit, "commit",
439 "print only branches that contain the commit",
440 PARSE_OPT_HIDDEN, opt_parse_with_commit,
441 },
442 OPT__ABBREV(&abbrev),
443
444 OPT_GROUP("Specific git-branch actions:"),
445 OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches",
446 REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
447 OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1),
448 OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
449 OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1),
450 OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
451 OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
452 OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"),
453 OPT_END(),
454 };
455
456 git_config(git_branch_config);
457
458 if (branch_use_color == -1)
459 branch_use_color = git_use_color_default;
460
461 track = branch_track;
462 argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
463 if (!!delete + !!rename + !!force_create > 1)
464 usage_with_options(builtin_branch_usage, options);
465
466 head = resolve_ref("HEAD", head_sha1, 0, NULL);
467 if (!head)
468 die("Failed to resolve HEAD as a valid ref.");
469 head = xstrdup(head);
470 if (!strcmp(head, "HEAD")) {
471 detached = 1;
472 } else {
473 if (prefixcmp(head, "refs/heads/"))
474 die("HEAD not found below refs/heads!");
475 head += 11;
476 }
477
478 if (delete)
479 return delete_branches(argc, argv, delete > 1, kinds);
480 else if (argc == 0)
481 print_ref_list(kinds, detached, verbose, abbrev, with_commit);
482 else if (rename && (argc == 1))
483 rename_branch(head, argv[0], rename > 1);
484 else if (rename && (argc == 2))
485 rename_branch(argv[0], argv[1], rename > 1);
486 else if (argc <= 2)
487 create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
488 force_create, reflog, track);
489 else
490 usage_with_options(builtin_branch_usage, options);
491
492 return 0;
493}