]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-branch.c
branch --contains: default to HEAD
[thirdparty/git.git] / builtin-branch.c
CommitLineData
c31820c2
LH
1/*
2 * Builtin "git branch"
3 *
d972cce0 4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
c31820c2
LH
5 * Based on git-branch.sh by Junio C Hamano.
6 */
7
8#include "cache.h"
85023577 9#include "color.h"
c31820c2
LH
10#include "refs.h"
11#include "commit.h"
12#include "builtin.h"
6f084a56 13#include "remote.h"
a8dfd5ea 14#include "parse-options.h"
e496c003 15#include "branch.h"
a8dfd5ea
PH
16
17static const char * const builtin_branch_usage[] = {
e8b404c2 18 "git-branch [options] [-r | -a] [--merged | --no-merged]",
a8dfd5ea
PH
19 "git-branch [options] [-l] [-f] <branchname> [<start-point>]",
20 "git-branch [options] [-r] (-d | -D) <branchname>",
21 "git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
22 NULL
23};
c31820c2 24
f3d985c3
JH
25#define REF_UNKNOWN_TYPE 0x00
26#define REF_LOCAL_BRANCH 0x01
27#define REF_REMOTE_BRANCH 0x02
28#define REF_TAG 0x04
c31820c2
LH
29
30static const char *head;
31static unsigned char head_sha1[20];
32
6b2f2d98 33static int branch_use_color = -1;
a1158cae
AP
34static char branch_colors[][COLOR_MAXLEN] = {
35 "\033[m", /* reset */
36 "", /* PLAIN (normal) */
37 "\033[31m", /* REMOTE (red) */
753f96a4
JH
38 "", /* LOCAL (normal) */
39 "\033[32m", /* CURRENT (green) */
a1158cae
AP
40};
41enum color_branch {
42 COLOR_BRANCH_RESET = 0,
43 COLOR_BRANCH_PLAIN = 1,
44 COLOR_BRANCH_REMOTE = 2,
45 COLOR_BRANCH_LOCAL = 3,
46 COLOR_BRANCH_CURRENT = 4,
47};
48
e8b404c2
LH
49static int mergefilter = -1;
50
a1158cae
AP
51static int parse_branch_color_slot(const char *var, int ofs)
52{
53 if (!strcasecmp(var+ofs, "plain"))
54 return COLOR_BRANCH_PLAIN;
55 if (!strcasecmp(var+ofs, "reset"))
56 return COLOR_BRANCH_RESET;
57 if (!strcasecmp(var+ofs, "remote"))
58 return COLOR_BRANCH_REMOTE;
59 if (!strcasecmp(var+ofs, "local"))
60 return COLOR_BRANCH_LOCAL;
61 if (!strcasecmp(var+ofs, "current"))
62 return COLOR_BRANCH_CURRENT;
63 die("bad config variable '%s'", var);
64}
65
ef90d6d4 66static int git_branch_config(const char *var, const char *value, void *cb)
a1158cae
AP
67{
68 if (!strcmp(var, "color.branch")) {
0f6f5a40 69 branch_use_color = git_config_colorbool(var, value, -1);
a1158cae
AP
70 return 0;
71 }
cc44c765 72 if (!prefixcmp(var, "color.branch.")) {
a1158cae 73 int slot = parse_branch_color_slot(var, 13);
5768c98a
JH
74 if (!value)
75 return config_error_nonbool(var);
a1158cae
AP
76 color_parse(value, var, branch_colors[slot]);
77 return 0;
78 }
ef90d6d4 79 return git_color_default_config(var, value, cb);
a1158cae
AP
80}
81
52fae7de 82static const char *branch_get_color(enum color_branch ix)
a1158cae 83{
6b2f2d98 84 if (branch_use_color > 0)
a1158cae
AP
85 return branch_colors[ix];
86 return "";
87}
88
b8e9a00d 89static int delete_branches(int argc, const char **argv, int force, int kinds)
c31820c2 90{
67affd51 91 struct commit *rev, *head_rev = head_rev;
c31820c2 92 unsigned char sha1[20];
b8e9a00d 93 char *name = NULL;
f3d985c3 94 const char *fmt, *remote;
f1eccbab 95 char section[PATH_MAX];
c31820c2 96 int i;
b8e9a00d 97 int ret = 0;
c31820c2 98
f3d985c3
JH
99 switch (kinds) {
100 case REF_REMOTE_BRANCH:
101 fmt = "refs/remotes/%s";
102 remote = "remote ";
103 force = 1;
104 break;
105 case REF_LOCAL_BRANCH:
106 fmt = "refs/heads/%s";
107 remote = "";
108 break;
109 default:
110 die("cannot use -a with -d");
111 }
c31820c2 112
67affd51
JH
113 if (!force) {
114 head_rev = lookup_commit_reference(head_sha1);
115 if (!head_rev)
116 die("Couldn't look up commit object for HEAD");
117 }
c31820c2 118 for (i = 0; i < argc; i++) {
b8e9a00d
QT
119 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) {
120 error("Cannot delete the branch '%s' "
121 "which you are currently on.", argv[i]);
122 ret = 1;
123 continue;
124 }
125
8e0f7003 126 free(name);
c31820c2 127
f3d985c3 128 name = xstrdup(mkpath(fmt, argv[i]));
b8e9a00d
QT
129 if (!resolve_ref(name, sha1, 1, NULL)) {
130 error("%sbranch '%s' not found.",
131 remote, argv[i]);
132 ret = 1;
133 continue;
134 }
c31820c2
LH
135
136 rev = lookup_commit_reference(sha1);
b8e9a00d
QT
137 if (!rev) {
138 error("Couldn't look up commit object for '%s'", name);
139 ret = 1;
140 continue;
141 }
c31820c2
LH
142
143 /* This checks whether the merge bases of branch and
144 * HEAD contains branch -- which means that the HEAD
145 * contains everything in both.
146 */
147
148 if (!force &&
03840fc3 149 !in_merge_bases(rev, &head_rev, 1)) {
00ae8289 150 error("The branch '%s' is not an ancestor of "
b8e9a00d
QT
151 "your current HEAD.\n"
152 "If you are sure you want to delete it, "
153 "run 'git branch -D %s'.", argv[i], argv[i]);
154 ret = 1;
155 continue;
c31820c2
LH
156 }
157
b8e9a00d
QT
158 if (delete_ref(name, sha1)) {
159 error("Error deleting %sbranch '%s'", remote,
f3d985c3 160 argv[i]);
b8e9a00d 161 ret = 1;
f1eccbab 162 } else {
f3d985c3 163 printf("Deleted %sbranch %s.\n", remote, argv[i]);
f1eccbab
GP
164 snprintf(section, sizeof(section), "branch.%s",
165 argv[i]);
166 if (git_config_rename_section(section, NULL) < 0)
167 warning("Update of config-file failed");
168 }
c31820c2 169 }
c31820c2 170
8e0f7003 171 free(name);
b8e9a00d
QT
172
173 return(ret);
c31820c2 174}
c31820c2 175
bfcc9214
AP
176struct ref_item {
177 char *name;
178 unsigned int kind;
75e6e213 179 unsigned char sha1[20];
bfcc9214
AP
180};
181
182struct ref_list {
75e6e213 183 int index, alloc, maxwidth;
bfcc9214 184 struct ref_item *list;
694a5775 185 struct commit_list *with_commit;
bfcc9214
AP
186 int kinds;
187};
188
694a5775
JH
189static int has_commit(const unsigned char *sha1, struct commit_list *with_commit)
190{
191 struct commit *commit;
192
193 if (!with_commit)
194 return 1;
195 commit = lookup_commit_reference_gently(sha1, 1);
196 if (!commit)
197 return 0;
198 while (with_commit) {
199 struct commit *other;
200
201 other = with_commit->item;
202 with_commit = with_commit->next;
203 if (in_merge_bases(other, &commit, 1))
204 return 1;
205 }
206 return 0;
207}
208
bfcc9214 209static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
c31820c2 210{
bfcc9214
AP
211 struct ref_list *ref_list = (struct ref_list*)(cb_data);
212 struct ref_item *newitem;
213 int kind = REF_UNKNOWN_TYPE;
75e6e213 214 int len;
e8b404c2 215 static struct commit_list branch;
bfcc9214
AP
216
217 /* Detect kind */
cc44c765 218 if (!prefixcmp(refname, "refs/heads/")) {
bfcc9214
AP
219 kind = REF_LOCAL_BRANCH;
220 refname += 11;
cc44c765 221 } else if (!prefixcmp(refname, "refs/remotes/")) {
bfcc9214
AP
222 kind = REF_REMOTE_BRANCH;
223 refname += 13;
cc44c765 224 } else if (!prefixcmp(refname, "refs/tags/")) {
bfcc9214
AP
225 kind = REF_TAG;
226 refname += 10;
227 }
228
694a5775
JH
229 /* Filter with with_commit if specified */
230 if (!has_commit(sha1, ref_list->with_commit))
231 return 0;
232
bfcc9214
AP
233 /* Don't add types the caller doesn't want */
234 if ((kind & ref_list->kinds) == 0)
235 return 0;
236
e8b404c2
LH
237 if (mergefilter > -1) {
238 branch.item = lookup_commit_reference_gently(sha1, 1);
239 if (!branch.item)
240 die("Unable to lookup tip of branch %s", refname);
241 if (mergefilter == 0 && has_commit(head_sha1, &branch))
242 return 0;
243 if (mergefilter == 1 && !has_commit(head_sha1, &branch))
244 return 0;
245 }
246
bfcc9214
AP
247 /* Resize buffer */
248 if (ref_list->index >= ref_list->alloc) {
249 ref_list->alloc = alloc_nr(ref_list->alloc);
250 ref_list->list = xrealloc(ref_list->list,
251 ref_list->alloc * sizeof(struct ref_item));
c31820c2
LH
252 }
253
bfcc9214
AP
254 /* Record the new item */
255 newitem = &(ref_list->list[ref_list->index++]);
256 newitem->name = xstrdup(refname);
257 newitem->kind = kind;
75e6e213
LH
258 hashcpy(newitem->sha1, sha1);
259 len = strlen(newitem->name);
260 if (len > ref_list->maxwidth)
261 ref_list->maxwidth = len;
c31820c2
LH
262
263 return 0;
264}
265
bfcc9214
AP
266static void free_ref_list(struct ref_list *ref_list)
267{
268 int i;
269
270 for (i = 0; i < ref_list->index; i++)
271 free(ref_list->list[i].name);
272 free(ref_list->list);
273}
274
c31820c2
LH
275static int ref_cmp(const void *r1, const void *r2)
276{
bfcc9214
AP
277 struct ref_item *c1 = (struct ref_item *)(r1);
278 struct ref_item *c2 = (struct ref_item *)(r2);
279
280 if (c1->kind != c2->kind)
281 return c1->kind - c2->kind;
282 return strcmp(c1->name, c2->name);
c31820c2
LH
283}
284
af0e4ac0
LH
285static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
286 int abbrev, int current)
75e6e213 287{
af0e4ac0
LH
288 char c;
289 int color;
75e6e213 290 struct commit *commit;
75e6e213 291
af0e4ac0
LH
292 switch (item->kind) {
293 case REF_LOCAL_BRANCH:
294 color = COLOR_BRANCH_LOCAL;
295 break;
296 case REF_REMOTE_BRANCH:
297 color = COLOR_BRANCH_REMOTE;
298 break;
299 default:
300 color = COLOR_BRANCH_PLAIN;
301 break;
302 }
75e6e213 303
af0e4ac0
LH
304 c = ' ';
305 if (current) {
306 c = '*';
307 color = COLOR_BRANCH_CURRENT;
308 }
75e6e213 309
af0e4ac0 310 if (verbose) {
674d1727 311 struct strbuf subject;
80583c0e
JH
312 const char *sub = " **** invalid ref ****";
313
674d1727
PH
314 strbuf_init(&subject, 0);
315
af0e4ac0 316 commit = lookup_commit(item->sha1);
80583c0e 317 if (commit && !parse_commit(commit)) {
674d1727 318 pretty_print_commit(CMIT_FMT_ONELINE, commit,
4593fb84 319 &subject, 0, NULL, NULL, 0, 0);
674d1727 320 sub = subject.buf;
80583c0e 321 }
af0e4ac0
LH
322 printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color),
323 maxwidth, item->name,
324 branch_get_color(COLOR_BRANCH_RESET),
80583c0e 325 find_unique_abbrev(item->sha1, abbrev), sub);
674d1727 326 strbuf_release(&subject);
af0e4ac0
LH
327 } else {
328 printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
329 branch_get_color(COLOR_BRANCH_RESET));
330 }
75e6e213
LH
331}
332
694a5775 333static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
c31820c2
LH
334{
335 int i;
bfcc9214 336 struct ref_list ref_list;
c31820c2 337
bfcc9214
AP
338 memset(&ref_list, 0, sizeof(ref_list));
339 ref_list.kinds = kinds;
694a5775 340 ref_list.with_commit = with_commit;
bfcc9214 341 for_each_ref(append_ref, &ref_list);
c31820c2 342
bfcc9214 343 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
c31820c2 344
0016a482 345 detached = (detached && (kinds & REF_LOCAL_BRANCH));
694a5775 346 if (detached && has_commit(head_sha1, with_commit)) {
0016a482 347 struct ref_item item;
3a55602e 348 item.name = xstrdup("(no branch)");
0016a482
LH
349 item.kind = REF_LOCAL_BRANCH;
350 hashcpy(item.sha1, head_sha1);
351 if (strlen(item.name) > ref_list.maxwidth)
352 ref_list.maxwidth = strlen(item.name);
353 print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
3a55602e 354 free(item.name);
0016a482
LH
355 }
356
bfcc9214 357 for (i = 0; i < ref_list.index; i++) {
0016a482
LH
358 int current = !detached &&
359 (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
af0e4ac0
LH
360 !strcmp(ref_list.list[i].name, head);
361 print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
362 abbrev, current);
c31820c2 363 }
bfcc9214
AP
364
365 free_ref_list(&ref_list);
c31820c2
LH
366}
367
c976d415
LH
368static void rename_branch(const char *oldname, const char *newname, int force)
369{
678d0f4c 370 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
c976d415 371 unsigned char sha1[20];
19eba151 372 char oldsection[PATH_MAX], newsection[PATH_MAX];
c976d415 373
c847f537 374 if (!oldname)
3dff5379 375 die("cannot rename the current branch while not on any.");
c847f537 376
c976d415
LH
377 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
378 die("Old branchname too long");
379
380 if (check_ref_format(oldref))
381 die("Invalid branch name: %s", oldref);
382
383 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
384 die("New branchname too long");
385
386 if (check_ref_format(newref))
387 die("Invalid branch name: %s", newref);
388
389 if (resolve_ref(newref, sha1, 1, NULL) && !force)
390 die("A branch named '%s' already exists.", newname);
391
678d0f4c
LH
392 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
393 oldref, newref);
394
395 if (rename_ref(oldref, newref, logmsg))
c976d415
LH
396 die("Branch rename failed");
397
8b5157e4
NP
398 /* no need to pass logmsg here as HEAD didn't really move */
399 if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
c976d415 400 die("Branch renamed to %s, but HEAD is not updated!", newname);
19eba151
LH
401
402 snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
403 snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
404 if (git_config_rename_section(oldsection, newsection) < 0)
405 die("Branch is renamed, but update of config-file failed");
c976d415
LH
406}
407
694a5775
JH
408static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
409{
410 unsigned char sha1[20];
411 struct commit *commit;
412
413 if (!arg)
414 return -1;
415 if (get_sha1(arg, sha1))
416 die("malformed object name %s", arg);
417 commit = lookup_commit_reference(sha1);
418 if (!commit)
419 die("no such commit %s", arg);
420 commit_list_insert(commit, opt->value);
421 return 0;
422}
423
c31820c2
LH
424int cmd_branch(int argc, const char **argv, const char *prefix)
425{
d11d44fa 426 int delete = 0, rename = 0, force_create = 0;
0016a482 427 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
9ed36cfa
JS
428 int reflog = 0;
429 enum branch_track track;
d11d44fa 430 int kinds = REF_LOCAL_BRANCH;
694a5775 431 struct commit_list *with_commit = NULL;
a8dfd5ea
PH
432
433 struct option options[] = {
434 OPT_GROUP("Generic options"),
435 OPT__VERBOSE(&verbose),
9ed36cfa
JS
436 OPT_SET_INT( 0 , "track", &track, "set up tracking mode (see git-pull(1))",
437 BRANCH_TRACK_EXPLICIT),
a8dfd5ea 438 OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"),
d11d44fa
PH
439 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
440 REF_REMOTE_BRANCH),
e84fb2ff
JH
441 {
442 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
443 "print only branches that contain the commit",
444 PARSE_OPT_LASTARG_DEFAULT,
445 opt_parse_with_commit, (intptr_t)"HEAD",
446 },
694a5775
JH
447 {
448 OPTION_CALLBACK, 0, "with", &with_commit, "commit",
449 "print only branches that contain the commit",
e84fb2ff
JH
450 PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
451 opt_parse_with_commit, (intptr_t) "HEAD",
694a5775 452 },
a8dfd5ea
PH
453 OPT__ABBREV(&abbrev),
454
455 OPT_GROUP("Specific git-branch actions:"),
d11d44fa
PH
456 OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches",
457 REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
458 OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1),
459 OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
460 OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1),
461 OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
462 OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
463 OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"),
e8b404c2 464 OPT_SET_INT(0, "merged", &mergefilter, "list only merged branches", 1),
a8dfd5ea
PH
465 OPT_END(),
466 };
c31820c2 467
ef90d6d4 468 git_config(git_branch_config, NULL);
6b2f2d98
MK
469
470 if (branch_use_color == -1)
471 branch_use_color = git_use_color_default;
472
9ed36cfa 473 track = git_branch_track;
a8dfd5ea 474 argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
d11d44fa 475 if (!!delete + !!rename + !!force_create > 1)
a8dfd5ea 476 usage_with_options(builtin_branch_usage, options);
c976d415 477
078f8380 478 head = resolve_ref("HEAD", head_sha1, 0, NULL);
c31820c2
LH
479 if (!head)
480 die("Failed to resolve HEAD as a valid ref.");
078f8380 481 head = xstrdup(head);
0016a482
LH
482 if (!strcmp(head, "HEAD")) {
483 detached = 1;
a8dfd5ea 484 } else {
cc44c765 485 if (prefixcmp(head, "refs/heads/"))
0016a482
LH
486 die("HEAD not found below refs/heads!");
487 head += 11;
488 }
c31820c2
LH
489
490 if (delete)
d11d44fa 491 return delete_branches(argc, argv, delete > 1, kinds);
a8dfd5ea 492 else if (argc == 0)
694a5775 493 print_ref_list(kinds, detached, verbose, abbrev, with_commit);
a8dfd5ea 494 else if (rename && (argc == 1))
d11d44fa 495 rename_branch(head, argv[0], rename > 1);
a8dfd5ea 496 else if (rename && (argc == 2))
d11d44fa 497 rename_branch(argv[0], argv[1], rename > 1);
a8dfd5ea 498 else if (argc <= 2)
e496c003 499 create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
45994a1e 500 force_create, reflog, track);
c31820c2 501 else
a8dfd5ea 502 usage_with_options(builtin_branch_usage, options);
c31820c2
LH
503
504 return 0;
505}