On Fri, Jul 25, 2014 at 6:43 AM, Nguyễn Thái Ngọc Duy <[email protected]> wrote:
> In the beginning, we had resolve_ref() that returns a buffer owned by
> this function. Then we started to move away from that direction because
> the buffer could be overwritten by the next resolve_ref() call and
> introduced two new functions: resolve_ref_unsafe() and resolve_refdup().
> The static buffer is still kept internally.
>
> This patch makes the core of resolve_ref use a strbuf instead of static
> buffer. Which makes resolve_refdup() more efficient (no need to copy
> from the static buffer to a new buffer). It also removes the (random?)
> 256 char limit. In future, resolve_ref() could be used directly without
> going through resolve_refdup() wrapper.
>
> A minor bonus. resolve_ref(dup) are now more thread-friendly (although I'm
> not 100% sure if they are thread-safe yet).
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]>
> ---
> cache.h | 1 +
> refs.c | 122
> +++++++++++++++++++++++++++++++++++-----------------------------
> 2 files changed, 68 insertions(+), 55 deletions(-)
>
> diff --git a/cache.h b/cache.h
> index fcb511d..5ffbafb 100644
> --- a/cache.h
> +++ b/cache.h
> +const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int
> reading, int *flag)
> +{
> + static struct strbuf buf = STRBUF_INIT;
> + if (!resolve_ref(refname, &buf, sha1, reading, flag))
> + return buf.buf;
> + else
> + return NULL;
> }
>
> char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int
> *flag)
> {
> - const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
> - return ret ? xstrdup(ret) : NULL;
> + struct strbuf buf = STRBUF_INIT;
> + if (!resolve_ref(ref, &buf, sha1, reading, flag))
> + return buf.buf;
return strbuf_detach(&buf, NULL);
> + else {
> + strbuf_release(&buf);
> + return NULL;
> + }
> }
>
> /* The argument to filter_refs */
> --
> 1.9.1.346.ga2b5940
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html