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

[PATCH v0 2/3] index_fd(): split into two helper functions

From
Junio C Hamano <gitster@pobox.com>
Date
May 8, 2011, 08:47 UTC
Message-ID
<1304844455-23570-3-git-send-email-gitster@pobox.com>
In-Reply-To
<1304844455-23570-1-git-send-email-gitster@pobox.com>

Split out the case where we do not know the size of the input (hence we read everything into a strbuf before doing anything) to index_pipe(), and the other case where we mmap or read the whole data to index_bulk().

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 sha1_file.c |   42 +++++++++++++++++++++++++++++++-----------
 1 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 17c179c..49416b0 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2619,22 +2619,29 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
 	return ret;
 }
 
+static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
+		      const char *path, unsigned flags)
+{
+	struct strbuf sbuf = STRBUF_INIT;
+	int ret;
+
+	if (strbuf_read(&sbuf, fd, 4096) >= 0)
+		ret = index_mem(sha1, sbuf.buf, sbuf.len, type,	path, flags);
+	else
+		ret = -1;
+	strbuf_release(&sbuf);
+	return ret;
+}
+
 #define SMALL_FILE_SIZE (32*1024)
 
-int index_fd(unsigned char *sha1, int fd, struct stat *st,
-	     enum object_type type, const char *path, unsigned flags)
+static int index_core(unsigned char *sha1, int fd, size_t size,
+		      enum object_type type, const char *path,
+		      unsigned flags)
 {
 	int ret;
-	size_t size = xsize_t(st->st_size);
 
-	if (!S_ISREG(st->st_mode)) {
-		struct strbuf sbuf = STRBUF_INIT;
-		if (strbuf_read(&sbuf, fd, 4096) >= 0)
-			ret = index_mem(sha1, sbuf.buf, sbuf.len, type,	path, flags);
-		else
-			ret = -1;
-		strbuf_release(&sbuf);
-	} else if (!size) {
+	if (!size) {
 		ret = index_mem(sha1, NULL, size, type, path, flags);
 	} else if (size <= SMALL_FILE_SIZE) {
 		char *buf = xmalloc(size);
@@ -2648,6 +2655,19 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
 		ret = index_mem(sha1, buf, size, type, path, flags);
 		munmap(buf, size);
 	}
+	return ret;
+}
+
+int index_fd(unsigned char *sha1, int fd, struct stat *st,
+	     enum object_type type, const char *path, unsigned flags)
+{
+	int ret;
+	size_t size = xsize_t(st->st_size);
+
+	if (!S_ISREG(st->st_mode))
+		ret = index_pipe(sha1, fd, type, path, flags);
+	else
+		ret = index_core(sha1, fd, size, type, path, flags);
 	close(fd);
 	return ret;
 }
-- 
1.7.5.1.268.gce5bd
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 18 in “git add a-Big-file”
  1. 0/3 git add a-Big-fileJunio C Hamano, May 8, 2011
  2. 1/3 index_fd(): turn write_object and format_check arguments into one flagJunio C Hamano, May 8, 2011
  3. 2/3 index_fd(): split into two helper functionsJunio C Hamano, May 8, 2011
  4. 3/3 Bigfile: teach "git add" to send a large file straight to a packJunio C Hamano, May 8, 2011
  5. Nguyen Thai Ngoc DuyMay 8, 2011
  6. Junio C HamanoMay 8, 2011
  7. 0/4 convert.c clean-upJunio C Hamano, May 9, 2011
  8. 1/4 convert: rename the "eol" global variable to "core_eol"Junio C Hamano, May 9, 2011
  9. 2/4 convert: give saner names to crlf/eol variables, types and functionsJunio C Hamano, May 9, 2011
  10. 3/4 convert: make it safer to add conversion attributesJunio C Hamano, May 9, 2011
  11. 4/4 convert: make it harder to screw up adding a conversion attributeJunio C Hamano, May 9, 2011
  12. Nguyen Thai Ngoc DuyMay 10, 2011
  13. Junio C HamanoMay 10, 2011
  14. Shawn PearceMay 9, 2011
  15. Junio C HamanoMay 9, 2011
  16. Junio C HamanoMay 9, 2011
  17. Ævar Arnfjörð BjarmasonMay 29, 2011
  18. Junio C HamanoMay 29, 2011

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.