gitweb: optimize git_get_last_activity.
[git/mingw.git] / builtin-branch.c
blob903d5cf05600f9a7e297dc9450ad5980388072e9
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 "cache.h"
9 #include "color.h"
10 #include "refs.h"
11 #include "commit.h"
12 #include "builtin.h"
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
20 #define REF_TAG 0x04
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] = {
27 "\033[m", /* reset */
28 "", /* PLAIN (normal) */
29 "\033[31m", /* REMOTE (red) */
30 "", /* LOCAL (normal) */
31 "\033[32m", /* CURRENT (green) */
33 enum 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,
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);
60 return 0;
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;
67 return git_default_config(var, value);
70 const char *branch_get_color(enum color_branch ix)
72 if (branch_use_color)
73 return branch_colors[ix];
74 return "";
77 static int in_merge_bases(const unsigned char *sha1,
78 struct commit *rev1,
79 struct commit *rev2)
81 struct commit_list *bases, *b;
82 int ret = 0;
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;
92 free_commit_list(bases);
93 return ret;
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];
100 char *name = NULL;
101 const char *fmt, *remote;
102 int i;
103 int ret = 0;
105 switch (kinds) {
106 case REF_REMOTE_BRANCH:
107 fmt = "refs/remotes/%s";
108 remote = "remote ";
109 force = 1;
110 break;
111 case REF_LOCAL_BRANCH:
112 fmt = "refs/heads/%s";
113 remote = "";
114 break;
115 default:
116 die("cannot use -a with -d");
119 if (!force) {
120 head_rev = lookup_commit_reference(head_sha1);
121 if (!head_rev)
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]);
128 ret = 1;
129 continue;
132 if (name)
133 free(name);
135 name = xstrdup(mkpath(fmt, argv[i]));
136 if (!resolve_ref(name, sha1, 1, NULL)) {
137 error("%sbranch '%s' not found.",
138 remote, argv[i]);
139 ret = 1;
140 continue;
143 rev = lookup_commit_reference(sha1);
144 if (!rev) {
145 error("Couldn't look up commit object for '%s'", name);
146 ret = 1;
147 continue;
150 /* This checks whether the merge bases of branch and
151 * HEAD contains branch -- which means that the HEAD
152 * contains everything in both.
155 if (!force &&
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]);
161 ret = 1;
162 continue;
165 if (delete_ref(name, sha1)) {
166 error("Error deleting %sbranch '%s'", remote,
167 argv[i]);
168 ret = 1;
169 } else
170 printf("Deleted %sbranch %s.\n", remote, argv[i]);
174 if (name)
175 free(name);
177 return(ret);
180 struct ref_item {
181 char *name;
182 unsigned int kind;
183 unsigned char sha1[20];
186 struct ref_list {
187 int index, alloc, maxwidth;
188 struct ref_item *list;
189 int kinds;
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;
197 int len;
199 /* Detect kind */
200 if (!strncmp(refname, "refs/heads/", 11)) {
201 kind = REF_LOCAL_BRANCH;
202 refname += 11;
203 } else if (!strncmp(refname, "refs/remotes/", 13)) {
204 kind = REF_REMOTE_BRANCH;
205 refname += 13;
206 } else if (!strncmp(refname, "refs/tags/", 10)) {
207 kind = REF_TAG;
208 refname += 10;
211 /* Don't add types the caller doesn't want */
212 if ((kind & ref_list->kinds) == 0)
213 return 0;
215 /* Resize buffer */
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;
231 return 0;
234 static void free_ref_list(struct ref_list *ref_list)
236 int i;
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;
256 char subject[256];
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,
263 NULL, NULL, 0);
264 else
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)
272 int i;
273 char c;
274 struct ref_list ref_list;
275 int color;
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;
287 break;
288 case REF_REMOTE_BRANCH:
289 color = COLOR_BRANCH_REMOTE;
290 break;
291 default:
292 color = COLOR_BRANCH_PLAIN;
293 break;
296 c = ' ';
297 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
298 !strcmp(ref_list.list[i].name, head)) {
299 c = '*';
300 color = COLOR_BRANCH_CURRENT;
303 if (verbose) {
304 printf("%c %s%-*s%s", c,
305 branch_get_color(color),
306 ref_list.maxwidth,
307 ref_list.list[i].name,
308 branch_get_color(COLOR_BRANCH_RESET));
309 print_ref_info(ref_list.list[i].sha1, abbrev);
311 else
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)) {
334 if (!force)
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);
346 if (!lock)
347 die("Failed to lock ref for update: %s.", strerror(errno));
349 if (reflog) {
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",
379 oldref, newref);
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;
393 int reflog = 0;
394 int kinds = REF_LOCAL_BRANCH;
395 int i;
397 setup_ident();
398 git_config(git_branch_config);
400 for (i = 1; i < argc; i++) {
401 const char *arg = argv[i];
403 if (arg[0] != '-')
404 break;
405 if (!strcmp(arg, "--")) {
406 i++;
407 break;
409 if (!strcmp(arg, "-d")) {
410 delete = 1;
411 continue;
413 if (!strcmp(arg, "-D")) {
414 delete = 1;
415 force_delete = 1;
416 continue;
418 if (!strcmp(arg, "-f")) {
419 force_create = 1;
420 continue;
422 if (!strcmp(arg, "-m")) {
423 rename = 1;
424 continue;
426 if (!strcmp(arg, "-M")) {
427 rename = 1;
428 force_rename = 1;
429 continue;
431 if (!strcmp(arg, "-r")) {
432 kinds = REF_REMOTE_BRANCH;
433 continue;
435 if (!strcmp(arg, "-a")) {
436 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
437 continue;
439 if (!strcmp(arg, "-l")) {
440 reflog = 1;
441 continue;
443 if (!strncmp(arg, "--abbrev=", 9)) {
444 abbrev = atoi(arg+9);
445 continue;
447 if (!strcmp(arg, "-v")) {
448 verbose = 1;
449 continue;
451 if (!strcmp(arg, "--color")) {
452 branch_use_color = 1;
453 continue;
455 if (!strcmp(arg, "--no-color")) {
456 branch_use_color = 0;
457 continue;
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));
467 if (!head)
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!");
471 head += 11;
473 if (delete)
474 return delete_branches(argc - i, argv + i, force_delete, kinds);
475 else if (i == argc)
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);
485 else
486 usage(builtin_branch_usage);
488 return 0;