@matheusbsbs / matheus-bagliano / commits / 0016a48251
Fork 0

git-branch: show detached HEAD

This makes git-branch show a detached HEAD as '* (no branch)'. Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Lars Hjemli committed Jan 3, 2007 at 21:10 UTC 0016a48251abefed11efc919703d980a21c95f2c
1 file changed +24 -7
builtin-branch.c
+24 -7
@@ -275,7 +275,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
275 }
276 }
277
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)
279 {
280 int i;
281 struct ref_list ref_list;
@@ -286,8 +286,20 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
286
287 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
288
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 +
300 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) &&
303 !strcmp(ref_list.list[i].name, head);
304 print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
305 abbrev, current);
@@ -367,7 +379,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
379 {
380 int delete = 0, force_delete = 0, force_create = 0;
381 int rename = 0, force_rename = 0;
370 - int verbose = 0, abbrev = DEFAULT_ABBREV;
382 + int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
383 int reflog = 0;
384 int kinds = REF_LOCAL_BRANCH;
385 int i;
@@ -444,14 +456,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
456 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
457 if (!head)
458 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 + }
467
468 if (delete)
469 return delete_branches(argc - i, argv + i, force_delete, kinds);
470 else if (i == argc)
454 - print_ref_list(kinds, verbose, abbrev);
471 + print_ref_list(kinds, detached, verbose, abbrev);
472 else if (rename && (i == argc - 1))
473 rename_branch(head, argv[i], force_rename);
474 else if (rename && (i == argc - 2))