Re: [PATCH 1/5] fetch-pack: sort incoming heads
- From
Junio C Hamano <gitster@pobox.com>
- Date
- May 22, 2012, 20:08 UTC
- Message-ID
- <7v7gw43rn9.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <20120521221702.GA22914@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 39 quoted lines
> There's no reason to preserve the incoming order of the
> heads we're requested to fetch. By having them sorted, we
> can replace some of the quadratic algorithms with linear
> ones.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> I actually wouldn't be surprised if these were typically sorted already,
> as they frequently come from the ref-mapping functions, which in turn
> process the lists we get from the remote. But we also might get random
> junk on the command-line of fetch-pack, so we need to be careful.
>
> builtin/fetch-pack.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
> index 10db15b..380743e 100644
> --- a/builtin/fetch-pack.c
> +++ b/builtin/fetch-pack.c
> @@ -1057,6 +1057,11 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
> return ret;
> }
>
> +static int compare_heads(const void *a, const void *b)
> +{
> + return strcmp(*(const char **)a, *(const char **)b);
> +}
> +
> struct ref *fetch_pack(struct fetch_pack_args *my_args,
> int fd[], struct child_process *conn,
> const struct ref *ref,
> @@ -1076,6 +1081,8 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
> st.st_mtime = 0;
> }
>
> + qsort(heads, nr_heads, sizeof(*heads), compare_heads);
> +
> if (heads && nr_heads)
> nr_heads = remove_duplicates(nr_heads, heads);Hrm, could heads and/or nr_heads be NULL/0 here when we try to run qsort() in this codepath?
> if (!ref) {