4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
14 static 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>]]";
17 #define REF_UNKNOWN_TYPE 0x00
18 #define REF_LOCAL_BRANCH 0x01
19 #define REF_REMOTE_BRANCH 0x02
22 static const char *head;
23 static unsigned char head_sha1[20];
25 static int branch_use_color;
26 static char branch_colors[][COLOR_MAXLEN] = {
28 "", /* PLAIN (normal) */
29 "\033[31m", /* REMOTE (red) */
30 "", /* LOCAL (normal) */
31 "\033[32m", /* CURRENT (green) */
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,
41 static int parse_branch_color_slot(const char *var, int ofs)
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);
56 int git_branch_config(const char *var, const char *value)
58 if (!strcmp(var, "color.branch")) {
59 branch_use_color = git_config_colorbool(var, value);
62 if (!strncmp(var, "color.branch.", 13)) {
63 int slot = parse_branch_color_slot(var, 13);
64 color_parse(value, var, branch_colors[slot]);
67 return git_default_config(var, value);
70 const char *branch_get_color(enum color_branch ix)
73 return branch_colors[ix];
77 static int in_merge_bases(const unsigned char *sha1,
81 struct commit_list *bases, *b;
84 bases = get_merge_bases(rev1, rev2, 1);
85 for (b = bases; b; b = b->next) {
86 if (!hashcmp(sha1, b->item->object.sha1)) {
92 free_commit_list(bases);
96 static int delete_branches(int argc, const char **argv, int force, int kinds)
98 struct commit *rev, *head_rev = head_rev;
99 unsigned char sha1[20];
101 const char *fmt, *remote;
106 case REF_REMOTE_BRANCH:
107 fmt = "refs/remotes/%s";
111 case REF_LOCAL_BRANCH:
112 fmt = "refs/heads/%s";
116 die("cannot use -a with -d");
120 head_rev = lookup_commit_reference(head_sha1);
122 die("Couldn't look up commit object for HEAD");
124 for (i = 0; i < argc; i++) {
125 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) {
126 error("Cannot delete the branch '%s' "
127 "which you are currently on.", argv[i]);
135 name = xstrdup(mkpath(fmt, argv[i]));
136 if (!resolve_ref(name, sha1, 1, NULL)) {
137 error("%sbranch '%s' not found.",
143 rev = lookup_commit_reference(sha1);
145 error("Couldn't look up commit object for '%s'", name);
150 /* This checks whether the merge bases of branch and
151 * HEAD contains branch -- which means that the HEAD
152 * contains everything in both.
156 !in_merge_bases(sha1, rev, head_rev)) {
157 error("The branch '%s' is not a strict subset of "
158 "your current HEAD.\n"
159 "If you are sure you want to delete it, "
160 "run 'git branch -D %s'.", argv[i], argv[i]);
165 if (delete_ref(name, sha1)) {
166 error("Error deleting %sbranch '%s'", remote,
170 printf("Deleted %sbranch %s.\n", remote, argv[i]);
183 unsigned char sha1[20];
187 int index, alloc, maxwidth;
188 struct ref_item *list;
192 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
194 struct ref_list *ref_list = (struct ref_list*)(cb_data);
195 struct ref_item *newitem;
196 int kind = REF_UNKNOWN_TYPE;
200 if (!strncmp(refname, "refs/heads/", 11)) {
201 kind = REF_LOCAL_BRANCH;
203 } else if (!strncmp(refname, "refs/remotes/", 13)) {
204 kind = REF_REMOTE_BRANCH;
206 } else if (!strncmp(refname, "refs/tags/", 10)) {
211 /* Don't add types the caller doesn't want */
212 if ((kind & ref_list->kinds) == 0)
216 if (ref_list->index >= ref_list->alloc) {
217 ref_list->alloc = alloc_nr(ref_list->alloc);
218 ref_list->list = xrealloc(ref_list->list,
219 ref_list->alloc * sizeof(struct ref_item));
222 /* Record the new item */
223 newitem = &(ref_list->list[ref_list->index++]);
224 newitem->name = xstrdup(refname);
225 newitem->kind = kind;
226 hashcpy(newitem->sha1, sha1);
227 len = strlen(newitem->name);
228 if (len > ref_list->maxwidth)
229 ref_list->maxwidth = len;
234 static void free_ref_list(struct ref_list *ref_list)
238 for (i = 0; i < ref_list->index; i++)
239 free(ref_list->list[i].name);
240 free(ref_list->list);
243 static int ref_cmp(const void *r1, const void *r2)
245 struct ref_item *c1 = (struct ref_item *)(r1);
246 struct ref_item *c2 = (struct ref_item *)(r2);
248 if (c1->kind != c2->kind)
249 return c1->kind - c2->kind;
250 return strcmp(c1->name, c2->name);
253 static void print_ref_info(const unsigned char *sha1, int abbrev)
255 struct commit *commit;
259 commit = lookup_commit(sha1);
260 if (commit && !parse_commit(commit))
261 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
262 subject, sizeof(subject), 0,
265 strcpy(subject, " **** invalid ref ****");
267 printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
270 static void print_ref_list(int kinds, int verbose, int abbrev)
274 struct ref_list ref_list;
277 memset(&ref_list, 0, sizeof(ref_list));
278 ref_list.kinds = kinds;
279 for_each_ref(append_ref, &ref_list);
281 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
283 for (i = 0; i < ref_list.index; i++) {
284 switch( ref_list.list[i].kind ) {
285 case REF_LOCAL_BRANCH:
286 color = COLOR_BRANCH_LOCAL;
288 case REF_REMOTE_BRANCH:
289 color = COLOR_BRANCH_REMOTE;
292 color = COLOR_BRANCH_PLAIN;
297 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
298 !strcmp(ref_list.list[i].name, head)) {
300 color = COLOR_BRANCH_CURRENT;
304 printf("%c %s%-*s%s", c,
305 branch_get_color(color),
307 ref_list.list[i].name,
308 branch_get_color(COLOR_BRANCH_RESET));
309 print_ref_info(ref_list.list[i].sha1, abbrev);
312 printf("%c %s%s%s\n", c,
313 branch_get_color(color),
314 ref_list.list[i].name,
315 branch_get_color(COLOR_BRANCH_RESET));
318 free_ref_list(&ref_list);
321 static void create_branch(const char *name, const char *start,
322 int force, int reflog)
324 struct ref_lock *lock;
325 struct commit *commit;
326 unsigned char sha1[20];
327 char ref[PATH_MAX], msg[PATH_MAX + 20];
329 snprintf(ref, sizeof ref, "refs/heads/%s", name);
330 if (check_ref_format(ref))
331 die("'%s' is not a valid branch name.", name);
333 if (resolve_ref(ref, sha1, 1, NULL)) {
335 die("A branch named '%s' already exists.", name);
336 else if (!strcmp(head, name))
337 die("Cannot force update the current branch.");
340 if (get_sha1(start, sha1) ||
341 (commit = lookup_commit_reference(sha1)) == NULL)
342 die("Not a valid branch point: '%s'.", start);
343 hashcpy(sha1, commit->object.sha1);
345 lock = lock_any_ref_for_update(ref, NULL);
347 die("Failed to lock ref for update: %s.", strerror(errno));
350 log_all_ref_updates = 1;
351 snprintf(msg, sizeof msg, "branch: Created from %s", start);
354 if (write_ref_sha1(lock, sha1, msg) < 0)
355 die("Failed to write ref: %s.", strerror(errno));
358 static void rename_branch(const char *oldname, const char *newname, int force)
360 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
361 unsigned char sha1[20];
363 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
364 die("Old branchname too long");
366 if (check_ref_format(oldref))
367 die("Invalid branch name: %s", oldref);
369 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
370 die("New branchname too long");
372 if (check_ref_format(newref))
373 die("Invalid branch name: %s", newref);
375 if (resolve_ref(newref, sha1, 1, NULL) && !force)
376 die("A branch named '%s' already exists.", newname);
378 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
381 if (rename_ref(oldref, newref, logmsg))
382 die("Branch rename failed");
384 if (!strcmp(oldname, head) && create_symref("HEAD", newref))
385 die("Branch renamed to %s, but HEAD is not updated!", newname);
388 int cmd_branch(int argc, const char **argv, const char *prefix)
390 int delete = 0, force_delete = 0, force_create = 0;
391 int rename = 0, force_rename = 0;
392 int verbose = 0, abbrev = DEFAULT_ABBREV;
394 int kinds = REF_LOCAL_BRANCH;
398 git_config(git_branch_config);
400 for (i = 1; i < argc; i++) {
401 const char *arg = argv[i];
405 if (!strcmp(arg, "--")) {
409 if (!strcmp(arg, "-d")) {
413 if (!strcmp(arg, "-D")) {
418 if (!strcmp(arg, "-f")) {
422 if (!strcmp(arg, "-m")) {
426 if (!strcmp(arg, "-M")) {
431 if (!strcmp(arg, "-r")) {
432 kinds = REF_REMOTE_BRANCH;
435 if (!strcmp(arg, "-a")) {
436 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
439 if (!strcmp(arg, "-l")) {
443 if (!strncmp(arg, "--abbrev=", 9)) {
444 abbrev = atoi(arg+9);
447 if (!strcmp(arg, "-v")) {
451 if (!strcmp(arg, "--color")) {
452 branch_use_color = 1;
455 if (!strcmp(arg, "--no-color")) {
456 branch_use_color = 0;
459 usage(builtin_branch_usage);
462 if ((delete && rename) || (delete && force_create) ||
463 (rename && force_create))
464 usage(builtin_branch_usage);
466 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
468 die("Failed to resolve HEAD as a valid ref.");
469 if (strncmp(head, "refs/heads/", 11))
470 die("HEAD not found below refs/heads!");
474 return delete_branches(argc - i, argv + i, force_delete, kinds);
476 print_ref_list(kinds, verbose, abbrev);
477 else if (rename && (i == argc - 1))
478 rename_branch(head, argv[i], force_rename);
479 else if (rename && (i == argc - 2))
480 rename_branch(argv[i], argv[i + 1], force_rename);
481 else if (i == argc - 1)
482 create_branch(argv[i], head, force_create, reflog);
483 else if (i == argc - 2)
484 create_branch(argv[i], argv[i + 1], force_create, reflog);
486 usage(builtin_branch_usage);