From: Michael Haggerty <mhagger@alum.mit.edu>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
"Junio C Hamano" <gitster@pobox.com>
Cc: Eric Sunshine <sunshine@sunshineco.com>,
Ronnie Sahlberg <sahlberg@google.com>,
git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH v2 09/11] refs.c: rewrite resolve_gitlink_ref() to use parse_ref()
Date: Wed, 15 Oct 2014 17:06:21 +0200 [thread overview]
Message-ID: <1413385583-4872-10-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1413385583-4872-1-git-send-email-mhagger@alum.mit.edu>
From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
resolve_gitlink_ref_recursive() did about the same thing as
parse_ref(), but didn't know as many tricks. It also had another
random limit of 128 bytes for symrefs. So base resolve_gitlink_ref()
on parse_ref() instead.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
refs.c | 69 ++++++++++++++++++++++++++----------------------------------------
1 file changed, 27 insertions(+), 42 deletions(-)
diff --git a/refs.c b/refs.c
index e1aa6a4..a7c8abd 100644
--- a/refs.c
+++ b/refs.c
@@ -1299,48 +1299,11 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,
return 0;
}
-static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
- const char *refname, unsigned char *sha1,
- int recursion)
-{
- int fd, len;
- char buffer[128], *p;
- char *path;
-
- if (recursion > MAXDEPTH)
- return -1;
- path = *refs->name
- ? git_path_submodule(refs->name, "%s", refname)
- : git_path("%s", refname);
- fd = open(path, O_RDONLY);
- if (fd < 0)
- return resolve_gitlink_packed_ref(refs, refname, sha1);
-
- len = read(fd, buffer, sizeof(buffer)-1);
- close(fd);
- if (len < 0)
- return -1;
- while (len && isspace(buffer[len-1]))
- len--;
- buffer[len] = 0;
-
- /* Was it a detached head or an old-fashioned symlink? */
- if (!get_sha1_hex(buffer, sha1))
- return 0;
-
- /* Symref? */
- if (strncmp(buffer, "ref:", 4))
- return -1;
- p = buffer + 4;
- while (isspace(*p))
- p++;
-
- return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
-}
-
int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
{
- int len = strlen(path), retval;
+ struct strbuf result = STRBUF_INIT;
+ int len = strlen(path), parseval, ret;
+ int depth = MAXDEPTH;
char *submodule;
struct ref_cache *refs;
@@ -1352,8 +1315,30 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
refs = get_ref_cache(submodule);
free(submodule);
- retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
- return retval;
+ strbuf_addstr(&result, refname);
+ do {
+ if (--depth < 0) {
+ errno = ELOOP;
+ ret = -1;
+ goto out;
+ }
+ path = *refs->name
+ ? git_path_submodule(refs->name, "%s", result.buf)
+ : git_path("%s", result.buf);
+ parseval = parse_ref(path, &result, sha1, NULL);
+ } while (!parseval);
+
+ if (parseval == 1) {
+ ret = 0;
+ } else if (parseval == -2) {
+ ret = resolve_gitlink_packed_ref(refs, result.buf, sha1) ? -1 : 0;
+ } else {
+ ret = -1;
+ }
+
+out:
+ strbuf_release(&result);
+ return ret;
}
/*
--
2.1.1
next prev parent reply other threads:[~2014-10-15 15:06 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-15 15:06 [PATCH v2 00/11] Consolidate ref parsing code Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 01/11] strbuf_read_file(): preserve errno on failure Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 02/11] handle_missing_loose_ref(): return an int Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 03/11] resolve_ref_unsafe(): reverse the logic of the symref conditional Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 04/11] resolve_ref_unsafe(): use skip_prefix() to skip over "ref:" Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 05/11] refs.c: refactor resolve_ref_unsafe() to use strbuf internally Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 06/11] refs.c: move ref parsing code out of resolve_ref() Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 07/11] handle_missing_loose_ref(): inline function Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 08/11] resolve_gitlink_ref_recursive(): drop arbitrary refname length limit Michael Haggerty
2014-10-15 15:06 ` Michael Haggerty [this message]
2014-10-15 15:06 ` [PATCH v2 10/11] resove_gitlink_packed_ref(): inline function Michael Haggerty
2014-10-15 15:06 ` [PATCH v2 11/11] resolve_gitlink_ref(): remove redundant test Michael Haggerty
2014-10-16 20:47 ` [PATCH v2 00/11] Consolidate ref parsing code Junio C Hamano
2014-10-16 21:51 ` Junio C Hamano
2014-10-16 23:23 ` Michael Haggerty
2014-10-16 23:53 ` Duy Nguyen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1413385583-4872-10-git-send-email-mhagger@alum.mit.edu \
--to=mhagger@alum.mit.edu \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=sahlberg@google.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).