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 "color.h"
9#include "cache.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> | [-r | -a] [-v [--abbrev=<length>]]";
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 (!strncmp(var, "color.branch.", 13)) {
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 in_merge_bases(const unsigned char *sha1,
78 struct commit *rev1,
79 struct commit *rev2)
80{
81 struct commit_list *bases, *b;
82 int ret = 0;
83
84 bases = get_merge_bases(rev1, rev2, 1);
85 for (b = bases; b; b = b->next) {
86 if (!hashcmp(sha1, b->item->object.sha1)) {
87 ret = 1;
88 break;
89 }
90 }
91
92 free_commit_list(bases);
93 return ret;
94}
95
96static void delete_branches(int argc, const char **argv, int force, int kinds)
97{
98 struct commit *rev, *head_rev = head_rev;
99 unsigned char sha1[20];
100 char *name;
101 const char *fmt, *remote;
102 int i;
103
104 switch (kinds) {
105 case REF_REMOTE_BRANCH:
106 fmt = "refs/remotes/%s";
107 remote = "remote ";
108 force = 1;
109 break;
110 case REF_LOCAL_BRANCH:
111 fmt = "refs/heads/%s";
112 remote = "";
113 break;
114 default:
115 die("cannot use -a with -d");
116 }
117
118 if (!force) {
119 head_rev = lookup_commit_reference(head_sha1);
120 if (!head_rev)
121 die("Couldn't look up commit object for HEAD");
122 }
123 for (i = 0; i < argc; i++) {
124 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i]))
125 die("Cannot delete the branch you are currently on.");
126
127 name = xstrdup(mkpath(fmt, argv[i]));
128 if (!resolve_ref(name, sha1, 1, NULL))
129 die("%sbranch '%s' not found.", remote, argv[i]);
130
131 rev = lookup_commit_reference(sha1);
132 if (!rev)
133 die("Couldn't look up commit object for '%s'", name);
134
135 /* This checks whether the merge bases of branch and
136 * HEAD contains branch -- which means that the HEAD
137 * contains everything in both.
138 */
139
140 if (!force &&
141 !in_merge_bases(sha1, rev, head_rev)) {
142 fprintf(stderr,
143 "The branch '%s' is not a strict subset of your current HEAD.\n"
144 "If you are sure you want to delete it, run 'git branch -D %s'.\n",
145 argv[i], argv[i]);
146 exit(1);
147 }
148
149 if (delete_ref(name, sha1))
150 printf("Error deleting %sbranch '%s'\n", remote,
151 argv[i]);
152 else
153 printf("Deleted %sbranch %s.\n", remote, argv[i]);
154
155 free(name);
156 }
157}
158
159struct ref_item {
160 char *name;
161 unsigned int kind;
162 unsigned char sha1[20];
163};
164
165struct ref_list {
166 int index, alloc, maxwidth;
167 struct ref_item *list;
168 int kinds;
169};
170
171static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
172{
173 struct ref_list *ref_list = (struct ref_list*)(cb_data);
174 struct ref_item *newitem;
175 int kind = REF_UNKNOWN_TYPE;
176 int len;
177
178 /* Detect kind */
179 if (!strncmp(refname, "refs/heads/", 11)) {
180 kind = REF_LOCAL_BRANCH;
181 refname += 11;
182 } else if (!strncmp(refname, "refs/remotes/", 13)) {
183 kind = REF_REMOTE_BRANCH;
184 refname += 13;
185 } else if (!strncmp(refname, "refs/tags/", 10)) {
186 kind = REF_TAG;
187 refname += 10;
188 }
189
190 /* Don't add types the caller doesn't want */
191 if ((kind & ref_list->kinds) == 0)
192 return 0;
193
194 /* Resize buffer */
195 if (ref_list->index >= ref_list->alloc) {
196 ref_list->alloc = alloc_nr(ref_list->alloc);
197 ref_list->list = xrealloc(ref_list->list,
198 ref_list->alloc * sizeof(struct ref_item));
199 }
200
201 /* Record the new item */
202 newitem = &(ref_list->list[ref_list->index++]);
203 newitem->name = xstrdup(refname);
204 newitem->kind = kind;
205 hashcpy(newitem->sha1, sha1);
206 len = strlen(newitem->name);
207 if (len > ref_list->maxwidth)
208 ref_list->maxwidth = len;
209
210 return 0;
211}
212
213static void free_ref_list(struct ref_list *ref_list)
214{
215 int i;
216
217 for (i = 0; i < ref_list->index; i++)
218 free(ref_list->list[i].name);
219 free(ref_list->list);
220}
221
222static int ref_cmp(const void *r1, const void *r2)
223{
224 struct ref_item *c1 = (struct ref_item *)(r1);
225 struct ref_item *c2 = (struct ref_item *)(r2);
226
227 if (c1->kind != c2->kind)
228 return c1->kind - c2->kind;
229 return strcmp(c1->name, c2->name);
230}
231
232static void print_ref_info(const unsigned char *sha1, int abbrev)
233{
234 struct commit *commit;
235 char subject[256];
236
237
238 commit = lookup_commit(sha1);
239 if (commit && !parse_commit(commit))
240 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
241 subject, sizeof(subject), 0,
242 NULL, NULL, 0);
243 else
244 strcpy(subject, " **** invalid ref ****");
245
246 printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
247}
248
249static void print_ref_list(int kinds, int verbose, int abbrev)
250{
251 int i;
252 char c;
253 struct ref_list ref_list;
254 int color;
255
256 memset(&ref_list, 0, sizeof(ref_list));
257 ref_list.kinds = kinds;
258 for_each_ref(append_ref, &ref_list);
259
260 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
261
262 for (i = 0; i < ref_list.index; i++) {
263 switch( ref_list.list[i].kind ) {
264 case REF_LOCAL_BRANCH:
265 color = COLOR_BRANCH_LOCAL;
266 break;
267 case REF_REMOTE_BRANCH:
268 color = COLOR_BRANCH_REMOTE;
269 break;
270 default:
271 color = COLOR_BRANCH_PLAIN;
272 break;
273 }
274
275 c = ' ';
276 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
277 !strcmp(ref_list.list[i].name, head)) {
278 c = '*';
279 color = COLOR_BRANCH_CURRENT;
280 }
281
282 if (verbose) {
283 printf("%c %s%-*s%s", c,
284 branch_get_color(color),
285 ref_list.maxwidth,
286 ref_list.list[i].name,
287 branch_get_color(COLOR_BRANCH_RESET));
288 print_ref_info(ref_list.list[i].sha1, abbrev);
289 }
290 else
291 printf("%c %s%s%s\n", c,
292 branch_get_color(color),
293 ref_list.list[i].name,
294 branch_get_color(COLOR_BRANCH_RESET));
295 }
296
297 free_ref_list(&ref_list);
298}
299
300static void create_branch(const char *name, const char *start,
301 int force, int reflog)
302{
303 struct ref_lock *lock;
304 struct commit *commit;
305 unsigned char sha1[20];
306 char ref[PATH_MAX], msg[PATH_MAX + 20];
307
308 snprintf(ref, sizeof ref, "refs/heads/%s", name);
309 if (check_ref_format(ref))
310 die("'%s' is not a valid branch name.", name);
311
312 if (resolve_ref(ref, sha1, 1, NULL)) {
313 if (!force)
314 die("A branch named '%s' already exists.", name);
315 else if (!strcmp(head, name))
316 die("Cannot force update the current branch.");
317 }
318
319 if (get_sha1(start, sha1) ||
320 (commit = lookup_commit_reference(sha1)) == NULL)
321 die("Not a valid branch point: '%s'.", start);
322 hashcpy(sha1, commit->object.sha1);
323
324 lock = lock_any_ref_for_update(ref, NULL);
325 if (!lock)
326 die("Failed to lock ref for update: %s.", strerror(errno));
327
328 if (reflog) {
329 log_all_ref_updates = 1;
330 snprintf(msg, sizeof msg, "branch: Created from %s", start);
331 }
332
333 if (write_ref_sha1(lock, sha1, msg) < 0)
334 die("Failed to write ref: %s.", strerror(errno));
335}
336
337static void rename_branch(const char *oldname, const char *newname, int force)
338{
339 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
340 unsigned char sha1[20];
341
342 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
343 die("Old branchname too long");
344
345 if (check_ref_format(oldref))
346 die("Invalid branch name: %s", oldref);
347
348 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
349 die("New branchname too long");
350
351 if (check_ref_format(newref))
352 die("Invalid branch name: %s", newref);
353
354 if (resolve_ref(newref, sha1, 1, NULL) && !force)
355 die("A branch named '%s' already exists.", newname);
356
357 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
358 oldref, newref);
359
360 if (rename_ref(oldref, newref, logmsg))
361 die("Branch rename failed");
362
363 if (!strcmp(oldname, head) && create_symref("HEAD", newref))
364 die("Branch renamed to %s, but HEAD is not updated!", newname);
365}
366
367int cmd_branch(int argc, const char **argv, const char *prefix)
368{
369 int delete = 0, force_delete = 0, force_create = 0;
370 int rename = 0, force_rename = 0;
371 int verbose = 0, abbrev = DEFAULT_ABBREV;
372 int reflog = 0;
373 int kinds = REF_LOCAL_BRANCH;
374 int i;
375
376 setup_ident();
377 git_config(git_branch_config);
378
379 for (i = 1; i < argc; i++) {
380 const char *arg = argv[i];
381
382 if (arg[0] != '-')
383 break;
384 if (!strcmp(arg, "--")) {
385 i++;
386 break;
387 }
388 if (!strcmp(arg, "-d")) {
389 delete = 1;
390 continue;
391 }
392 if (!strcmp(arg, "-D")) {
393 delete = 1;
394 force_delete = 1;
395 continue;
396 }
397 if (!strcmp(arg, "-f")) {
398 force_create = 1;
399 continue;
400 }
401 if (!strcmp(arg, "-m")) {
402 rename = 1;
403 continue;
404 }
405 if (!strcmp(arg, "-M")) {
406 rename = 1;
407 force_rename = 1;
408 continue;
409 }
410 if (!strcmp(arg, "-r")) {
411 kinds = REF_REMOTE_BRANCH;
412 continue;
413 }
414 if (!strcmp(arg, "-a")) {
415 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
416 continue;
417 }
418 if (!strcmp(arg, "-l")) {
419 reflog = 1;
420 continue;
421 }
422 if (!strncmp(arg, "--abbrev=", 9)) {
423 abbrev = atoi(arg+9);
424 continue;
425 }
426 if (!strcmp(arg, "-v")) {
427 verbose = 1;
428 continue;
429 }
430 if (!strcmp(arg, "--color")) {
431 branch_use_color = 1;
432 continue;
433 }
434 if (!strcmp(arg, "--no-color")) {
435 branch_use_color = 0;
436 continue;
437 }
438 usage(builtin_branch_usage);
439 }
440
441 if ((delete && rename) || (delete && force_create) ||
442 (rename && force_create))
443 usage(builtin_branch_usage);
444
445 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
446 if (!head)
447 die("Failed to resolve HEAD as a valid ref.");
448 if (strncmp(head, "refs/heads/", 11))
449 die("HEAD not found below refs/heads!");
450 head += 11;
451
452 if (delete)
453 delete_branches(argc - i, argv + i, force_delete, kinds);
454 else if (i == argc)
455 print_ref_list(kinds, verbose, abbrev);
456 else if (rename && (i == argc - 1))
457 rename_branch(head, argv[i], force_rename);
458 else if (rename && (i == argc - 2))
459 rename_branch(argv[i], argv[i + 1], force_rename);
460 else if (i == argc - 1)
461 create_branch(argv[i], head, force_create, reflog);
462 else if (i == argc - 2)
463 create_branch(argv[i], argv[i + 1], force_create, reflog);
464 else
465 usage(builtin_branch_usage);
466
467 return 0;
468}