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