branch: show "HEAD detached" first under reverse sort

Change the output of the likes of "git branch -l --sort=-objectsize" to show the "(HEAD detached at <hash>)" message at the start of the output. Before the compare_detached_head() function added in a preceding commit we'd emit this output as an emergent effect. It doesn't make any sense to consider the objectsize, type or other non-attribute of the "(HEAD detached at <hash>)" message for the purposes of sorting. Let's always emit it at the top instead. The only reason it was sorted in the first place is because we're injecting it into the ref-filter machinery so builtin/branch.c doesn't need to do its own "am I detached?" detection. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 7, 2021 at 10:51 UTC 4045f659bdccb5108800bdc2ec96bc6f3945ff40
2 files changed +7 -4
ref-filter.c
+4 -1
@@ -2357,6 +2357,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
2357 {
2358 struct atom_value *va, *vb;
2359 int cmp;
2360 + int cmp_detached_head = 0;
2361 cmp_type cmp_type = used_atom[s->atom].type;
2362 struct strbuf err = STRBUF_INIT;
2363
@@ -2368,6 +2369,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
2369 if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
2370 ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
2371 cmp = compare_detached_head(a, b);
2372 + cmp_detached_head = 1;
2373 } else if (s->sort_flags & REF_SORTING_VERSION) {
2374 cmp = versioncmp(va->s, vb->s);
2375 } else if (cmp_type == FIELD_STR) {
@@ -2384,7 +2386,8 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
2386 cmp = 1;
2387 }
2388
2387 - return (s->sort_flags & REF_SORTING_REVERSE) ? -cmp : cmp;
2389 + return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
2390 + ? -cmp : cmp;
2391 }
2392
2393 static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
t/t3203-branch-output.sh
+3 -3
@@ -221,10 +221,10 @@ test_expect_success 'git branch `--sort=[-]objectsize` option' '
221 test_i18ncmp expect actual &&
222
223 cat >expect <<-\EOF &&
224 + * (HEAD detached from fromtag)
225 branch-one
226 main
227 branch-two
227 - * (HEAD detached from fromtag)
228 EOF
229 git branch --sort=-objectsize >actual &&
230 test_i18ncmp expect actual
@@ -241,10 +241,10 @@ test_expect_success 'git branch `--sort=[-]type` option' '
241 test_i18ncmp expect actual &&
242
243 cat >expect <<-\EOF &&
244 + * (HEAD detached from fromtag)
245 branch-one
246 branch-two
247 main
247 - * (HEAD detached from fromtag)
248 EOF
249 git branch --sort=-type >actual &&
250 test_i18ncmp expect actual
@@ -261,10 +261,10 @@ test_expect_success 'git branch `--sort=[-]version:refname` option' '
261 test_i18ncmp expect actual &&
262
263 cat >expect <<-\EOF &&
264 + * (HEAD detached from fromtag)
265 main
266 branch-two
267 branch-one
267 - * (HEAD detached from fromtag)
268 EOF
269 git branch --sort=-version:refname >actual &&
270 test_i18ncmp expect actual