Merge branch 'maint'
[git/git.git] / builtin-branch.c
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 "cache.h"
9 #include "color.h"
10 #include "refs.h"
11 #include "commit.h"
12 #include "builtin.h"
13
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>]]";
16
17
18 static const char *head;
19 static unsigned char head_sha1[20];
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) */
28 };
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,
35 };
36
37 static int parse_branch_color_slot(const char *var, int ofs)
38 {
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);
50 }
51
52 int git_branch_config(const char *var, const char *value)
53 {
54 if (!strcmp(var, "color.branch")) {
55 branch_use_color = git_config_colorbool(var, value);
56 return 0;
57 }
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;
62 }
63 return git_default_config(var, value);
64 }
65
66 const char *branch_get_color(enum color_branch ix)
67 {
68 if (branch_use_color)
69 return branch_colors[ix];
70 return "";
71 }
72
73 static int in_merge_bases(const unsigned char *sha1,
74 struct commit *rev1,
75 struct commit *rev2)
76 {
77 struct commit_list *bases, *b;
78 int ret = 0;
79
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;
85 }
86 }
87
88 free_commit_list(bases);
89 return ret;
90 }
91
92 static void delete_branches(int argc, const char **argv, int force)
93 {
94 struct commit *rev, *head_rev = head_rev;
95 unsigned char sha1[20];
96 char *name;
97 int i;
98
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");
103 }
104 for (i = 0; i < argc; i++) {
105 if (!strcmp(head, argv[i]))
106 die("Cannot delete the branch you are currently on.");
107
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]);
111
112 rev = lookup_commit_reference(sha1);
113 if (!rev)
114 die("Couldn't look up commit object for '%s'", name);
115
116 /* This checks whether the merge bases of branch and
117 * HEAD contains branch -- which means that the HEAD
118 * contains everything in both.
119 */
120
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);
128 }
129
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]);
134
135 free(name);
136 }
137 }
138
139 #define REF_UNKNOWN_TYPE 0x00
140 #define REF_LOCAL_BRANCH 0x01
141 #define REF_REMOTE_BRANCH 0x02
142 #define REF_TAG 0x04
143
144 struct ref_item {
145 char *name;
146 unsigned int kind;
147 unsigned char sha1[20];
148 };
149
150 struct ref_list {
151 int index, alloc, maxwidth;
152 struct ref_item *list;
153 int kinds;
154 };
155
156 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
157 {
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;
162
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;
173 }
174
175 /* Don't add types the caller doesn't want */
176 if ((kind & ref_list->kinds) == 0)
177 return 0;
178
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));
184 }
185
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;
194
195 return 0;
196 }
197
198 static void free_ref_list(struct ref_list *ref_list)
199 {
200 int i;
201
202 for (i = 0; i < ref_list->index; i++)
203 free(ref_list->list[i].name);
204 free(ref_list->list);
205 }
206
207 static int ref_cmp(const void *r1, const void *r2)
208 {
209 struct ref_item *c1 = (struct ref_item *)(r1);
210 struct ref_item *c2 = (struct ref_item *)(r2);
211
212 if (c1->kind != c2->kind)
213 return c1->kind - c2->kind;
214 return strcmp(c1->name, c2->name);
215 }
216
217 static void print_ref_info(const unsigned char *sha1, int abbrev)
218 {
219 struct commit *commit;
220 char subject[256];
221
222
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 ****");
230
231 printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
232 }
233
234 static void print_ref_list(int kinds, int verbose, int abbrev)
235 {
236 int i;
237 char c;
238 struct ref_list ref_list;
239 int color;
240
241 memset(&ref_list, 0, sizeof(ref_list));
242 ref_list.kinds = kinds;
243 for_each_ref(append_ref, &ref_list);
244
245 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
246
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;
258 }
259
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;
265 }
266
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);
274 }
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));
280 }
281
282 free_ref_list(&ref_list);
283 }
284
285 static void create_branch(const char *name, const char *start,
286 int force, int reflog)
287 {
288 struct ref_lock *lock;
289 struct commit *commit;
290 unsigned char sha1[20];
291 char ref[PATH_MAX], msg[PATH_MAX + 20];
292
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);
296
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.");
302 }
303
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);
308
309 lock = lock_any_ref_for_update(ref, NULL);
310 if (!lock)
311 die("Failed to lock ref for update: %s.", strerror(errno));
312
313 if (reflog) {
314 log_all_ref_updates = 1;
315 snprintf(msg, sizeof msg, "branch: Created from %s", start);
316 }
317
318 if (write_ref_sha1(lock, sha1, msg) < 0)
319 die("Failed to write ref: %s.", strerror(errno));
320 }
321
322 static void rename_branch(const char *oldname, const char *newname, int force)
323 {
324 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
325 unsigned char sha1[20];
326
327 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
328 die("Old branchname too long");
329
330 if (check_ref_format(oldref))
331 die("Invalid branch name: %s", oldref);
332
333 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
334 die("New branchname too long");
335
336 if (check_ref_format(newref))
337 die("Invalid branch name: %s", newref);
338
339 if (resolve_ref(newref, sha1, 1, NULL) && !force)
340 die("A branch named '%s' already exists.", newname);
341
342 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
343 oldref, newref);
344
345 if (rename_ref(oldref, newref, logmsg))
346 die("Branch rename failed");
347
348 if (!strcmp(oldname, head) && create_symref("HEAD", newref))
349 die("Branch renamed to %s, but HEAD is not updated!", newname);
350 }
351
352 int cmd_branch(int argc, const char **argv, const char *prefix)
353 {
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;
360
361 setup_ident();
362 git_config(git_branch_config);
363
364 for (i = 1; i < argc; i++) {
365 const char *arg = argv[i];
366
367 if (arg[0] != '-')
368 break;
369 if (!strcmp(arg, "--")) {
370 i++;
371 break;
372 }
373 if (!strcmp(arg, "-d")) {
374 delete = 1;
375 continue;
376 }
377 if (!strcmp(arg, "-D")) {
378 delete = 1;
379 force_delete = 1;
380 continue;
381 }
382 if (!strcmp(arg, "-f")) {
383 force_create = 1;
384 continue;
385 }
386 if (!strcmp(arg, "-m")) {
387 rename = 1;
388 continue;
389 }
390 if (!strcmp(arg, "-M")) {
391 rename = 1;
392 force_rename = 1;
393 continue;
394 }
395 if (!strcmp(arg, "-r")) {
396 kinds = REF_REMOTE_BRANCH;
397 continue;
398 }
399 if (!strcmp(arg, "-a")) {
400 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
401 continue;
402 }
403 if (!strcmp(arg, "-l")) {
404 reflog = 1;
405 continue;
406 }
407 if (!strncmp(arg, "--abbrev=", 9)) {
408 abbrev = atoi(arg+9);
409 continue;
410 }
411 if (!strcmp(arg, "-v")) {
412 verbose = 1;
413 continue;
414 }
415 if (!strcmp(arg, "--color")) {
416 branch_use_color = 1;
417 continue;
418 }
419 if (!strcmp(arg, "--no-color")) {
420 branch_use_color = 0;
421 continue;
422 }
423 usage(builtin_branch_usage);
424 }
425
426 if ((delete && rename) || (delete && force_create) ||
427 (rename && force_create))
428 usage(builtin_branch_usage);
429
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;
436
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);
451
452 return 0;
453 }