@matheusbsbs / matheus-bagliano / commits / 7e9ff00bbe
Fork 0

git branch: clean up detached branch handling

Make the 'show detached branch info' a routine of its own. And in the process, avoid the object lookup that is unnecessary if the current branch isn't detached. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Jul 23, 2009 at 12:13 UTC 7e9ff00bbe1f437ff492a714758a4f7360feb22b
1 file changed +23 -15
builtin-branch.c
+23 -15
@@ -191,7 +191,7 @@ struct ref_item {
191
192 struct ref_list {
193 struct rev_info revs;
194 - int index, alloc, maxwidth, verbose;
194 + int index, alloc, maxwidth, verbose, abbrev;
195 struct ref_item *list;
196 struct commit_list *with_commit;
197 int kinds;
@@ -418,15 +418,34 @@ static int calc_maxwidth(struct ref_list *refs)
418 return w;
419 }
420
421 +
422 +static void show_detached(struct ref_list *ref_list)
423 +{
424 + struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
425 +
426 + if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
427 + struct ref_item item;
428 + item.name = xstrdup("(no branch)");
429 + item.len = strlen(item.name);
430 + item.kind = REF_LOCAL_BRANCH;
431 + item.dest = NULL;
432 + item.commit = head_commit;
433 + if (item.len > ref_list->maxwidth)
434 + ref_list->maxwidth = item.len;
435 + print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
436 + free(item.name);
437 + }
438 +}
439 +
440 static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
441 {
442 int i;
443 struct ref_list ref_list;
425 - struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
444
445 memset(&ref_list, 0, sizeof(ref_list));
446 ref_list.kinds = kinds;
447 ref_list.verbose = verbose;
448 + ref_list.abbrev = abbrev;
449 ref_list.with_commit = with_commit;
450 if (merge_filter != NO_FILTER)
451 init_revisions(&ref_list.revs, NULL);
@@ -446,19 +465,8 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
465 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
466
467 detached = (detached && (kinds & REF_LOCAL_BRANCH));
449 - if (detached && head_commit &&
450 - is_descendant_of(head_commit, with_commit)) {
451 - struct ref_item item;
452 - item.name = xstrdup("(no branch)");
453 - item.len = strlen(item.name);
454 - item.kind = REF_LOCAL_BRANCH;
455 - item.dest = NULL;
456 - item.commit = head_commit;
457 - if (item.len > ref_list.maxwidth)
458 - ref_list.maxwidth = item.len;
459 - print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1, "");
460 - free(item.name);
461 - }
468 + if (detached)
469 + show_detached(&ref_list);
470
471 for (i = 0; i < ref_list.index; i++) {
472 int current = !detached &&