Skip to content

Commit 0016a48

Browse files
hjemliJunio C Hamano
authored andcommitted
git-branch: show detached HEAD
This makes git-branch show a detached HEAD as '* (no branch)'. Signed-off-by: Lars Hjemli <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bda324c commit 0016a48

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

builtin-branch.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
275275
}
276276
}
277277

278-
static void print_ref_list(int kinds, int verbose, int abbrev)
278+
static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
279279
{
280280
int i;
281281
struct ref_list ref_list;
@@ -286,8 +286,20 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
286286

287287
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
288288

289+
detached = (detached && (kinds & REF_LOCAL_BRANCH));
290+
if (detached) {
291+
struct ref_item item;
292+
item.name = "(no branch)";
293+
item.kind = REF_LOCAL_BRANCH;
294+
hashcpy(item.sha1, head_sha1);
295+
if (strlen(item.name) > ref_list.maxwidth)
296+
ref_list.maxwidth = strlen(item.name);
297+
print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
298+
}
299+
289300
for (i = 0; i < ref_list.index; i++) {
290-
int current = (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
301+
int current = !detached &&
302+
(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
291303
!strcmp(ref_list.list[i].name, head);
292304
print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
293305
abbrev, current);
@@ -367,7 +379,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
367379
{
368380
int delete = 0, force_delete = 0, force_create = 0;
369381
int rename = 0, force_rename = 0;
370-
int verbose = 0, abbrev = DEFAULT_ABBREV;
382+
int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
371383
int reflog = 0;
372384
int kinds = REF_LOCAL_BRANCH;
373385
int i;
@@ -444,14 +456,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
444456
head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
445457
if (!head)
446458
die("Failed to resolve HEAD as a valid ref.");
447-
if (strncmp(head, "refs/heads/", 11))
448-
die("HEAD not found below refs/heads!");
449-
head += 11;
459+
if (!strcmp(head, "HEAD")) {
460+
detached = 1;
461+
}
462+
else {
463+
if (strncmp(head, "refs/heads/", 11))
464+
die("HEAD not found below refs/heads!");
465+
head += 11;
466+
}
450467

451468
if (delete)
452469
return delete_branches(argc - i, argv + i, force_delete, kinds);
453470
else if (i == argc)
454-
print_ref_list(kinds, verbose, abbrev);
471+
print_ref_list(kinds, detached, verbose, abbrev);
455472
else if (rename && (i == argc - 1))
456473
rename_branch(head, argv[i], force_rename);
457474
else if (rename && (i == argc - 2))

0 commit comments

Comments
 (0)