4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
14 #include "parse-options.h"
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>",
25 #define REF_UNKNOWN_TYPE 0x00
26 #define REF_LOCAL_BRANCH 0x01
27 #define REF_REMOTE_BRANCH 0x02
30 static const char *head
;
31 static unsigned char head_sha1
[20];
33 static int branch_use_color
;
34 static char branch_colors
[][COLOR_MAXLEN
] = {
36 "", /* PLAIN (normal) */
37 "\033[31m", /* REMOTE (red) */
38 "", /* LOCAL (normal) */
39 "\033[32m", /* CURRENT (green) */
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,
49 static int parse_branch_color_slot(const char *var
, int ofs
)
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
);
64 static int git_branch_config(const char *var
, const char *value
)
66 if (!strcmp(var
, "color.branch")) {
67 branch_use_color
= git_config_colorbool(var
, value
, -1);
70 if (!prefixcmp(var
, "color.branch.")) {
71 int slot
= parse_branch_color_slot(var
, 13);
72 color_parse(value
, var
, branch_colors
[slot
]);
75 return git_default_config(var
, value
);
78 static const char *branch_get_color(enum color_branch ix
)
81 return branch_colors
[ix
];
85 static int delete_branches(int argc
, const char **argv
, int force
, int kinds
)
87 struct commit
*rev
, *head_rev
= head_rev
;
88 unsigned char sha1
[20];
90 const char *fmt
, *remote
;
91 char section
[PATH_MAX
];
96 case REF_REMOTE_BRANCH
:
97 fmt
= "refs/remotes/%s";
101 case REF_LOCAL_BRANCH
:
102 fmt
= "refs/heads/%s";
106 die("cannot use -a with -d");
110 head_rev
= lookup_commit_reference(head_sha1
);
112 die("Couldn't look up commit object for HEAD");
114 for (i
= 0; i
< argc
; i
++) {
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
]);
125 name
= xstrdup(mkpath(fmt
, argv
[i
]));
126 if (!resolve_ref(name
, sha1
, 1, NULL
)) {
127 error("%sbranch '%s' not found.",
133 rev
= lookup_commit_reference(sha1
);
135 error("Couldn't look up commit object for '%s'", name
);
140 /* This checks whether the merge bases of branch and
141 * HEAD contains branch -- which means that the HEAD
142 * contains everything in both.
146 !in_merge_bases(rev
, &head_rev
, 1)) {
147 error("The branch '%s' is not an ancestor of "
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
]);
155 if (delete_ref(name
, sha1
)) {
156 error("Error deleting %sbranch '%s'", remote
,
160 printf("Deleted %sbranch %s.\n", remote
, argv
[i
]);
161 snprintf(section
, sizeof(section
), "branch.%s",
163 if (git_config_rename_section(section
, NULL
) < 0)
164 warning("Update of config-file failed");
177 unsigned char sha1
[20];
181 int index
, alloc
, maxwidth
;
182 struct ref_item
*list
;
183 struct commit_list
*with_commit
;
187 static int has_commit(const unsigned char *sha1
, struct commit_list
*with_commit
)
189 struct commit
*commit
;
193 commit
= lookup_commit_reference_gently(sha1
, 1);
196 while (with_commit
) {
197 struct commit
*other
;
199 other
= with_commit
->item
;
200 with_commit
= with_commit
->next
;
201 if (in_merge_bases(other
, &commit
, 1))
207 static int append_ref(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
209 struct ref_list
*ref_list
= (struct ref_list
*)(cb_data
);
210 struct ref_item
*newitem
;
211 int kind
= REF_UNKNOWN_TYPE
;
215 if (!prefixcmp(refname
, "refs/heads/")) {
216 kind
= REF_LOCAL_BRANCH
;
218 } else if (!prefixcmp(refname
, "refs/remotes/")) {
219 kind
= REF_REMOTE_BRANCH
;
221 } else if (!prefixcmp(refname
, "refs/tags/")) {
226 /* Filter with with_commit if specified */
227 if (!has_commit(sha1
, ref_list
->with_commit
))
230 /* Don't add types the caller doesn't want */
231 if ((kind
& ref_list
->kinds
) == 0)
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
));
241 /* Record the new item */
242 newitem
= &(ref_list
->list
[ref_list
->index
++]);
243 newitem
->name
= xstrdup(refname
);
244 newitem
->kind
= kind
;
245 hashcpy(newitem
->sha1
, sha1
);
246 len
= strlen(newitem
->name
);
247 if (len
> ref_list
->maxwidth
)
248 ref_list
->maxwidth
= len
;
253 static void free_ref_list(struct ref_list
*ref_list
)
257 for (i
= 0; i
< ref_list
->index
; i
++)
258 free(ref_list
->list
[i
].name
);
259 free(ref_list
->list
);
262 static int ref_cmp(const void *r1
, const void *r2
)
264 struct ref_item
*c1
= (struct ref_item
*)(r1
);
265 struct ref_item
*c2
= (struct ref_item
*)(r2
);
267 if (c1
->kind
!= c2
->kind
)
268 return c1
->kind
- c2
->kind
;
269 return strcmp(c1
->name
, c2
->name
);
272 static void print_ref_item(struct ref_item
*item
, int maxwidth
, int verbose
,
273 int abbrev
, int current
)
277 struct commit
*commit
;
279 switch (item
->kind
) {
280 case REF_LOCAL_BRANCH
:
281 color
= COLOR_BRANCH_LOCAL
;
283 case REF_REMOTE_BRANCH
:
284 color
= COLOR_BRANCH_REMOTE
;
287 color
= COLOR_BRANCH_PLAIN
;
294 color
= COLOR_BRANCH_CURRENT
;
298 struct strbuf subject
;
299 const char *sub
= " **** invalid ref ****";
301 strbuf_init(&subject
, 0);
303 commit
= lookup_commit(item
->sha1
);
304 if (commit
&& !parse_commit(commit
)) {
305 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
306 &subject
, 0, NULL
, NULL
, 0, 0);
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
),
312 find_unique_abbrev(item
->sha1
, abbrev
), sub
);
313 strbuf_release(&subject
);
315 printf("%c %s%s%s\n", c
, branch_get_color(color
), item
->name
,
316 branch_get_color(COLOR_BRANCH_RESET
));
320 static void print_ref_list(int kinds
, int detached
, int verbose
, int abbrev
, struct commit_list
*with_commit
)
323 struct ref_list ref_list
;
325 memset(&ref_list
, 0, sizeof(ref_list
));
326 ref_list
.kinds
= kinds
;
327 ref_list
.with_commit
= with_commit
;
328 for_each_ref(append_ref
, &ref_list
);
330 qsort(ref_list
.list
, ref_list
.index
, sizeof(struct ref_item
), ref_cmp
);
332 detached
= (detached
&& (kinds
& REF_LOCAL_BRANCH
));
333 if (detached
&& has_commit(head_sha1
, with_commit
)) {
334 struct ref_item item
;
335 item
.name
= xstrdup("(no branch)");
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);
344 for (i
= 0; i
< ref_list
.index
; i
++) {
345 int current
= !detached
&&
346 (ref_list
.list
[i
].kind
== REF_LOCAL_BRANCH
) &&
347 !strcmp(ref_list
.list
[i
].name
, head
);
348 print_ref_item(&ref_list
.list
[i
], ref_list
.maxwidth
, verbose
,
352 free_ref_list(&ref_list
);
355 static void rename_branch(const char *oldname
, const char *newname
, int force
)
357 char oldref
[PATH_MAX
], newref
[PATH_MAX
], logmsg
[PATH_MAX
*2 + 100];
358 unsigned char sha1
[20];
359 char oldsection
[PATH_MAX
], newsection
[PATH_MAX
];
362 die("cannot rename the current branch while not on any.");
364 if (snprintf(oldref
, sizeof(oldref
), "refs/heads/%s", oldname
) > sizeof(oldref
))
365 die("Old branchname too long");
367 if (check_ref_format(oldref
))
368 die("Invalid branch name: %s", oldref
);
370 if (snprintf(newref
, sizeof(newref
), "refs/heads/%s", newname
) > sizeof(newref
))
371 die("New branchname too long");
373 if (check_ref_format(newref
))
374 die("Invalid branch name: %s", newref
);
376 if (resolve_ref(newref
, sha1
, 1, NULL
) && !force
)
377 die("A branch named '%s' already exists.", newname
);
379 snprintf(logmsg
, sizeof(logmsg
), "Branch: renamed %s to %s",
382 if (rename_ref(oldref
, newref
, logmsg
))
383 die("Branch rename failed");
385 /* no need to pass logmsg here as HEAD didn't really move */
386 if (!strcmp(oldname
, head
) && create_symref("HEAD", newref
, NULL
))
387 die("Branch renamed to %s, but HEAD is not updated!", newname
);
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");
395 static int opt_parse_with_commit(const struct option
*opt
, const char *arg
, int unset
)
397 unsigned char sha1
[20];
398 struct commit
*commit
;
402 if (get_sha1(arg
, sha1
))
403 die("malformed object name %s", arg
);
404 commit
= lookup_commit_reference(sha1
);
406 die("no such commit %s", arg
);
407 commit_list_insert(commit
, opt
->value
);
411 int cmd_branch(int argc
, const char **argv
, const char *prefix
)
413 int delete = 0, rename
= 0, force_create
= 0;
414 int verbose
= 0, abbrev
= DEFAULT_ABBREV
, detached
= 0;
416 enum branch_track track
;
417 int kinds
= REF_LOCAL_BRANCH
;
418 struct commit_list
*with_commit
= NULL
;
420 struct option options
[] = {
421 OPT_GROUP("Generic options"),
422 OPT__VERBOSE(&verbose
),
423 OPT_SET_INT( 0 , "track", &track
, "set up tracking mode (see git-pull(1))",
424 BRANCH_TRACK_EXPLICIT
),
425 OPT_BOOLEAN( 0 , "color", &branch_use_color
, "use colored output"),
426 OPT_SET_INT('r', NULL
, &kinds
, "act on remote-tracking branches",
428 OPT_CALLBACK(0, "contains", &with_commit
, "commit",
429 "print only branches that contain the commit",
430 opt_parse_with_commit
),
432 OPTION_CALLBACK
, 0, "with", &with_commit
, "commit",
433 "print only branches that contain the commit",
434 PARSE_OPT_HIDDEN
, opt_parse_with_commit
,
436 OPT__ABBREV(&abbrev
),
438 OPT_GROUP("Specific git-branch actions:"),
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)"),
450 git_config(git_branch_config
);
451 track
= git_branch_track
;
452 argc
= parse_options(argc
, argv
, options
, builtin_branch_usage
, 0);
453 if (!!delete + !!rename
+ !!force_create
> 1)
454 usage_with_options(builtin_branch_usage
, options
);
456 head
= resolve_ref("HEAD", head_sha1
, 0, NULL
);
458 die("Failed to resolve HEAD as a valid ref.");
459 head
= xstrdup(head
);
460 if (!strcmp(head
, "HEAD")) {
463 if (prefixcmp(head
, "refs/heads/"))
464 die("HEAD not found below refs/heads!");
469 return delete_branches(argc
, argv
, delete > 1, kinds
);
471 print_ref_list(kinds
, detached
, verbose
, abbrev
, with_commit
);
472 else if (rename
&& (argc
== 1))
473 rename_branch(head
, argv
[0], rename
> 1);
474 else if (rename
&& (argc
== 2))
475 rename_branch(argv
[0], argv
[1], rename
> 1);
477 create_branch(head
, argv
[0], (argc
== 2) ? argv
[1] : head
,
478 force_create
, reflog
, track
);
480 usage_with_options(builtin_branch_usage
, options
);