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