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

[PATCH v2 2/9] apply: refactor read_file_or_gitlink()

From
Junio C Hamano <gitster@pobox.com>
Date
May 10, 2012, 22:32 UTC
Message-ID
<1336689173-15822-3-git-send-email-gitster@pobox.com>
In-Reply-To
<1336689173-15822-1-git-send-email-gitster@pobox.com>

Reading a blob out of the object store does not have to require that the caller has a cache entry for it.

Create a read_blob_object() helper function that takes the object name and mode, and use it to reimplement the original function as a thin wrapper to it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/apply.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 99b1608..ca330e3 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2930,20 +2930,17 @@ static int apply_fragments(struct image *img, struct patch *patch)
 	return 0;
 }
 
-static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
+static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
 {
-	if (!ce)
-		return 0;
-
-	if (S_ISGITLINK(ce->ce_mode)) {
+	if (S_ISGITLINK(mode)) {
 		strbuf_grow(buf, 100);
-		strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
+		strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
 	} else {
 		enum object_type type;
 		unsigned long sz;
 		char *result;
 
-		result = read_sha1_file(ce->sha1, &type, &sz);
+		result = read_sha1_file(sha1, &type, &sz);
 		if (!result)
 			return -1;
 		/* XXX read_sha1_file NUL-terminates */
@@ -2952,6 +2949,13 @@ static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
 	return 0;
 }
 
+static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
+{
+	if (!ce)
+		return 0;
+	return read_blob_object(buf, ce->sha1, ce->ce_mode);
+}
+
 static struct patch *in_fn_table(const char *name)
 {
 	struct string_list_item *item;
-- 
1.7.10.1.574.g840b38f
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 10 in “"git apply --3way"”
  1. 0/9 "git apply --3way"Junio C Hamano, May 10, 2012
  2. 1/9 apply: clear_image() clears things a bit moreJunio C Hamano, May 10, 2012
  3. 2/9 apply: refactor read_file_or_gitlink()Junio C Hamano, May 10, 2012
  4. 3/9 apply: split load_preimage() helper function outJunio C Hamano, May 10, 2012
  5. 4/9 apply: accept -3/--3way command line optionJunio C Hamano, May 10, 2012
  6. 5/9 apply: fall back on three-way mergeJunio C Hamano, May 10, 2012
  7. 6/9 apply: plug the three-way merge logic inJunio C Hamano, May 10, 2012
  8. 7/9 apply: register conflicted stages to the indexJunio C Hamano, May 10, 2012
  9. 8/9 apply: allow rerere() upon --3way resultsJunio C Hamano, May 10, 2012
  10. 9/9 apply: document --3way optionJunio C Hamano, May 10, 2012

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.