Skip to content

Commit 4045f65

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2708ce6 commit 4045f65

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

ref-filter.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
23572357
{
23582358
struct atom_value *va, *vb;
23592359
int cmp;
2360+
int cmp_detached_head = 0;
23602361
cmp_type cmp_type = used_atom[s->atom].type;
23612362
struct strbuf err = STRBUF_INIT;
23622363

@@ -2368,6 +2369,7 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
23682369
if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
23692370
((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
23702371
cmp = compare_detached_head(a, b);
2372+
cmp_detached_head = 1;
23712373
} else if (s->sort_flags & REF_SORTING_VERSION) {
23722374
cmp = versioncmp(va->s, vb->s);
23732375
} 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
23842386
cmp = 1;
23852387
}
23862388

2387-
return (s->sort_flags & REF_SORTING_REVERSE) ? -cmp : cmp;
2389+
return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
2390+
? -cmp : cmp;
23882391
}
23892392

23902393
static int compare_refs(const void *a_, const void *b_, void *ref_sorting)

t/t3203-branch-output.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ test_expect_success 'git branch `--sort=[-]objectsize` option' '
221221
test_i18ncmp expect actual &&
222222
223223
cat >expect <<-\EOF &&
224+
* (HEAD detached from fromtag)
224225
branch-one
225226
main
226227
branch-two
227-
* (HEAD detached from fromtag)
228228
EOF
229229
git branch --sort=-objectsize >actual &&
230230
test_i18ncmp expect actual
@@ -241,10 +241,10 @@ test_expect_success 'git branch `--sort=[-]type` option' '
241241
test_i18ncmp expect actual &&
242242
243243
cat >expect <<-\EOF &&
244+
* (HEAD detached from fromtag)
244245
branch-one
245246
branch-two
246247
main
247-
* (HEAD detached from fromtag)
248248
EOF
249249
git branch --sort=-type >actual &&
250250
test_i18ncmp expect actual
@@ -261,10 +261,10 @@ test_expect_success 'git branch `--sort=[-]version:refname` option' '
261261
test_i18ncmp expect actual &&
262262
263263
cat >expect <<-\EOF &&
264+
* (HEAD detached from fromtag)
264265
main
265266
branch-two
266267
branch-one
267-
* (HEAD detached from fromtag)
268268
EOF
269269
git branch --sort=-version:refname >actual &&
270270
test_i18ncmp expect actual

0 commit comments

Comments
 (0)