git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>
Cc: "David Turner" <novalis@novalis.org>,
	"Ramsay Jones" <ramsay@ramsayjones.plus.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Jeff King" <peff@peff.net>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	git@vger.kernel.org, "Michael Haggerty" <mhagger@alum.mit.edu>
Subject: [PATCH v2 16/38] resolve_gitlink_ref(): implement using resolve_ref_recursively()
Date: Sun,  4 Sep 2016 18:08:22 +0200	[thread overview]
Message-ID: <1e7e45a2a96e1ae7d45a1072c3c965bc7c02105c.1473003903.git.mhagger@alum.mit.edu> (raw)
In-Reply-To: <cover.1473003902.git.mhagger@alum.mit.edu>

resolve_ref_recursively() can handle references in arbitrary files
reference stores, so use it to resolve "gitlink" (i.e., submodule)
references. Aside from removing redundant code, this allows submodule
lookups to benefit from the much more robust code that we use for
reading non-submodule references. And, since the code is now agnostic
about reference backends, it will work for any future references
backend (so move its definition to refs.c).

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 refs.c               | 24 +++++++++++++++++++
 refs/files-backend.c | 67 ----------------------------------------------------
 2 files changed, 24 insertions(+), 67 deletions(-)

diff --git a/refs.c b/refs.c
index 3723169..647ead5 100644
--- a/refs.c
+++ b/refs.c
@@ -1299,6 +1299,30 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
 				       resolve_flags, sha1, flags);
 }
 
+int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
+{
+	int len = strlen(path);
+	struct strbuf submodule = STRBUF_INIT;
+	struct ref_store *refs;
+	int flags;
+
+	while (len && path[len-1] == '/')
+		len--;
+	if (!len)
+		return -1;
+
+	strbuf_add(&submodule, path, len);
+	refs = get_ref_store(submodule.buf);
+	strbuf_release(&submodule);
+	if (!refs)
+		return -1;
+
+	if (!resolve_ref_recursively(refs, refname, 0, sha1, &flags) ||
+	    is_null_sha1(sha1))
+		return -1;
+	return 0;
+}
+
 /* A pointer to the ref_store for the main repository: */
 static struct ref_store *main_ref_store;
 
diff --git a/refs/files-backend.c b/refs/files-backend.c
index ce6fe94..6382c6b 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1494,73 +1494,6 @@ static void unlock_ref(struct ref_lock *lock)
 	free(lock);
 }
 
-#define MAXREFLEN (1024)
-
-static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,
-					 const char *refname, unsigned char *sha1,
-					 int recursion)
-{
-	int fd, len;
-	char buffer[128], *p;
-	char *path;
-
-	if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
-		return -1;
-	path = *refs->base.submodule
-		? git_pathdup_submodule(refs->base.submodule, "%s", refname)
-		: git_pathdup("%s", refname);
-	fd = open(path, O_RDONLY);
-	free(path);
-	if (fd < 0) {
-		unsigned int flags;
-
-		return resolve_packed_ref(refs, refname, sha1, &flags);
-	}
-
-	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);
-	struct strbuf submodule = STRBUF_INIT;
-	struct files_ref_store *refs;
-
-	while (len && path[len-1] == '/')
-		len--;
-	if (!len)
-		return -1;
-
-	strbuf_add(&submodule, path, len);
-	refs = get_files_ref_store(submodule.buf, "resolve_gitlink_ref");
-	if (!refs) {
-		strbuf_release(&submodule);
-		return -1;
-	}
-	strbuf_release(&submodule);
-
-	return resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
-}
-
 /*
  * Lock refname, without following symrefs, and set *lock_p to point
  * at a newly-allocated lock object. Fill in lock->old_oid, referent,
-- 
2.9.3


  parent reply	other threads:[~2016-09-04 16:10 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-04 16:08 [PATCH v2 00/38] Virtualization of the refs API Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 01/38] resolve_gitlink_ref(): eliminate temporary variable Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 02/38] rename_ref_available(): add docstring Michael Haggerty
2016-09-06 14:25   ` Jakub Narębski
2016-09-07  4:32     ` Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 03/38] refs: rename struct ref_cache to files_ref_store Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 04/38] refs: add a backend method structure Michael Haggerty
2016-09-04 19:43   ` David Turner
2016-09-04 16:08 ` [PATCH v2 05/38] refs: create a base class "ref_store" for files_ref_store Michael Haggerty
2016-09-04 20:40   ` David Turner
2016-09-05  3:53     ` Michael Haggerty
2016-09-05  5:17       ` Stefan Beller
2016-09-05 16:16       ` David Turner
2016-09-04 16:08 ` [PATCH v2 06/38] add_packed_ref(): add a files_ref_store argument Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 07/38] get_packed_ref(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 08/38] resolve_missing_loose_ref(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 09/38] {lock,commit,rollback}_packed_refs(): add files_ref_store arguments Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 10/38] refs: add a transaction_commit() method Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 11/38] refs: reorder definitions Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 12/38] resolve_packed_ref(): rename function from resolve_missing_loose_ref() Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 13/38] resolve_gitlink_packed_ref(): remove function Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 14/38] read_raw_ref(): take a (struct ref_store *) argument Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 15/38] resolve_ref_recursively(): new function Michael Haggerty
2016-09-04 16:08 ` Michael Haggerty [this message]
2016-09-04 16:08 ` [PATCH v2 17/38] resolve_gitlink_ref(): avoid memory allocation in many cases Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 18/38] resolve_gitlink_ref(): rename path parameter to submodule Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 19/38] refs: make read_raw_ref() virtual Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 20/38] refs: make verify_refname_available() virtual Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 21/38] refs: make pack_refs() virtual Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 22/38] refs: make create_symref() virtual Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 23/38] refs: make peel_ref() virtual Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 24/38] repack_without_refs(): add a files_ref_store argument Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 25/38] lock_raw_ref(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 26/38] commit_ref_update(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 27/38] lock_ref_for_update(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 28/38] lock_ref_sha1_basic(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 29/38] split_symref_update(): " Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 30/38] files_ref_iterator_begin(): take a ref_store argument Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 31/38] refs: add method iterator_begin Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 32/38] refs: add methods for reflog Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 33/38] refs: add method for initial ref transaction commit Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 34/38] refs: make delete_refs() virtual Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 35/38] refs: add methods to init refs db Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 36/38] refs: add method to rename refs Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 37/38] refs: make lock generic Michael Haggerty
2016-09-04 16:08 ` [PATCH v2 38/38] refs: implement iteration over only per-worktree refs Michael Haggerty
2016-09-08 21:45   ` David Turner
2016-09-07 19:20 ` [PATCH v2 00/38] Virtualization of the refs API Junio C Hamano
2016-09-08  6:42   ` Michael Haggerty

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1e7e45a2a96e1ae7d45a1072c3c965bc7c02105c.1473003903.git.mhagger@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=novalis@novalis.org \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=ramsay@ramsayjones.plus.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).