Skip to content

Commit

Permalink
remote: improve sorting of "configure for git push" list
Browse files Browse the repository at this point in the history
The data structure used to store this list is a string_list
of sources with the destination in the util member. The
current code just sorts on the source; if a single source is
pushed to two different destination refs at a remote, then
the order in which they are printed is non-deterministic.

This patch implements a comparison using both fields.
Besides being a little nicer on the eyes, giving a stable
sort prevents false negatives in the test suite when
comparing output.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Mar 22, 2009
1 parent 8321c56 commit 48ef563
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion builtin-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,20 @@ int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
return 0;
}

/*
* Sorting comparison for a string list that has push_info
* structs in its util field
*/
static int cmp_string_with_push(const void *va, const void *vb)
{
const struct string_list_item *a = va;
const struct string_list_item *b = vb;
const struct push_info *a_push = a->util;
const struct push_info *b_push = b->util;
int cmp = strcmp(a->string, b->string);
return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
}

int show_push_info_item(struct string_list_item *item, void *cb_data)
{
struct show_info *show_info = cb_data;
Expand Down Expand Up @@ -1041,7 +1055,8 @@ static int show(int argc, const char **argv)

info.width = info.width2 = 0;
for_each_string_list(add_push_to_show_info, &states.push, &info);
sort_string_list(info.list);
qsort(info.list->items, info.list->nr,
sizeof(*info.list->items), cmp_string_with_push);
if (info.list->nr)
printf(" Local ref%s configured for 'git push'%s:\n",
info.list->nr > 1 ? "s" : "",
Expand Down

0 comments on commit 48ef563

Please sign in to comment.