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
= 1; /* 0 = none, 1 = remotes, 2 = all */
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 if (!strcmp(value
, "all"))
73 branch_track
= git_config_bool(var
, value
);
76 return git_default_config(var
, value
);
79 static const char *branch_get_color(enum color_branch ix
)
82 return branch_colors
[ix
];
86 static int delete_branches(int argc
, const char **argv
, int force
, int kinds
)
88 struct commit
*rev
, *head_rev
= head_rev
;
89 unsigned char sha1
[20];
91 const char *fmt
, *remote
;
92 char section
[PATH_MAX
];
97 case REF_REMOTE_BRANCH
:
98 fmt
= "refs/remotes/%s";
102 case REF_LOCAL_BRANCH
:
103 fmt
= "refs/heads/%s";
107 die("cannot use -a with -d");
111 head_rev
= lookup_commit_reference(head_sha1
);
113 die("Couldn't look up commit object for HEAD");
115 for (i
= 0; i
< argc
; i
++) {
116 if (kinds
== REF_LOCAL_BRANCH
&& !strcmp(head
, argv
[i
])) {
117 error("Cannot delete the branch '%s' "
118 "which you are currently on.", argv
[i
]);
126 name
= xstrdup(mkpath(fmt
, argv
[i
]));
127 if (!resolve_ref(name
, sha1
, 1, NULL
)) {
128 error("%sbranch '%s' not found.",
134 rev
= lookup_commit_reference(sha1
);
136 error("Couldn't look up commit object for '%s'", name
);
141 /* This checks whether the merge bases of branch and
142 * HEAD contains branch -- which means that the HEAD
143 * contains everything in both.
147 !in_merge_bases(rev
, &head_rev
, 1)) {
148 error("The branch '%s' is not a strict subset of "
149 "your current HEAD.\n"
150 "If you are sure you want to delete it, "
151 "run 'git branch -D %s'.", argv
[i
], argv
[i
]);
156 if (delete_ref(name
, sha1
)) {
157 error("Error deleting %sbranch '%s'", remote
,
161 printf("Deleted %sbranch %s.\n", remote
, argv
[i
]);
162 snprintf(section
, sizeof(section
), "branch.%s",
164 if (git_config_rename_section(section
, NULL
) < 0)
165 warning("Update of config-file failed");
178 unsigned char sha1
[20];
182 int index
, alloc
, maxwidth
;
183 struct ref_item
*list
;
187 static int append_ref(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
189 struct ref_list
*ref_list
= (struct ref_list
*)(cb_data
);
190 struct ref_item
*newitem
;
191 int kind
= REF_UNKNOWN_TYPE
;
195 if (!prefixcmp(refname
, "refs/heads/")) {
196 kind
= REF_LOCAL_BRANCH
;
198 } else if (!prefixcmp(refname
, "refs/remotes/")) {
199 kind
= REF_REMOTE_BRANCH
;
201 } else if (!prefixcmp(refname
, "refs/tags/")) {
206 /* Don't add types the caller doesn't want */
207 if ((kind
& ref_list
->kinds
) == 0)
211 if (ref_list
->index
>= ref_list
->alloc
) {
212 ref_list
->alloc
= alloc_nr(ref_list
->alloc
);
213 ref_list
->list
= xrealloc(ref_list
->list
,
214 ref_list
->alloc
* sizeof(struct ref_item
));
217 /* Record the new item */
218 newitem
= &(ref_list
->list
[ref_list
->index
++]);
219 newitem
->name
= xstrdup(refname
);
220 newitem
->kind
= kind
;
221 hashcpy(newitem
->sha1
, sha1
);
222 len
= strlen(newitem
->name
);
223 if (len
> ref_list
->maxwidth
)
224 ref_list
->maxwidth
= len
;
229 static void free_ref_list(struct ref_list
*ref_list
)
233 for (i
= 0; i
< ref_list
->index
; i
++)
234 free(ref_list
->list
[i
].name
);
235 free(ref_list
->list
);
238 static int ref_cmp(const void *r1
, const void *r2
)
240 struct ref_item
*c1
= (struct ref_item
*)(r1
);
241 struct ref_item
*c2
= (struct ref_item
*)(r2
);
243 if (c1
->kind
!= c2
->kind
)
244 return c1
->kind
- c2
->kind
;
245 return strcmp(c1
->name
, c2
->name
);
248 static void print_ref_item(struct ref_item
*item
, int maxwidth
, int verbose
,
249 int abbrev
, int current
)
253 struct commit
*commit
;
255 switch (item
->kind
) {
256 case REF_LOCAL_BRANCH
:
257 color
= COLOR_BRANCH_LOCAL
;
259 case REF_REMOTE_BRANCH
:
260 color
= COLOR_BRANCH_REMOTE
;
263 color
= COLOR_BRANCH_PLAIN
;
270 color
= COLOR_BRANCH_CURRENT
;
274 char *subject
= NULL
;
275 unsigned long subject_len
= 0;
276 const char *sub
= " **** invalid ref ****";
278 commit
= lookup_commit(item
->sha1
);
279 if (commit
&& !parse_commit(commit
)) {
280 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0,
281 &subject
, &subject_len
, 0,
285 printf("%c %s%-*s%s %s %s\n", c
, branch_get_color(color
),
286 maxwidth
, item
->name
,
287 branch_get_color(COLOR_BRANCH_RESET
),
288 find_unique_abbrev(item
->sha1
, abbrev
), sub
);
292 printf("%c %s%s%s\n", c
, branch_get_color(color
), item
->name
,
293 branch_get_color(COLOR_BRANCH_RESET
));
297 static void print_ref_list(int kinds
, int detached
, int verbose
, int abbrev
)
300 struct ref_list ref_list
;
302 memset(&ref_list
, 0, sizeof(ref_list
));
303 ref_list
.kinds
= kinds
;
304 for_each_ref(append_ref
, &ref_list
);
306 qsort(ref_list
.list
, ref_list
.index
, sizeof(struct ref_item
), ref_cmp
);
308 detached
= (detached
&& (kinds
& REF_LOCAL_BRANCH
));
310 struct ref_item item
;
311 item
.name
= xstrdup("(no branch)");
312 item
.kind
= REF_LOCAL_BRANCH
;
313 hashcpy(item
.sha1
, head_sha1
);
314 if (strlen(item
.name
) > ref_list
.maxwidth
)
315 ref_list
.maxwidth
= strlen(item
.name
);
316 print_ref_item(&item
, ref_list
.maxwidth
, verbose
, abbrev
, 1);
320 for (i
= 0; i
< ref_list
.index
; i
++) {
321 int current
= !detached
&&
322 (ref_list
.list
[i
].kind
== REF_LOCAL_BRANCH
) &&
323 !strcmp(ref_list
.list
[i
].name
, head
);
324 print_ref_item(&ref_list
.list
[i
], ref_list
.maxwidth
, verbose
,
328 free_ref_list(&ref_list
);
331 static char *config_repo
;
332 static char *config_remote
;
333 static const char *start_ref
;
335 static int get_remote_branch_name(const char *value
)
343 colon
= strchr(value
, ':');
347 end
= value
+ strlen(value
);
350 * Try an exact match first. I.e. handle the case where the
351 * value is "$anything:refs/foo/bar/baz" and start_ref is exactly
352 * "refs/foo/bar/baz". Then the name at the remote is $anything.
354 if (!strcmp(colon
+ 1, start_ref
)) {
355 /* Truncate the value before the colon. */
356 nfasprintf(&config_repo
, "%.*s", colon
- value
, value
);
361 * Is this a wildcard match?
363 if ((end
- 2 <= value
) || end
[-2] != '/' || end
[-1] != '*' ||
364 (colon
- 2 <= value
) || colon
[-2] != '/' || colon
[-1] != '*')
368 * Value is "refs/foo/bar/<asterisk>:refs/baz/boa/<asterisk>"
369 * and start_ref begins with "refs/baz/boa/"; the name at the
370 * remote is refs/foo/bar/ with the remaining part of the
371 * start_ref. The length of the prefix on the RHS is (end -
372 * colon - 2), including the slash immediately before the
375 if ((strlen(start_ref
) < end
- colon
- 2) ||
376 memcmp(start_ref
, colon
+ 1, end
- colon
- 2))
377 return 0; /* does not match prefix */
379 /* Replace the asterisk with the remote branch name. */
380 nfasprintf(&config_repo
, "%.*s%s",
381 (colon
- 1) - value
, value
,
382 start_ref
+ (end
- colon
- 2));
386 static int get_remote_config(const char *key
, const char *value
)
389 if (prefixcmp(key
, "remote."))
392 var
= strrchr(key
, '.');
393 if (var
== key
+ 6 || strcmp(var
, ".fetch"))
396 * Ok, we are looking at key == "remote.$foo.fetch";
398 if (get_remote_branch_name(value
))
399 nfasprintf(&config_remote
, "%.*s", var
- (key
+ 7), key
+ 7);
404 static void set_branch_merge(const char *name
, const char *config_remote
,
405 const char *config_repo
)
409 snprintf(key
, sizeof(key
), "branch.%s.remote", name
))
410 die("what a long branch name you have!");
411 git_config_set(key
, config_remote
);
414 * We do not have to check if we have enough space for
415 * the 'merge' key, since it's shorter than the
416 * previous 'remote' key, which we already checked.
418 snprintf(key
, sizeof(key
), "branch.%s.merge", name
);
419 git_config_set(key
, config_repo
);
422 static void set_branch_defaults(const char *name
, const char *real_ref
)
425 * name is the name of new branch under refs/heads;
426 * real_ref is typically refs/remotes/$foo/$bar, where
427 * $foo is the remote name (there typically are no slashes)
428 * and $bar is the branch name we map from the remote
429 * (it could have slashes).
431 start_ref
= real_ref
;
432 git_config(get_remote_config
);
433 if (!config_repo
&& !config_remote
&&
434 !prefixcmp(real_ref
, "refs/heads/")) {
435 set_branch_merge(name
, ".", real_ref
);
436 printf("Branch %s set up to track local branch %s.\n",
440 if (config_repo
&& config_remote
) {
441 set_branch_merge(name
, config_remote
, config_repo
);
442 printf("Branch %s set up to track remote branch %s.\n",
452 static void create_branch(const char *name
, const char *start_name
,
453 int force
, int reflog
, int track
)
455 struct ref_lock
*lock
;
456 struct commit
*commit
;
457 unsigned char sha1
[20];
458 char *real_ref
, ref
[PATH_MAX
], msg
[PATH_MAX
+ 20];
461 snprintf(ref
, sizeof ref
, "refs/heads/%s", name
);
462 if (check_ref_format(ref
))
463 die("'%s' is not a valid branch name.", name
);
465 if (resolve_ref(ref
, sha1
, 1, NULL
)) {
467 die("A branch named '%s' already exists.", name
);
468 else if (!is_bare_repository() && !strcmp(head
, name
))
469 die("Cannot force update the current branch.");
474 if (get_sha1(start_name
, sha1
))
475 die("Not a valid object name: '%s'.", start_name
);
477 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
479 /* Not branching from any existing branch */
483 /* Unique completion -- good */
486 die("Ambiguous object name: '%s'.", start_name
);
490 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
491 die("Not a valid branch point: '%s'.", start_name
);
492 hashcpy(sha1
, commit
->object
.sha1
);
494 lock
= lock_any_ref_for_update(ref
, NULL
, 0);
496 die("Failed to lock ref for update: %s.", strerror(errno
));
499 log_all_ref_updates
= 1;
502 snprintf(msg
, sizeof msg
, "branch: Reset from %s",
505 snprintf(msg
, sizeof msg
, "branch: Created from %s",
508 /* When branching off a remote branch, set up so that git-pull
509 automatically merges from there. So far, this is only done for
510 remotes registered via .git/config. */
511 if (real_ref
&& (track
== 2 ||
513 !prefixcmp(real_ref
, "refs/remotes/"))))
514 set_branch_defaults(name
, real_ref
);
516 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
517 die("Failed to write ref: %s.", strerror(errno
));
523 static void rename_branch(const char *oldname
, const char *newname
, int force
)
525 char oldref
[PATH_MAX
], newref
[PATH_MAX
], logmsg
[PATH_MAX
*2 + 100];
526 unsigned char sha1
[20];
527 char oldsection
[PATH_MAX
], newsection
[PATH_MAX
];
530 die("cannot rename the current branch while not on any.");
532 if (snprintf(oldref
, sizeof(oldref
), "refs/heads/%s", oldname
) > sizeof(oldref
))
533 die("Old branchname too long");
535 if (check_ref_format(oldref
))
536 die("Invalid branch name: %s", oldref
);
538 if (snprintf(newref
, sizeof(newref
), "refs/heads/%s", newname
) > sizeof(newref
))
539 die("New branchname too long");
541 if (check_ref_format(newref
))
542 die("Invalid branch name: %s", newref
);
544 if (resolve_ref(newref
, sha1
, 1, NULL
) && !force
)
545 die("A branch named '%s' already exists.", newname
);
547 snprintf(logmsg
, sizeof(logmsg
), "Branch: renamed %s to %s",
550 if (rename_ref(oldref
, newref
, logmsg
))
551 die("Branch rename failed");
553 /* no need to pass logmsg here as HEAD didn't really move */
554 if (!strcmp(oldname
, head
) && create_symref("HEAD", newref
, NULL
))
555 die("Branch renamed to %s, but HEAD is not updated!", newname
);
557 snprintf(oldsection
, sizeof(oldsection
), "branch.%s", oldref
+ 11);
558 snprintf(newsection
, sizeof(newsection
), "branch.%s", newref
+ 11);
559 if (git_config_rename_section(oldsection
, newsection
) < 0)
560 die("Branch is renamed, but update of config-file failed");
563 int cmd_branch(int argc
, const char **argv
, const char *prefix
)
565 int delete = 0, force_delete
= 0, force_create
= 0;
566 int rename
= 0, force_rename
= 0;
567 int verbose
= 0, abbrev
= DEFAULT_ABBREV
, detached
= 0;
568 int reflog
= 0, track
;
569 int kinds
= REF_LOCAL_BRANCH
;
572 git_config(git_branch_config
);
573 track
= branch_track
;
575 for (i
= 1; i
< argc
; i
++) {
576 const char *arg
= argv
[i
];
580 if (!strcmp(arg
, "--")) {
584 if (!strcmp(arg
, "--track")) {
588 if (!strcmp(arg
, "--no-track")) {
592 if (!strcmp(arg
, "-d")) {
596 if (!strcmp(arg
, "-D")) {
601 if (!strcmp(arg
, "-f")) {
605 if (!strcmp(arg
, "-m")) {
609 if (!strcmp(arg
, "-M")) {
614 if (!strcmp(arg
, "-r")) {
615 kinds
= REF_REMOTE_BRANCH
;
618 if (!strcmp(arg
, "-a")) {
619 kinds
= REF_REMOTE_BRANCH
| REF_LOCAL_BRANCH
;
622 if (!strcmp(arg
, "-l")) {
626 if (!prefixcmp(arg
, "--no-abbrev")) {
630 if (!prefixcmp(arg
, "--abbrev=")) {
631 abbrev
= strtoul(arg
+ 9, NULL
, 10);
632 if (abbrev
< MINIMUM_ABBREV
)
633 abbrev
= MINIMUM_ABBREV
;
634 else if (abbrev
> 40)
638 if (!strcmp(arg
, "-v")) {
642 if (!strcmp(arg
, "--color")) {
643 branch_use_color
= 1;
646 if (!strcmp(arg
, "--no-color")) {
647 branch_use_color
= 0;
650 usage(builtin_branch_usage
);
653 if ((delete && rename
) || (delete && force_create
) ||
654 (rename
&& force_create
))
655 usage(builtin_branch_usage
);
657 head
= resolve_ref("HEAD", head_sha1
, 0, NULL
);
659 die("Failed to resolve HEAD as a valid ref.");
660 head
= xstrdup(head
);
661 if (!strcmp(head
, "HEAD")) {
665 if (prefixcmp(head
, "refs/heads/"))
666 die("HEAD not found below refs/heads!");
671 return delete_branches(argc
- i
, argv
+ i
, force_delete
, kinds
);
673 print_ref_list(kinds
, detached
, verbose
, abbrev
);
674 else if (rename
&& (i
== argc
- 1))
675 rename_branch(head
, argv
[i
], force_rename
);
676 else if (rename
&& (i
== argc
- 2))
677 rename_branch(argv
[i
], argv
[i
+ 1], force_rename
);
678 else if (i
== argc
- 1 || i
== argc
- 2)
679 create_branch(argv
[i
], (i
== argc
- 2) ? argv
[i
+1] : head
,
680 force_create
, reflog
, track
);
682 usage(builtin_branch_usage
);