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