From: Karthik Nayak Date: Wed, 16 Sep 2015 06:23:15 GMT Subject: Re: [PATCH v4 3/8] branch: roll show_detached HEAD into regular ref_list Message-ID: In-Reply-To: On Tue, Sep 15, 2015 at 1:05 AM, Junio C Hamano wrote: > Karthik Nayak writes: > >> + /* >> + * First we obtain all regular branch refs and if the HEAD is >> + * detached then we insert that ref to the end of the ref_fist >> + * so that it can be printed and removed first. >> + */ >> for_each_rawref(append_ref, &cb); >> + if (detached) >> + head_ref(append_ref, &cb); >> + index = ref_list.index; >> + >> + /* Print detached HEAD before sorting and printing the rest */ >> + if (detached && (ref_list.list[index - 1].kind == REF_DETACHED_HEAD) && >> + !strcmp(ref_list.list[index - 1].name, head)) { >> + print_ref_item(&ref_list.list[index - 1], maxwidth, verbose, abbrev, >> + 1, remote_prefix); >> + index -= 1; >> + } >> >> + qsort(ref_list.list, index, sizeof(struct ref_item), ref_cmp); > > This looks somewhat strange. Wouldn't it be more consistent to > teach ref_cmp that HEAD sorts where in the collection of refs (I > presume that kind is checked first and then name, so if you give > REF_DETACHED_HEAD a low number than others, it would automatically > give you the ordering you want) without all of the above special > casing? Thats nice, we could do that, something like this perhaps: qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); for (i = 0; i < ref_list.index; i++) { int current = !detached && (ref_list.list[i].kind == REF_LOCAL_BRANCH) && !strcmp(ref_list.list[i].name, head); /* If detached the first ref_item is the current ref */ if (detached && i == 0) current = 1; print_ref_item(&ref_list.list[i], maxwidth, verbose, abbrev, current, remote_prefix); } -- Regards, Karthik Nayak