4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
14 static const char builtin_branch_usage
[] =
15 "git-branch [-r] (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length>]]";
17 #define REF_UNKNOWN_TYPE 0x00
18 #define REF_LOCAL_BRANCH 0x01
19 #define REF_REMOTE_BRANCH 0x02
22 static const char *head
;
23 static unsigned char head_sha1
[20];
25 static int branch_use_color
;
26 static char branch_colors
[][COLOR_MAXLEN
] = {
28 "", /* PLAIN (normal) */
29 "\033[31m", /* REMOTE (red) */
30 "", /* LOCAL (normal) */
31 "\033[32m", /* CURRENT (green) */
34 COLOR_BRANCH_RESET
= 0,
35 COLOR_BRANCH_PLAIN
= 1,
36 COLOR_BRANCH_REMOTE
= 2,
37 COLOR_BRANCH_LOCAL
= 3,
38 COLOR_BRANCH_CURRENT
= 4,
41 static int parse_branch_color_slot(const char *var
, int ofs
)
43 if (!strcasecmp(var
+ofs
, "plain"))
44 return COLOR_BRANCH_PLAIN
;
45 if (!strcasecmp(var
+ofs
, "reset"))
46 return COLOR_BRANCH_RESET
;
47 if (!strcasecmp(var
+ofs
, "remote"))
48 return COLOR_BRANCH_REMOTE
;
49 if (!strcasecmp(var
+ofs
, "local"))
50 return COLOR_BRANCH_LOCAL
;
51 if (!strcasecmp(var
+ofs
, "current"))
52 return COLOR_BRANCH_CURRENT
;
53 die("bad config variable '%s'", var
);
56 int git_branch_config(const char *var
, const char *value
)
58 if (!strcmp(var
, "color.branch")) {
59 branch_use_color
= git_config_colorbool(var
, value
);
62 if (!strncmp(var
, "color.branch.", 13)) {
63 int slot
= parse_branch_color_slot(var
, 13);
64 color_parse(value
, var
, branch_colors
[slot
]);
67 return git_default_config(var
, value
);
70 const char *branch_get_color(enum color_branch ix
)
73 return branch_colors
[ix
];
77 static int delete_branches(int argc
, const char **argv
, int force
, int kinds
)
79 struct commit
*rev
, *head_rev
= head_rev
;
80 unsigned char sha1
[20];
82 const char *fmt
, *remote
;
87 case REF_REMOTE_BRANCH
:
88 fmt
= "refs/remotes/%s";
92 case REF_LOCAL_BRANCH
:
93 fmt
= "refs/heads/%s";
97 die("cannot use -a with -d");
101 head_rev
= lookup_commit_reference(head_sha1
);
103 die("Couldn't look up commit object for HEAD");
105 for (i
= 0; i
< argc
; i
++) {
106 if (kinds
== REF_LOCAL_BRANCH
&& !strcmp(head
, argv
[i
])) {
107 error("Cannot delete the branch '%s' "
108 "which you are currently on.", argv
[i
]);
116 name
= xstrdup(mkpath(fmt
, argv
[i
]));
117 if (!resolve_ref(name
, sha1
, 1, NULL
)) {
118 error("%sbranch '%s' not found.",
124 rev
= lookup_commit_reference(sha1
);
126 error("Couldn't look up commit object for '%s'", name
);
131 /* This checks whether the merge bases of branch and
132 * HEAD contains branch -- which means that the HEAD
133 * contains everything in both.
137 !in_merge_bases(rev
, head_rev
)) {
138 error("The branch '%s' is not a strict subset of "
139 "your current HEAD.\n"
140 "If you are sure you want to delete it, "
141 "run 'git branch -D %s'.", argv
[i
], argv
[i
]);
146 if (delete_ref(name
, sha1
)) {
147 error("Error deleting %sbranch '%s'", remote
,
151 printf("Deleted %sbranch %s.\n", remote
, argv
[i
]);
164 unsigned char sha1
[20];
168 int index
, alloc
, maxwidth
;
169 struct ref_item
*list
;
173 static int append_ref(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
175 struct ref_list
*ref_list
= (struct ref_list
*)(cb_data
);
176 struct ref_item
*newitem
;
177 int kind
= REF_UNKNOWN_TYPE
;
181 if (!strncmp(refname
, "refs/heads/", 11)) {
182 kind
= REF_LOCAL_BRANCH
;
184 } else if (!strncmp(refname
, "refs/remotes/", 13)) {
185 kind
= REF_REMOTE_BRANCH
;
187 } else if (!strncmp(refname
, "refs/tags/", 10)) {
192 /* Don't add types the caller doesn't want */
193 if ((kind
& ref_list
->kinds
) == 0)
197 if (ref_list
->index
>= ref_list
->alloc
) {
198 ref_list
->alloc
= alloc_nr(ref_list
->alloc
);
199 ref_list
->list
= xrealloc(ref_list
->list
,
200 ref_list
->alloc
* sizeof(struct ref_item
));
203 /* Record the new item */
204 newitem
= &(ref_list
->list
[ref_list
->index
++]);
205 newitem
->name
= xstrdup(refname
);
206 newitem
->kind
= kind
;
207 hashcpy(newitem
->sha1
, sha1
);
208 len
= strlen(newitem
->name
);
209 if (len
> ref_list
->maxwidth
)
210 ref_list
->maxwidth
= len
;
215 static void free_ref_list(struct ref_list
*ref_list
)
219 for (i
= 0; i
< ref_list
->index
; i
++)
220 free(ref_list
->list
[i
].name
);
221 free(ref_list
->list
);
224 static int ref_cmp(const void *r1
, const void *r2
)
226 struct ref_item
*c1
= (struct ref_item
*)(r1
);
227 struct ref_item
*c2
= (struct ref_item
*)(r2
);
229 if (c1
->kind
!= c2
->kind
)
230 return c1
->kind
- c2
->kind
;
231 return strcmp(c1
->name
, c2
->name
);
234 static void print_ref_info(const unsigned char *sha1
, int abbrev
)
236 struct commit
*commit
;
240 commit
= lookup_commit(sha1
);
241 if (commit
&& !parse_commit(commit
))
242 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
243 subject
, sizeof(subject
), 0,
246 strcpy(subject
, " **** invalid ref ****");
248 printf(" %s %s\n", find_unique_abbrev(sha1
, abbrev
), subject
);
251 static void print_ref_list(int kinds
, int verbose
, int abbrev
)
255 struct ref_list ref_list
;
258 memset(&ref_list
, 0, sizeof(ref_list
));
259 ref_list
.kinds
= kinds
;
260 for_each_ref(append_ref
, &ref_list
);
262 qsort(ref_list
.list
, ref_list
.index
, sizeof(struct ref_item
), ref_cmp
);
264 for (i
= 0; i
< ref_list
.index
; i
++) {
265 switch( ref_list
.list
[i
].kind
) {
266 case REF_LOCAL_BRANCH
:
267 color
= COLOR_BRANCH_LOCAL
;
269 case REF_REMOTE_BRANCH
:
270 color
= COLOR_BRANCH_REMOTE
;
273 color
= COLOR_BRANCH_PLAIN
;
278 if (ref_list
.list
[i
].kind
== REF_LOCAL_BRANCH
&&
279 !strcmp(ref_list
.list
[i
].name
, head
)) {
281 color
= COLOR_BRANCH_CURRENT
;
285 printf("%c %s%-*s%s", c
,
286 branch_get_color(color
),
288 ref_list
.list
[i
].name
,
289 branch_get_color(COLOR_BRANCH_RESET
));
290 print_ref_info(ref_list
.list
[i
].sha1
, abbrev
);
293 printf("%c %s%s%s\n", c
,
294 branch_get_color(color
),
295 ref_list
.list
[i
].name
,
296 branch_get_color(COLOR_BRANCH_RESET
));
299 free_ref_list(&ref_list
);
302 static void create_branch(const char *name
, const char *start
,
303 int force
, int reflog
)
305 struct ref_lock
*lock
;
306 struct commit
*commit
;
307 unsigned char sha1
[20];
308 char ref
[PATH_MAX
], msg
[PATH_MAX
+ 20];
310 snprintf(ref
, sizeof ref
, "refs/heads/%s", name
);
311 if (check_ref_format(ref
))
312 die("'%s' is not a valid branch name.", name
);
314 if (resolve_ref(ref
, sha1
, 1, NULL
)) {
316 die("A branch named '%s' already exists.", name
);
317 else if (!strcmp(head
, name
))
318 die("Cannot force update the current branch.");
321 if (get_sha1(start
, sha1
) ||
322 (commit
= lookup_commit_reference(sha1
)) == NULL
)
323 die("Not a valid branch point: '%s'.", start
);
324 hashcpy(sha1
, commit
->object
.sha1
);
326 lock
= lock_any_ref_for_update(ref
, NULL
);
328 die("Failed to lock ref for update: %s.", strerror(errno
));
331 log_all_ref_updates
= 1;
332 snprintf(msg
, sizeof msg
, "branch: Created from %s", start
);
335 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
336 die("Failed to write ref: %s.", strerror(errno
));
339 static void rename_branch(const char *oldname
, const char *newname
, int force
)
341 char oldref
[PATH_MAX
], newref
[PATH_MAX
], logmsg
[PATH_MAX
*2 + 100];
342 unsigned char sha1
[20];
344 if (snprintf(oldref
, sizeof(oldref
), "refs/heads/%s", oldname
) > sizeof(oldref
))
345 die("Old branchname too long");
347 if (check_ref_format(oldref
))
348 die("Invalid branch name: %s", oldref
);
350 if (snprintf(newref
, sizeof(newref
), "refs/heads/%s", newname
) > sizeof(newref
))
351 die("New branchname too long");
353 if (check_ref_format(newref
))
354 die("Invalid branch name: %s", newref
);
356 if (resolve_ref(newref
, sha1
, 1, NULL
) && !force
)
357 die("A branch named '%s' already exists.", newname
);
359 snprintf(logmsg
, sizeof(logmsg
), "Branch: renamed %s to %s",
362 if (rename_ref(oldref
, newref
, logmsg
))
363 die("Branch rename failed");
365 if (!strcmp(oldname
, head
) && create_symref("HEAD", newref
))
366 die("Branch renamed to %s, but HEAD is not updated!", newname
);
369 int cmd_branch(int argc
, const char **argv
, const char *prefix
)
371 int delete = 0, force_delete
= 0, force_create
= 0;
372 int rename
= 0, force_rename
= 0;
373 int verbose
= 0, abbrev
= DEFAULT_ABBREV
;
375 int kinds
= REF_LOCAL_BRANCH
;
379 git_config(git_branch_config
);
381 for (i
= 1; i
< argc
; i
++) {
382 const char *arg
= argv
[i
];
386 if (!strcmp(arg
, "--")) {
390 if (!strcmp(arg
, "-d")) {
394 if (!strcmp(arg
, "-D")) {
399 if (!strcmp(arg
, "-f")) {
403 if (!strcmp(arg
, "-m")) {
407 if (!strcmp(arg
, "-M")) {
412 if (!strcmp(arg
, "-r")) {
413 kinds
= REF_REMOTE_BRANCH
;
416 if (!strcmp(arg
, "-a")) {
417 kinds
= REF_REMOTE_BRANCH
| REF_LOCAL_BRANCH
;
420 if (!strcmp(arg
, "-l")) {
424 if (!strncmp(arg
, "--abbrev=", 9)) {
425 abbrev
= atoi(arg
+9);
428 if (!strcmp(arg
, "-v")) {
432 if (!strcmp(arg
, "--color")) {
433 branch_use_color
= 1;
436 if (!strcmp(arg
, "--no-color")) {
437 branch_use_color
= 0;
440 usage(builtin_branch_usage
);
443 if ((delete && rename
) || (delete && force_create
) ||
444 (rename
&& force_create
))
445 usage(builtin_branch_usage
);
447 head
= xstrdup(resolve_ref("HEAD", head_sha1
, 0, NULL
));
449 die("Failed to resolve HEAD as a valid ref.");
450 if (strncmp(head
, "refs/heads/", 11))
451 die("HEAD not found below refs/heads!");
455 return delete_branches(argc
- i
, argv
+ i
, force_delete
, kinds
);
457 print_ref_list(kinds
, verbose
, abbrev
);
458 else if (rename
&& (i
== argc
- 1))
459 rename_branch(head
, argv
[i
], force_rename
);
460 else if (rename
&& (i
== argc
- 2))
461 rename_branch(argv
[i
], argv
[i
+ 1], force_rename
);
462 else if (i
== argc
- 1)
463 create_branch(argv
[i
], head
, force_create
, reflog
);
464 else if (i
== argc
- 2)
465 create_branch(argv
[i
], argv
[i
+ 1], force_create
, reflog
);
467 usage(builtin_branch_usage
);