vim syntax: follow recent changes to commit template
[git/dscho.git] / builtin-branch.c
blob560309cb154504a8d280f12e3d9d2d3d6d715d7c
1 /*
2 * Builtin "git branch"
4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
6 */
8 #include "color.h"
9 #include "cache.h"
10 #include "refs.h"
11 #include "commit.h"
12 #include "builtin.h"
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] = {
23 "\033[m", /* reset */
24 "", /* PLAIN (normal) */
25 "\033[31m", /* REMOTE (red) */
26 "", /* LOCAL (normal) */
27 "\033[32m", /* CURRENT (green) */
29 enum color_branch {
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);
56 return 0;
58 if (!strncmp(var, "color.branch.", 13)) {
59 int slot = parse_branch_color_slot(var, 13);
60 color_parse(value, var, branch_colors[slot]);
61 return 0;
63 return git_default_config(var, value);
66 const char *branch_get_color(enum color_branch ix)
68 if (branch_use_color)
69 return branch_colors[ix];
70 return "";
73 static int in_merge_bases(const unsigned char *sha1,
74 struct commit *rev1,
75 struct commit *rev2)
77 struct commit_list *bases, *b;
78 int ret = 0;
80 bases = get_merge_bases(rev1, rev2, 1);
81 for (b = bases; b; b = b->next) {
82 if (!hashcmp(sha1, b->item->object.sha1)) {
83 ret = 1;
84 break;
88 free_commit_list(bases);
89 return ret;
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];
96 char *name;
97 int i;
99 if (!force) {
100 head_rev = lookup_commit_reference(head_sha1);
101 if (!head_rev)
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);
113 if (!rev)
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.
121 if (!force &&
122 !in_merge_bases(sha1, rev, head_rev)) {
123 fprintf(stderr,
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",
126 argv[i], argv[i]);
127 exit(1);
130 if (delete_ref(name, sha1))
131 printf("Error deleting branch '%s'\n", argv[i]);
132 else
133 printf("Deleted branch %s.\n", argv[i]);
135 free(name);
139 #define REF_UNKNOWN_TYPE 0x00
140 #define REF_LOCAL_BRANCH 0x01
141 #define REF_REMOTE_BRANCH 0x02
142 #define REF_TAG 0x04
144 struct ref_item {
145 char *name;
146 unsigned int kind;
147 unsigned char sha1[20];
150 struct ref_list {
151 int index, alloc, maxwidth;
152 struct ref_item *list;
153 int kinds;
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;
161 int len;
163 /* Detect kind */
164 if (!strncmp(refname, "refs/heads/", 11)) {
165 kind = REF_LOCAL_BRANCH;
166 refname += 11;
167 } else if (!strncmp(refname, "refs/remotes/", 13)) {
168 kind = REF_REMOTE_BRANCH;
169 refname += 13;
170 } else if (!strncmp(refname, "refs/tags/", 10)) {
171 kind = REF_TAG;
172 refname += 10;
175 /* Don't add types the caller doesn't want */
176 if ((kind & ref_list->kinds) == 0)
177 return 0;
179 /* Resize buffer */
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;
195 return 0;
198 static void free_ref_list(struct ref_list *ref_list)
200 int i;
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;
220 char subject[256];
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,
227 NULL, NULL, 0);
228 else
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)
236 int i;
237 char c;
238 struct ref_list ref_list;
239 int color;
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;
251 break;
252 case REF_REMOTE_BRANCH:
253 color = COLOR_BRANCH_REMOTE;
254 break;
255 default:
256 color = COLOR_BRANCH_PLAIN;
257 break;
260 c = ' ';
261 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
262 !strcmp(ref_list.list[i].name, head)) {
263 c = '*';
264 color = COLOR_BRANCH_CURRENT;
267 if (verbose) {
268 printf("%c %s%-*s%s", c,
269 branch_get_color(color),
270 ref_list.maxwidth,
271 ref_list.list[i].name,
272 branch_get_color(COLOR_BRANCH_RESET));
273 print_ref_info(ref_list.list[i].sha1, abbrev);
275 else
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)) {
298 if (!force)
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);
310 if (!lock)
311 die("Failed to lock ref for update: %s.", strerror(errno));
313 if (reflog) {
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",
343 oldref, newref);
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;
357 int reflog = 0;
358 int kinds = REF_LOCAL_BRANCH;
359 int i;
361 setup_ident();
362 git_config(git_branch_config);
364 for (i = 1; i < argc; i++) {
365 const char *arg = argv[i];
367 if (arg[0] != '-')
368 break;
369 if (!strcmp(arg, "--")) {
370 i++;
371 break;
373 if (!strcmp(arg, "-d")) {
374 delete = 1;
375 continue;
377 if (!strcmp(arg, "-D")) {
378 delete = 1;
379 force_delete = 1;
380 continue;
382 if (!strcmp(arg, "-f")) {
383 force_create = 1;
384 continue;
386 if (!strcmp(arg, "-m")) {
387 rename = 1;
388 continue;
390 if (!strcmp(arg, "-M")) {
391 rename = 1;
392 force_rename = 1;
393 continue;
395 if (!strcmp(arg, "-r")) {
396 kinds = REF_REMOTE_BRANCH;
397 continue;
399 if (!strcmp(arg, "-a")) {
400 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
401 continue;
403 if (!strcmp(arg, "-l")) {
404 reflog = 1;
405 continue;
407 if (!strncmp(arg, "--abbrev=", 9)) {
408 abbrev = atoi(arg+9);
409 continue;
411 if (!strcmp(arg, "-v")) {
412 verbose = 1;
413 continue;
415 if (!strcmp(arg, "--color")) {
416 branch_use_color = 1;
417 continue;
419 if (!strcmp(arg, "--no-color")) {
420 branch_use_color = 0;
421 continue;
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));
431 if (!head)
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!");
435 head += 11;
437 if (delete)
438 delete_branches(argc - i, argv + i, force_delete);
439 else if (i == argc)
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);
449 else
450 usage(builtin_branch_usage);
452 return 0;