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 (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [-r | -a] [-v [--abbrev=<length>]]";
18 static const char *head;
19 static unsigned char head_sha1[20];
21 static int branch_use_color;
22 static char branch_colors[][COLOR_MAXLEN] = {
24 "", /* PLAIN (normal) */
25 "\033[31m", /* REMOTE (red) */
26 "", /* LOCAL (normal) */
27 "\033[32m", /* CURRENT (green) */
30 COLOR_BRANCH_RESET = 0,
31 COLOR_BRANCH_PLAIN = 1,
32 COLOR_BRANCH_REMOTE = 2,
33 COLOR_BRANCH_LOCAL = 3,
34 COLOR_BRANCH_CURRENT = 4,
37 static int parse_branch_color_slot(const char *var, int ofs)
39 if (!strcasecmp(var+ofs, "plain"))
40 return COLOR_BRANCH_PLAIN;
41 if (!strcasecmp(var+ofs, "reset"))
42 return COLOR_BRANCH_RESET;
43 if (!strcasecmp(var+ofs, "remote"))
44 return COLOR_BRANCH_REMOTE;
45 if (!strcasecmp(var+ofs, "local"))
46 return COLOR_BRANCH_LOCAL;
47 if (!strcasecmp(var+ofs, "current"))
48 return COLOR_BRANCH_CURRENT;
49 die("bad config variable '%s'", var);
52 int git_branch_config(const char *var, const char *value)
54 if (!strcmp(var, "color.branch")) {
55 branch_use_color = git_config_colorbool(var, value);
58 if (!strncmp(var, "color.branch.", 13)) {
59 int slot = parse_branch_color_slot(var, 13);
60 color_parse(value, var, branch_colors[slot]);
63 return git_default_config(var, value);
66 const char *branch_get_color(enum color_branch ix)
69 return branch_colors[ix];
73 static int in_merge_bases(const unsigned char *sha1,
77 struct commit_list *bases, *b;
80 bases = get_merge_bases(rev1, rev2, 1);
81 for (b = bases; b; b = b->next) {
82 if (!hashcmp(sha1, b->item->object.sha1)) {
88 free_commit_list(bases);
92 static void delete_branches(int argc, const char **argv, int force)
94 struct commit *rev, *head_rev = head_rev;
95 unsigned char sha1[20];
100 head_rev = lookup_commit_reference(head_sha1);
102 die("Couldn't look up commit object for HEAD");
104 for (i = 0; i < argc; i++) {
105 if (!strcmp(head, argv[i]))
106 die("Cannot delete the branch you are currently on.");
108 name = xstrdup(mkpath("refs/heads/%s", argv[i]));
109 if (!resolve_ref(name, sha1, 1, NULL))
110 die("Branch '%s' not found.", argv[i]);
112 rev = lookup_commit_reference(sha1);
114 die("Couldn't look up commit object for '%s'", name);
116 /* This checks whether the merge bases of branch and
117 * HEAD contains branch -- which means that the HEAD
118 * contains everything in both.
122 !in_merge_bases(sha1, rev, head_rev)) {
124 "The branch '%s' is not a strict subset of your current HEAD.\n"
125 "If you are sure you want to delete it, run 'git branch -D %s'.\n",
130 if (delete_ref(name, sha1))
131 printf("Error deleting branch '%s'\n", argv[i]);
133 printf("Deleted branch %s.\n", argv[i]);
139 #define REF_UNKNOWN_TYPE 0x00
140 #define REF_LOCAL_BRANCH 0x01
141 #define REF_REMOTE_BRANCH 0x02
147 unsigned char sha1[20];
151 int index, alloc, maxwidth;
152 struct ref_item *list;
156 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
158 struct ref_list *ref_list = (struct ref_list*)(cb_data);
159 struct ref_item *newitem;
160 int kind = REF_UNKNOWN_TYPE;
164 if (!strncmp(refname, "refs/heads/", 11)) {
165 kind = REF_LOCAL_BRANCH;
167 } else if (!strncmp(refname, "refs/remotes/", 13)) {
168 kind = REF_REMOTE_BRANCH;
170 } else if (!strncmp(refname, "refs/tags/", 10)) {
175 /* Don't add types the caller doesn't want */
176 if ((kind & ref_list->kinds) == 0)
180 if (ref_list->index >= ref_list->alloc) {
181 ref_list->alloc = alloc_nr(ref_list->alloc);
182 ref_list->list = xrealloc(ref_list->list,
183 ref_list->alloc * sizeof(struct ref_item));
186 /* Record the new item */
187 newitem = &(ref_list->list[ref_list->index++]);
188 newitem->name = xstrdup(refname);
189 newitem->kind = kind;
190 hashcpy(newitem->sha1, sha1);
191 len = strlen(newitem->name);
192 if (len > ref_list->maxwidth)
193 ref_list->maxwidth = len;
198 static void free_ref_list(struct ref_list *ref_list)
202 for (i = 0; i < ref_list->index; i++)
203 free(ref_list->list[i].name);
204 free(ref_list->list);
207 static int ref_cmp(const void *r1, const void *r2)
209 struct ref_item *c1 = (struct ref_item *)(r1);
210 struct ref_item *c2 = (struct ref_item *)(r2);
212 if (c1->kind != c2->kind)
213 return c1->kind - c2->kind;
214 return strcmp(c1->name, c2->name);
217 static void print_ref_info(const unsigned char *sha1, int abbrev)
219 struct commit *commit;
223 commit = lookup_commit(sha1);
224 if (commit && !parse_commit(commit))
225 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
226 subject, sizeof(subject), 0,
229 strcpy(subject, " **** invalid ref ****");
231 printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
234 static void print_ref_list(int kinds, int verbose, int abbrev)
238 struct ref_list ref_list;
241 memset(&ref_list, 0, sizeof(ref_list));
242 ref_list.kinds = kinds;
243 for_each_ref(append_ref, &ref_list);
245 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
247 for (i = 0; i < ref_list.index; i++) {
248 switch( ref_list.list[i].kind ) {
249 case REF_LOCAL_BRANCH:
250 color = COLOR_BRANCH_LOCAL;
252 case REF_REMOTE_BRANCH:
253 color = COLOR_BRANCH_REMOTE;
256 color = COLOR_BRANCH_PLAIN;
261 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
262 !strcmp(ref_list.list[i].name, head)) {
264 color = COLOR_BRANCH_CURRENT;
268 printf("%c %s%-*s%s", c,
269 branch_get_color(color),
271 ref_list.list[i].name,
272 branch_get_color(COLOR_BRANCH_RESET));
273 print_ref_info(ref_list.list[i].sha1, abbrev);
276 printf("%c %s%s%s\n", c,
277 branch_get_color(color),
278 ref_list.list[i].name,
279 branch_get_color(COLOR_BRANCH_RESET));
282 free_ref_list(&ref_list);
285 static void create_branch(const char *name, const char *start,
286 int force, int reflog)
288 struct ref_lock *lock;
289 struct commit *commit;
290 unsigned char sha1[20];
291 char ref[PATH_MAX], msg[PATH_MAX + 20];
293 snprintf(ref, sizeof ref, "refs/heads/%s", name);
294 if (check_ref_format(ref))
295 die("'%s' is not a valid branch name.", name);
297 if (resolve_ref(ref, sha1, 1, NULL)) {
299 die("A branch named '%s' already exists.", name);
300 else if (!strcmp(head, name))
301 die("Cannot force update the current branch.");
304 if (get_sha1(start, sha1) ||
305 (commit = lookup_commit_reference(sha1)) == NULL)
306 die("Not a valid branch point: '%s'.", start);
307 hashcpy(sha1, commit->object.sha1);
309 lock = lock_any_ref_for_update(ref, NULL);
311 die("Failed to lock ref for update: %s.", strerror(errno));
314 log_all_ref_updates = 1;
315 snprintf(msg, sizeof msg, "branch: Created from %s", start);
318 if (write_ref_sha1(lock, sha1, msg) < 0)
319 die("Failed to write ref: %s.", strerror(errno));
322 static void rename_branch(const char *oldname, const char *newname, int force)
324 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
325 unsigned char sha1[20];
327 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
328 die("Old branchname too long");
330 if (check_ref_format(oldref))
331 die("Invalid branch name: %s", oldref);
333 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
334 die("New branchname too long");
336 if (check_ref_format(newref))
337 die("Invalid branch name: %s", newref);
339 if (resolve_ref(newref, sha1, 1, NULL) && !force)
340 die("A branch named '%s' already exists.", newname);
342 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
345 if (rename_ref(oldref, newref, logmsg))
346 die("Branch rename failed");
348 if (!strcmp(oldname, head) && create_symref("HEAD", newref))
349 die("Branch renamed to %s, but HEAD is not updated!", newname);
352 int cmd_branch(int argc, const char **argv, const char *prefix)
354 int delete = 0, force_delete = 0, force_create = 0;
355 int rename = 0, force_rename = 0;
356 int verbose = 0, abbrev = DEFAULT_ABBREV;
358 int kinds = REF_LOCAL_BRANCH;
362 git_config(git_branch_config);
364 for (i = 1; i < argc; i++) {
365 const char *arg = argv[i];
369 if (!strcmp(arg, "--")) {
373 if (!strcmp(arg, "-d")) {
377 if (!strcmp(arg, "-D")) {
382 if (!strcmp(arg, "-f")) {
386 if (!strcmp(arg, "-m")) {
390 if (!strcmp(arg, "-M")) {
395 if (!strcmp(arg, "-r")) {
396 kinds = REF_REMOTE_BRANCH;
399 if (!strcmp(arg, "-a")) {
400 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
403 if (!strcmp(arg, "-l")) {
407 if (!strncmp(arg, "--abbrev=", 9)) {
408 abbrev = atoi(arg+9);
411 if (!strcmp(arg, "-v")) {
415 if (!strcmp(arg, "--color")) {
416 branch_use_color = 1;
419 if (!strcmp(arg, "--no-color")) {
420 branch_use_color = 0;
423 usage(builtin_branch_usage);
426 if ((delete && rename) || (delete && force_create) ||
427 (rename && force_create))
428 usage(builtin_branch_usage);
430 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
432 die("Failed to resolve HEAD as a valid ref.");
433 if (strncmp(head, "refs/heads/", 11))
434 die("HEAD not found below refs/heads!");
438 delete_branches(argc - i, argv + i, force_delete);
440 print_ref_list(kinds, verbose, abbrev);
441 else if (rename && (i == argc - 1))
442 rename_branch(head, argv[i], force_rename);
443 else if (rename && (i == argc - 2))
444 rename_branch(argv[i], argv[i + 1], force_rename);
445 else if (i == argc - 1)
446 create_branch(argv[i], head, force_create, reflog);
447 else if (i == argc - 2)
448 create_branch(argv[i], argv[i + 1], force_create, reflog);
450 usage(builtin_branch_usage);