Move create_branch into a library file
[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 #include "remote.h"
14 #include "parse-options.h"
15 #include "branch.h"
16
17 static 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 };
24
25 #define REF_UNKNOWN_TYPE 0x00
26 #define REF_LOCAL_BRANCH 0x01
27 #define REF_REMOTE_BRANCH 0x02
28 #define REF_TAG 0x04
29
30 static const char *head;
31 static unsigned char head_sha1[20];
32
33 static int branch_track = 1;
34
35 static int branch_use_color;
36 static char branch_colors[][COLOR_MAXLEN] = {
37 "\033[m", /* reset */
38 "", /* PLAIN (normal) */
39 "\033[31m", /* REMOTE (red) */
40 "", /* LOCAL (normal) */
41 "\033[32m", /* CURRENT (green) */
42 };
43 enum 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
51 static 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
66 static int git_branch_config(const char *var, const char *value)
67 {
68 if (!strcmp(var, "color.branch")) {
69 branch_use_color = git_config_colorbool(var, value, -1);
70 return 0;
71 }
72 if (!prefixcmp(var, "color.branch.")) {
73 int slot = parse_branch_color_slot(var, 13);
74 color_parse(value, var, branch_colors[slot]);
75 return 0;
76 }
77 if (!strcmp(var, "branch.autosetupmerge"))
78 branch_track = git_config_bool(var, value);
79
80 return git_default_config(var, value);
81 }
82
83 static const char *branch_get_color(enum color_branch ix)
84 {
85 if (branch_use_color)
86 return branch_colors[ix];
87 return "";
88 }
89
90 static int delete_branches(int argc, const char **argv, int force, int kinds)
91 {
92 struct commit *rev, *head_rev = head_rev;
93 unsigned char sha1[20];
94 char *name = NULL;
95 const char *fmt, *remote;
96 char section[PATH_MAX];
97 int i;
98 int ret = 0;
99
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 }
113
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 }
119 for (i = 0; i < argc; i++) {
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);
129
130 name = xstrdup(mkpath(fmt, argv[i]));
131 if (!resolve_ref(name, sha1, 1, NULL)) {
132 error("%sbranch '%s' not found.",
133 remote, argv[i]);
134 ret = 1;
135 continue;
136 }
137
138 rev = lookup_commit_reference(sha1);
139 if (!rev) {
140 error("Couldn't look up commit object for '%s'", name);
141 ret = 1;
142 continue;
143 }
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 &&
151 !in_merge_bases(rev, &head_rev, 1)) {
152 error("The branch '%s' is not an ancestor of "
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;
158 }
159
160 if (delete_ref(name, sha1)) {
161 error("Error deleting %sbranch '%s'", remote,
162 argv[i]);
163 ret = 1;
164 } else {
165 printf("Deleted %sbranch %s.\n", remote, argv[i]);
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 }
171 }
172
173 if (name)
174 free(name);
175
176 return(ret);
177 }
178
179 struct ref_item {
180 char *name;
181 unsigned int kind;
182 unsigned char sha1[20];
183 };
184
185 struct ref_list {
186 int index, alloc, maxwidth;
187 struct ref_item *list;
188 struct commit_list *with_commit;
189 int kinds;
190 };
191
192 static 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
212 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
213 {
214 struct ref_list *ref_list = (struct ref_list*)(cb_data);
215 struct ref_item *newitem;
216 int kind = REF_UNKNOWN_TYPE;
217 int len;
218
219 /* Detect kind */
220 if (!prefixcmp(refname, "refs/heads/")) {
221 kind = REF_LOCAL_BRANCH;
222 refname += 11;
223 } else if (!prefixcmp(refname, "refs/remotes/")) {
224 kind = REF_REMOTE_BRANCH;
225 refname += 13;
226 } else if (!prefixcmp(refname, "refs/tags/")) {
227 kind = REF_TAG;
228 refname += 10;
229 }
230
231 /* Filter with with_commit if specified */
232 if (!has_commit(sha1, ref_list->with_commit))
233 return 0;
234
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));
244 }
245
246 /* Record the new item */
247 newitem = &(ref_list->list[ref_list->index++]);
248 newitem->name = xstrdup(refname);
249 newitem->kind = kind;
250 hashcpy(newitem->sha1, sha1);
251 len = strlen(newitem->name);
252 if (len > ref_list->maxwidth)
253 ref_list->maxwidth = len;
254
255 return 0;
256 }
257
258 static 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
267 static int ref_cmp(const void *r1, const void *r2)
268 {
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);
275 }
276
277 static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
278 int abbrev, int current)
279 {
280 char c;
281 int color;
282 struct commit *commit;
283
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 }
295
296 c = ' ';
297 if (current) {
298 c = '*';
299 color = COLOR_BRANCH_CURRENT;
300 }
301
302 if (verbose) {
303 struct strbuf subject;
304 const char *sub = " **** invalid ref ****";
305
306 strbuf_init(&subject, 0);
307
308 commit = lookup_commit(item->sha1);
309 if (commit && !parse_commit(commit)) {
310 pretty_print_commit(CMIT_FMT_ONELINE, commit,
311 &subject, 0, NULL, NULL, 0, 0);
312 sub = subject.buf;
313 }
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),
317 find_unique_abbrev(item->sha1, abbrev), sub);
318 strbuf_release(&subject);
319 } else {
320 printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
321 branch_get_color(COLOR_BRANCH_RESET));
322 }
323 }
324
325 static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
326 {
327 int i;
328 struct ref_list ref_list;
329
330 memset(&ref_list, 0, sizeof(ref_list));
331 ref_list.kinds = kinds;
332 ref_list.with_commit = with_commit;
333 for_each_ref(append_ref, &ref_list);
334
335 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
336
337 detached = (detached && (kinds & REF_LOCAL_BRANCH));
338 if (detached && has_commit(head_sha1, with_commit)) {
339 struct ref_item item;
340 item.name = xstrdup("(no branch)");
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);
346 free(item.name);
347 }
348
349 for (i = 0; i < ref_list.index; i++) {
350 int current = !detached &&
351 (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
352 !strcmp(ref_list.list[i].name, head);
353 print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
354 abbrev, current);
355 }
356
357 free_ref_list(&ref_list);
358 }
359
360 static void rename_branch(const char *oldname, const char *newname, int force)
361 {
362 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
363 unsigned char sha1[20];
364 char oldsection[PATH_MAX], newsection[PATH_MAX];
365
366 if (!oldname)
367 die("cannot rename the current branch while not on any.");
368
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
384 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
385 oldref, newref);
386
387 if (rename_ref(oldref, newref, logmsg))
388 die("Branch rename failed");
389
390 /* no need to pass logmsg here as HEAD didn't really move */
391 if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
392 die("Branch renamed to %s, but HEAD is not updated!", newname);
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");
398 }
399
400 static 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
416 int cmd_branch(int argc, const char **argv, const char *prefix)
417 {
418 int delete = 0, rename = 0, force_create = 0;
419 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
420 int reflog = 0, track;
421 int kinds = REF_LOCAL_BRANCH;
422 struct commit_list *with_commit = NULL;
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"),
429 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
430 REF_REMOTE_BRANCH),
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 },
439 OPT__ABBREV(&abbrev),
440
441 OPT_GROUP("Specific git-branch actions:"),
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)"),
450 OPT_END(),
451 };
452
453 git_config(git_branch_config);
454 track = branch_track;
455 argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
456 if (!!delete + !!rename + !!force_create > 1)
457 usage_with_options(builtin_branch_usage, options);
458
459 head = resolve_ref("HEAD", head_sha1, 0, NULL);
460 if (!head)
461 die("Failed to resolve HEAD as a valid ref.");
462 head = xstrdup(head);
463 if (!strcmp(head, "HEAD")) {
464 detached = 1;
465 } else {
466 if (prefixcmp(head, "refs/heads/"))
467 die("HEAD not found below refs/heads!");
468 head += 11;
469 }
470
471 if (delete)
472 return delete_branches(argc, argv, delete > 1, kinds);
473 else if (argc == 0)
474 print_ref_list(kinds, detached, verbose, abbrev, with_commit);
475 else if (rename && (argc == 1))
476 rename_branch(head, argv[0], rename > 1);
477 else if (rename && (argc == 2))
478 rename_branch(argv[0], argv[1], rename > 1);
479 else if (argc <= 2)
480 create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
481 force_create, reflog, track);
482 else
483 usage_with_options(builtin_branch_usage, options);
484
485 return 0;
486 }