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> | [--track | --no-track] [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
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_track_remotes
= 1;
27 static int branch_use_color
;
28 static char branch_colors
[][COLOR_MAXLEN
] = {
30 "", /* PLAIN (normal) */
31 "\033[31m", /* REMOTE (red) */
32 "", /* LOCAL (normal) */
33 "\033[32m", /* CURRENT (green) */
36 COLOR_BRANCH_RESET
= 0,
37 COLOR_BRANCH_PLAIN
= 1,
38 COLOR_BRANCH_REMOTE
= 2,
39 COLOR_BRANCH_LOCAL
= 3,
40 COLOR_BRANCH_CURRENT
= 4,
43 static int parse_branch_color_slot(const char *var
, int ofs
)
45 if (!strcasecmp(var
+ofs
, "plain"))
46 return COLOR_BRANCH_PLAIN
;
47 if (!strcasecmp(var
+ofs
, "reset"))
48 return COLOR_BRANCH_RESET
;
49 if (!strcasecmp(var
+ofs
, "remote"))
50 return COLOR_BRANCH_REMOTE
;
51 if (!strcasecmp(var
+ofs
, "local"))
52 return COLOR_BRANCH_LOCAL
;
53 if (!strcasecmp(var
+ofs
, "current"))
54 return COLOR_BRANCH_CURRENT
;
55 die("bad config variable '%s'", var
);
58 static int git_branch_config(const char *var
, const char *value
)
60 if (!strcmp(var
, "color.branch")) {
61 branch_use_color
= git_config_colorbool(var
, value
);
64 if (!prefixcmp(var
, "color.branch.")) {
65 int slot
= parse_branch_color_slot(var
, 13);
66 color_parse(value
, var
, branch_colors
[slot
]);
69 if (!strcmp(var
, "branch.autosetupmerge"))
70 branch_track_remotes
= git_config_bool(var
, value
);
72 return git_default_config(var
, value
);
75 static const char *branch_get_color(enum color_branch ix
)
78 return branch_colors
[ix
];
82 static int delete_branches(int argc
, const char **argv
, int force
, int kinds
)
84 struct commit
*rev
, *head_rev
= head_rev
;
85 unsigned char sha1
[20];
87 const char *fmt
, *remote
;
88 char section
[PATH_MAX
];
93 case REF_REMOTE_BRANCH
:
94 fmt
= "refs/remotes/%s";
98 case REF_LOCAL_BRANCH
:
99 fmt
= "refs/heads/%s";
103 die("cannot use -a with -d");
107 head_rev
= lookup_commit_reference(head_sha1
);
109 die("Couldn't look up commit object for HEAD");
111 for (i
= 0; i
< argc
; i
++) {
112 if (kinds
== REF_LOCAL_BRANCH
&& !strcmp(head
, argv
[i
])) {
113 error("Cannot delete the branch '%s' "
114 "which you are currently on.", argv
[i
]);
122 name
= xstrdup(mkpath(fmt
, argv
[i
]));
123 if (!resolve_ref(name
, sha1
, 1, NULL
)) {
124 error("%sbranch '%s' not found.",
130 rev
= lookup_commit_reference(sha1
);
132 error("Couldn't look up commit object for '%s'", name
);
137 /* This checks whether the merge bases of branch and
138 * HEAD contains branch -- which means that the HEAD
139 * contains everything in both.
143 !in_merge_bases(rev
, &head_rev
, 1)) {
144 error("The branch '%s' is not a strict subset of "
145 "your current HEAD.\n"
146 "If you are sure you want to delete it, "
147 "run 'git branch -D %s'.", argv
[i
], argv
[i
]);
152 if (delete_ref(name
, sha1
)) {
153 error("Error deleting %sbranch '%s'", remote
,
157 printf("Deleted %sbranch %s.\n", remote
, argv
[i
]);
158 snprintf(section
, sizeof(section
), "branch.%s",
160 if (git_config_rename_section(section
, NULL
) < 0)
161 warning("Update of config-file failed");
174 unsigned char sha1
[20];
178 int index
, alloc
, maxwidth
;
179 struct ref_item
*list
;
183 static int append_ref(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
185 struct ref_list
*ref_list
= (struct ref_list
*)(cb_data
);
186 struct ref_item
*newitem
;
187 int kind
= REF_UNKNOWN_TYPE
;
191 if (!prefixcmp(refname
, "refs/heads/")) {
192 kind
= REF_LOCAL_BRANCH
;
194 } else if (!prefixcmp(refname
, "refs/remotes/")) {
195 kind
= REF_REMOTE_BRANCH
;
197 } else if (!prefixcmp(refname
, "refs/tags/")) {
202 /* Don't add types the caller doesn't want */
203 if ((kind
& ref_list
->kinds
) == 0)
207 if (ref_list
->index
>= ref_list
->alloc
) {
208 ref_list
->alloc
= alloc_nr(ref_list
->alloc
);
209 ref_list
->list
= xrealloc(ref_list
->list
,
210 ref_list
->alloc
* sizeof(struct ref_item
));
213 /* Record the new item */
214 newitem
= &(ref_list
->list
[ref_list
->index
++]);
215 newitem
->name
= xstrdup(refname
);
216 newitem
->kind
= kind
;
217 hashcpy(newitem
->sha1
, sha1
);
218 len
= strlen(newitem
->name
);
219 if (len
> ref_list
->maxwidth
)
220 ref_list
->maxwidth
= len
;
225 static void free_ref_list(struct ref_list
*ref_list
)
229 for (i
= 0; i
< ref_list
->index
; i
++)
230 free(ref_list
->list
[i
].name
);
231 free(ref_list
->list
);
234 static int ref_cmp(const void *r1
, const void *r2
)
236 struct ref_item
*c1
= (struct ref_item
*)(r1
);
237 struct ref_item
*c2
= (struct ref_item
*)(r2
);
239 if (c1
->kind
!= c2
->kind
)
240 return c1
->kind
- c2
->kind
;
241 return strcmp(c1
->name
, c2
->name
);
244 static void print_ref_item(struct ref_item
*item
, int maxwidth
, int verbose
,
245 int abbrev
, int current
)
249 struct commit
*commit
;
251 switch (item
->kind
) {
252 case REF_LOCAL_BRANCH
:
253 color
= COLOR_BRANCH_LOCAL
;
255 case REF_REMOTE_BRANCH
:
256 color
= COLOR_BRANCH_REMOTE
;
259 color
= COLOR_BRANCH_PLAIN
;
266 color
= COLOR_BRANCH_CURRENT
;
270 char *subject
= NULL
;
271 unsigned long subject_len
= 0;
272 const char *sub
= " **** invalid ref ****";
274 commit
= lookup_commit(item
->sha1
);
275 if (commit
&& !parse_commit(commit
)) {
276 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
277 &subject
, &subject_len
, 0,
281 printf("%c %s%-*s%s %s %s\n", c
, branch_get_color(color
),
282 maxwidth
, item
->name
,
283 branch_get_color(COLOR_BRANCH_RESET
),
284 find_unique_abbrev(item
->sha1
, abbrev
), sub
);
288 printf("%c %s%s%s\n", c
, branch_get_color(color
), item
->name
,
289 branch_get_color(COLOR_BRANCH_RESET
));
293 static void print_ref_list(int kinds
, int detached
, int verbose
, int abbrev
)
296 struct ref_list ref_list
;
298 memset(&ref_list
, 0, sizeof(ref_list
));
299 ref_list
.kinds
= kinds
;
300 for_each_ref(append_ref
, &ref_list
);
302 qsort(ref_list
.list
, ref_list
.index
, sizeof(struct ref_item
), ref_cmp
);
304 detached
= (detached
&& (kinds
& REF_LOCAL_BRANCH
));
306 struct ref_item item
;
307 item
.name
= xstrdup("(no branch)");
308 item
.kind
= REF_LOCAL_BRANCH
;
309 hashcpy(item
.sha1
, head_sha1
);
310 if (strlen(item
.name
) > ref_list
.maxwidth
)
311 ref_list
.maxwidth
= strlen(item
.name
);
312 print_ref_item(&item
, ref_list
.maxwidth
, verbose
, abbrev
, 1);
316 for (i
= 0; i
< ref_list
.index
; i
++) {
317 int current
= !detached
&&
318 (ref_list
.list
[i
].kind
== REF_LOCAL_BRANCH
) &&
319 !strcmp(ref_list
.list
[i
].name
, head
);
320 print_ref_item(&ref_list
.list
[i
], ref_list
.maxwidth
, verbose
,
324 free_ref_list(&ref_list
);
327 static char *config_repo
;
328 static char *config_remote
;
329 static const char *start_ref
;
331 static int get_remote_branch_name(const char *value
)
339 colon
= strchr(value
, ':');
343 end
= value
+ strlen(value
);
346 * Try an exact match first. I.e. handle the case where the
347 * value is "$anything:refs/foo/bar/baz" and start_ref is exactly
348 * "refs/foo/bar/baz". Then the name at the remote is $anything.
350 if (!strcmp(colon
+ 1, start_ref
)) {
351 /* Truncate the value before the colon. */
352 nfasprintf(&config_repo
, "%.*s", colon
- value
, value
);
357 * Is this a wildcard match?
359 if ((end
- 2 <= value
) || end
[-2] != '/' || end
[-1] != '*' ||
360 (colon
- 2 <= value
) || colon
[-2] != '/' || colon
[-1] != '*')
364 * Value is "refs/foo/bar/<asterisk>:refs/baz/boa/<asterisk>"
365 * and start_ref begins with "refs/baz/boa/"; the name at the
366 * remote is refs/foo/bar/ with the remaining part of the
367 * start_ref. The length of the prefix on the RHS is (end -
368 * colon - 2), including the slash immediately before the
371 if ((strlen(start_ref
) < end
- colon
- 2) ||
372 memcmp(start_ref
, colon
+ 1, end
- colon
- 2))
373 return 0; /* does not match prefix */
375 /* Replace the asterisk with the remote branch name. */
376 nfasprintf(&config_repo
, "%.*s%s",
377 (colon
- 1) - value
, value
,
378 start_ref
+ (end
- colon
- 2));
382 static int get_remote_config(const char *key
, const char *value
)
385 if (prefixcmp(key
, "remote."))
388 var
= strrchr(key
, '.');
389 if (var
== key
+ 6 || strcmp(var
, ".fetch"))
392 * Ok, we are looking at key == "remote.$foo.fetch";
394 if (get_remote_branch_name(value
))
395 nfasprintf(&config_remote
, "%.*s", var
- (key
+ 7), key
+ 7);
400 static void set_branch_merge(const char *name
, const char *config_remote
,
401 const char *config_repo
)
405 snprintf(key
, sizeof(key
), "branch.%s.remote", name
))
406 die("what a long branch name you have!");
407 git_config_set(key
, config_remote
);
410 * We do not have to check if we have enough space for
411 * the 'merge' key, since it's shorter than the
412 * previous 'remote' key, which we already checked.
414 snprintf(key
, sizeof(key
), "branch.%s.merge", name
);
415 git_config_set(key
, config_repo
);
418 static void set_branch_defaults(const char *name
, const char *real_ref
)
421 * name is the name of new branch under refs/heads;
422 * real_ref is typically refs/remotes/$foo/$bar, where
423 * $foo is the remote name (there typically are no slashes)
424 * and $bar is the branch name we map from the remote
425 * (it could have slashes).
427 start_ref
= real_ref
;
428 git_config(get_remote_config
);
429 if (!config_repo
&& !config_remote
&&
430 !prefixcmp(real_ref
, "refs/heads/")) {
431 set_branch_merge(name
, ".", real_ref
);
432 printf("Branch %s set up to track local branch %s.\n",
436 if (config_repo
&& config_remote
) {
437 set_branch_merge(name
, config_remote
, config_repo
);
438 printf("Branch %s set up to track remote branch %s.\n",
448 static void create_branch(const char *name
, const char *start_name
,
449 int force
, int reflog
, int track
)
451 struct ref_lock
*lock
;
452 struct commit
*commit
;
453 unsigned char sha1
[20];
454 char *real_ref
, ref
[PATH_MAX
], msg
[PATH_MAX
+ 20];
457 snprintf(ref
, sizeof ref
, "refs/heads/%s", name
);
458 if (check_ref_format(ref
))
459 die("'%s' is not a valid branch name.", name
);
461 if (resolve_ref(ref
, sha1
, 1, NULL
)) {
463 die("A branch named '%s' already exists.", name
);
464 else if (!is_bare_repository() && !strcmp(head
, name
))
465 die("Cannot force update the current branch.");
470 if (get_sha1(start_name
, sha1
))
471 die("Not a valid object name: '%s'.", start_name
);
473 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
475 /* Not branching from any existing branch */
479 /* Unique completion -- good */
482 die("Ambiguous object name: '%s'.", start_name
);
486 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
487 die("Not a valid branch point: '%s'.", start_name
);
488 hashcpy(sha1
, commit
->object
.sha1
);
490 lock
= lock_any_ref_for_update(ref
, NULL
, 0);
492 die("Failed to lock ref for update: %s.", strerror(errno
));
495 log_all_ref_updates
= 1;
498 snprintf(msg
, sizeof msg
, "branch: Reset from %s",
501 snprintf(msg
, sizeof msg
, "branch: Created from %s",
504 /* When branching off a remote branch, set up so that git-pull
505 automatically merges from there. So far, this is only done for
506 remotes registered via .git/config. */
507 if (real_ref
&& track
)
508 set_branch_defaults(name
, real_ref
);
510 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
511 die("Failed to write ref: %s.", strerror(errno
));
517 static void rename_branch(const char *oldname
, const char *newname
, int force
)
519 char oldref
[PATH_MAX
], newref
[PATH_MAX
], logmsg
[PATH_MAX
*2 + 100];
520 unsigned char sha1
[20];
521 char oldsection
[PATH_MAX
], newsection
[PATH_MAX
];
524 die("cannot rename the current branch while not on any.");
526 if (snprintf(oldref
, sizeof(oldref
), "refs/heads/%s", oldname
) > sizeof(oldref
))
527 die("Old branchname too long");
529 if (check_ref_format(oldref
))
530 die("Invalid branch name: %s", oldref
);
532 if (snprintf(newref
, sizeof(newref
), "refs/heads/%s", newname
) > sizeof(newref
))
533 die("New branchname too long");
535 if (check_ref_format(newref
))
536 die("Invalid branch name: %s", newref
);
538 if (resolve_ref(newref
, sha1
, 1, NULL
) && !force
)
539 die("A branch named '%s' already exists.", newname
);
541 snprintf(logmsg
, sizeof(logmsg
), "Branch: renamed %s to %s",
544 if (rename_ref(oldref
, newref
, logmsg
))
545 die("Branch rename failed");
547 /* no need to pass logmsg here as HEAD didn't really move */
548 if (!strcmp(oldname
, head
) && create_symref("HEAD", newref
, NULL
))
549 die("Branch renamed to %s, but HEAD is not updated!", newname
);
551 snprintf(oldsection
, sizeof(oldsection
), "branch.%s", oldref
+ 11);
552 snprintf(newsection
, sizeof(newsection
), "branch.%s", newref
+ 11);
553 if (git_config_rename_section(oldsection
, newsection
) < 0)
554 die("Branch is renamed, but update of config-file failed");
557 int cmd_branch(int argc
, const char **argv
, const char *prefix
)
559 int delete = 0, force_delete
= 0, force_create
= 0;
560 int rename
= 0, force_rename
= 0;
561 int verbose
= 0, abbrev
= DEFAULT_ABBREV
, detached
= 0;
562 int reflog
= 0, track
;
563 int kinds
= REF_LOCAL_BRANCH
;
566 git_config(git_branch_config
);
567 track
= branch_track_remotes
;
569 for (i
= 1; i
< argc
; i
++) {
570 const char *arg
= argv
[i
];
574 if (!strcmp(arg
, "--")) {
578 if (!strcmp(arg
, "--track")) {
582 if (!strcmp(arg
, "--no-track")) {
586 if (!strcmp(arg
, "-d")) {
590 if (!strcmp(arg
, "-D")) {
595 if (!strcmp(arg
, "-f")) {
599 if (!strcmp(arg
, "-m")) {
603 if (!strcmp(arg
, "-M")) {
608 if (!strcmp(arg
, "-r")) {
609 kinds
= REF_REMOTE_BRANCH
;
612 if (!strcmp(arg
, "-a")) {
613 kinds
= REF_REMOTE_BRANCH
| REF_LOCAL_BRANCH
;
616 if (!strcmp(arg
, "-l")) {
620 if (!prefixcmp(arg
, "--no-abbrev")) {
624 if (!prefixcmp(arg
, "--abbrev=")) {
625 abbrev
= strtoul(arg
+ 9, NULL
, 10);
626 if (abbrev
< MINIMUM_ABBREV
)
627 abbrev
= MINIMUM_ABBREV
;
628 else if (abbrev
> 40)
632 if (!strcmp(arg
, "-v")) {
636 if (!strcmp(arg
, "--color")) {
637 branch_use_color
= 1;
640 if (!strcmp(arg
, "--no-color")) {
641 branch_use_color
= 0;
644 usage(builtin_branch_usage
);
647 if ((delete && rename
) || (delete && force_create
) ||
648 (rename
&& force_create
))
649 usage(builtin_branch_usage
);
651 head
= resolve_ref("HEAD", head_sha1
, 0, NULL
);
653 die("Failed to resolve HEAD as a valid ref.");
654 head
= xstrdup(head
);
655 if (!strcmp(head
, "HEAD")) {
659 if (prefixcmp(head
, "refs/heads/"))
660 die("HEAD not found below refs/heads!");
665 return delete_branches(argc
- i
, argv
+ i
, force_delete
, kinds
);
667 print_ref_list(kinds
, detached
, verbose
, abbrev
);
668 else if (rename
&& (i
== argc
- 1))
669 rename_branch(head
, argv
[i
], force_rename
);
670 else if (rename
&& (i
== argc
- 2))
671 rename_branch(argv
[i
], argv
[i
+ 1], force_rename
);
672 else if (i
== argc
- 1 || i
== argc
- 2)
673 create_branch(argv
[i
], (i
== argc
- 2) ? argv
[i
+1] : head
,
674 force_create
, reflog
, track
);
676 usage(builtin_branch_usage
);