* [PATCH 0/2] Enable git-branch to show detached HEAD
@ 2007-01-03 20:10 Lars Hjemli
[not found] ` <ec7218855930593c29c5ced76630ff7844947e6b.1167849110.git.hjemli@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Lars Hjemli @ 2007-01-03 20:10 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano
These two patches let git-branch show a detached HEAD in a sensible way:
[PATCH 1/2] Refactor print-functions in builtin-branch
[PATCH 2/2] git-branch: show detached HEAD
^ permalink raw reply [flat|nested] 3+ messages in thread[parent not found: <ec7218855930593c29c5ced76630ff7844947e6b.1167849110.git.hjemli@gmail.com>]
* [PATCH 1/2] Refactor print-functions in builtin-branch [not found] ` <ec7218855930593c29c5ced76630ff7844947e6b.1167849110.git.hjemli@gmail.com> @ 2007-01-03 20:10 ` Lars Hjemli [not found] ` <2ffb0289d95e442df58ba3c3082b31b81f59399c.1167849110.git.hjemli@gmail.com> 1 sibling, 0 replies; 3+ messages in thread From: Lars Hjemli @ 2007-01-03 20:10 UTC (permalink / raw) To: git; +Cc: Junio C Hamano This moves the guts of print_ref_list() into a revamped print_ref_info(), which at the same time gets renamed to print_ref_item(). Signed-off-by: Lars Hjemli <hjemli@gmail.com> --- builtin-branch.c | 83 ++++++++++++++++++++++++++---------------------------- 1 files changed, 40 insertions(+), 43 deletions(-) diff --git a/builtin-branch.c b/builtin-branch.c index 71f88f2..b9a8bae 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -231,29 +231,54 @@ static int ref_cmp(const void *r1, const void *r2) return strcmp(c1->name, c2->name); } -static void print_ref_info(const unsigned char *sha1, int abbrev) +static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, + int abbrev, int current) { + char c; + int color; struct commit *commit; char subject[256]; + switch (item->kind) { + case REF_LOCAL_BRANCH: + color = COLOR_BRANCH_LOCAL; + break; + case REF_REMOTE_BRANCH: + color = COLOR_BRANCH_REMOTE; + break; + default: + color = COLOR_BRANCH_PLAIN; + break; + } - commit = lookup_commit(sha1); - if (commit && !parse_commit(commit)) - pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, - subject, sizeof(subject), 0, - NULL, NULL, 0); - else - strcpy(subject, " **** invalid ref ****"); + c = ' '; + if (current) { + c = '*'; + color = COLOR_BRANCH_CURRENT; + } - printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject); + if (verbose) { + commit = lookup_commit(item->sha1); + if (commit && !parse_commit(commit)) + pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, + subject, sizeof(subject), 0, + NULL, NULL, 0); + else + strcpy(subject, " **** invalid ref ****"); + printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color), + maxwidth, item->name, + branch_get_color(COLOR_BRANCH_RESET), + find_unique_abbrev(item->sha1, abbrev), subject); + } else { + printf("%c %s%s%s\n", c, branch_get_color(color), item->name, + branch_get_color(COLOR_BRANCH_RESET)); + } } static void print_ref_list(int kinds, int verbose, int abbrev) { int i; - char c; struct ref_list ref_list; - int color; memset(&ref_list, 0, sizeof(ref_list)); ref_list.kinds = kinds; @@ -262,38 +287,10 @@ static void print_ref_list(int kinds, int verbose, int abbrev) qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); for (i = 0; i < ref_list.index; i++) { - switch( ref_list.list[i].kind ) { - case REF_LOCAL_BRANCH: - color = COLOR_BRANCH_LOCAL; - break; - case REF_REMOTE_BRANCH: - color = COLOR_BRANCH_REMOTE; - break; - default: - color = COLOR_BRANCH_PLAIN; - break; - } - - c = ' '; - if (ref_list.list[i].kind == REF_LOCAL_BRANCH && - !strcmp(ref_list.list[i].name, head)) { - c = '*'; - color = COLOR_BRANCH_CURRENT; - } - - if (verbose) { - printf("%c %s%-*s%s", c, - branch_get_color(color), - ref_list.maxwidth, - ref_list.list[i].name, - branch_get_color(COLOR_BRANCH_RESET)); - print_ref_info(ref_list.list[i].sha1, abbrev); - } - else - printf("%c %s%s%s\n", c, - branch_get_color(color), - ref_list.list[i].name, - branch_get_color(COLOR_BRANCH_RESET)); + int current = (ref_list.list[i].kind == REF_LOCAL_BRANCH) && + !strcmp(ref_list.list[i].name, head); + print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose, + abbrev, current); } free_ref_list(&ref_list); -- 1.5.0.rc0.ge900 ^ permalink raw reply related [flat|nested] 3+ messages in thread
[parent not found: <2ffb0289d95e442df58ba3c3082b31b81f59399c.1167849110.git.hjemli@gmail.com>]
* [PATCH 2/2] git-branch: show detached HEAD [not found] ` <2ffb0289d95e442df58ba3c3082b31b81f59399c.1167849110.git.hjemli@gmail.com> @ 2007-01-03 20:10 ` Lars Hjemli 0 siblings, 0 replies; 3+ messages in thread From: Lars Hjemli @ 2007-01-03 20:10 UTC (permalink / raw) To: git; +Cc: Junio C Hamano This makes git-branch show a detached HEAD as '* (no branch)'. Signed-off-by: Lars Hjemli <hjemli@gmail.com> --- builtin-branch.c | 23 +++++++++++++++++------ 1 files changed, 17 insertions(+), 6 deletions(-) diff --git a/builtin-branch.c b/builtin-branch.c index b9a8bae..f04718d 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -275,7 +275,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, } } -static void print_ref_list(int kinds, int verbose, int abbrev) +static void print_ref_list(int kinds, int detached, int verbose, int abbrev) { int i; struct ref_list ref_list; @@ -286,8 +286,20 @@ static void print_ref_list(int kinds, int verbose, int abbrev) qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); + detached = (detached && (kinds & REF_LOCAL_BRANCH)); + if (detached) { + struct ref_item item; + item.name = "(no branch)"; + item.kind = REF_LOCAL_BRANCH; + hashcpy(item.sha1, head_sha1); + if (strlen(item.name) > ref_list.maxwidth) + ref_list.maxwidth = strlen(item.name); + print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1); + } + for (i = 0; i < ref_list.index; i++) { - int current = (ref_list.list[i].kind == REF_LOCAL_BRANCH) && + int current = !detached && + (ref_list.list[i].kind == REF_LOCAL_BRANCH) && !strcmp(ref_list.list[i].name, head); print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose, abbrev, current); @@ -377,7 +389,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) { int delete = 0, force_delete = 0, force_create = 0; int rename = 0, force_rename = 0; - int verbose = 0, abbrev = DEFAULT_ABBREV; + int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0; int reflog = 0; int kinds = REF_LOCAL_BRANCH; int i; @@ -455,8 +467,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (!head) die("Failed to resolve HEAD as a valid ref."); if (!strcmp(head, "HEAD")) { - /* detached HEAD */ - ; + detached = 1; } else { if (strncmp(head, "refs/heads/", 11)) @@ -467,7 +478,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (delete) return delete_branches(argc - i, argv + i, force_delete, kinds); else if (i == argc) - print_ref_list(kinds, verbose, abbrev); + print_ref_list(kinds, detached, verbose, abbrev); else if (rename && (i == argc - 1)) rename_branch(head, argv[i], force_rename); else if (rename && (i == argc - 2)) -- 1.5.0.rc0.ge900 ^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-01-03 20:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-01-03 20:10 [PATCH 0/2] Enable git-branch to show detached HEAD Lars Hjemli
[not found] ` <ec7218855930593c29c5ced76630ff7844947e6b.1167849110.git.hjemli@gmail.com>
2007-01-03 20:10 ` [PATCH 1/2] Refactor print-functions in builtin-branch Lars Hjemli
[not found] ` <2ffb0289d95e442df58ba3c3082b31b81f59399c.1167849110.git.hjemli@gmail.com>
2007-01-03 20:10 ` [PATCH 2/2] git-branch: show detached HEAD Lars Hjemli
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).