git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/4] refs.c: refactor resolve_ref_unsafe() to use strbuf internally

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 25, 2014, 15:55 UTC
Message-ID
<CAPig+cSvymyM3G+LujopPAVtaXVYN_mYJ0pxPkvb2pvLsq+e-A@mail.gmail.com>
In-Reply-To
<1406285039-22469-3-git-send-email-pclouds@gmail.com>
On Fri, Jul 25, 2014 at 6:43 AM, Nguyễn Thái Ngọc Duy <pclouds@gmail.com> wrote:
Show 41 quoted lines
> 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 <pclouds@gmail.com>
> ---
>  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);
Show 9 quoted lines
> +       else {
> +               strbuf_release(&buf);
> +               return NULL;
> +       }
>  }
>
>  /* The argument to filter_refs */
> --
> 1.9.1.346.ga2b5940
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 6 of 11 in “Consolidate ref parsing code”
  1. 0/4 Consolidate ref parsing codeNguyễn Thái Ngọc Duy, Jul 25, 2014
  2. 1/4 strbuf.c: keep errno in strbuf_read_file()Nguyễn Thái Ngọc Duy, Jul 25, 2014
  3. Eric SunshineJul 25, 2014
  4. Michael HaggertySep 26, 2014
  5. 2/4 refs.c: refactor resolve_ref_unsafe() to use strbuf internallyNguyễn Thái Ngọc Duy, Jul 25, 2014
  6. Eric SunshineJul 25, 2014
  7. Junio C HamanoJul 30, 2014
  8. 3/4 refs.c: move ref parsing code out of resolve_ref()Nguyễn Thái Ngọc Duy, Jul 25, 2014
  9. Ronnie SahlbergJul 25, 2014
  10. Duy NguyenJul 26, 2014
  11. 4/4 refs.c: rewrite resolve_gitlink_ref() to use parse_ref()Nguyễn Thái Ngọc Duy, Jul 25, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.