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

[PATCH] 2/7 Prepare for the next two patches

From
Bryan Larsen <bryan.larsen@gmail.com>
Date
Jul 8, 2005, 10:54 UTC
Message-ID
<42CE5B56.5090602@gmail.com>

Prepare for the next two patches. This patch makes the first half of write_sha1_file() and index_fd() externally visible. The first half of these two functions is basically just an SHA1 calculation, so there is probably a better way to do this. However, doing things this way makes this patch and the subsequent patches minimally intrusive.

Signed-off-by: Bryan Larsen <bryan.larsen@gmail.com>
---
  cache.h        |    3 ++-
  sha1_file.c    |   13 +++++++++----
  update-cache.c |    2 +-
  write-blob.c   |    2 +-
  4 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/cache.h b/cache.h
--- a/cache.h
+++ b/cache.h
@@ -139,7 +139,7 @@ extern int remove_cache_entry_at(int pos
  extern int remove_file_from_cache(char *path);
  extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
  extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
-extern int index_fd(unsigned char *sha1, int fd, struct stat *st);
+extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int 
info_only);
  extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);

  struct cache_file {
@@ -169,6 +169,7 @@ extern int sha1_object_info(const unsign
  extern void * unpack_sha1_file(void *map, unsigned long mapsize, char 
*type, unsigned long *size);
  extern void * read_sha1_file(const unsigned char *sha1, char *type, 
unsigned long *size);
  extern int write_sha1_file(void *buf, unsigned long len, const char 
*type, unsigned char *return_sha1);
+extern char *write_sha1_file_prepare(void *buf, unsigned long len, 
const char *type, unsigned char *sha1, unsigned char *hdr, int *hdrlen);

  extern int check_sha1_signature(const unsigned char *sha1, void *buf, 
unsigned long size, const char *type);

diff --git a/sha1_file.c b/sha1_file.c
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1084,7 +1084,7 @@ void *read_object_with_reference(const u
  	}
  }

-static char *write_sha1_file_prepare(void *buf,
+char *write_sha1_file_prepare(void *buf,
  				     unsigned long len,
  				     const char *type,
  				     unsigned char *sha1,
@@ -1283,12 +1283,14 @@ int has_sha1_file(const unsigned char *s
  	return find_pack_entry(sha1, &e);
  }

-int index_fd(unsigned char *sha1, int fd, struct stat *st)
+int index_fd(unsigned char *sha1, int fd, struct stat *st, int info_only)
  {
  	unsigned long size = st->st_size;
  	void *buf;
  	int ret;
-
+	unsigned char hdr[50];
+	int hdrlen;
+	
  	buf = "";
  	if (size)
  		buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
@@ -1296,7 +1298,10 @@ int index_fd(unsigned char *sha1, int fd
  	if ((int)(long)buf == -1)
  		return -1;

-	ret = write_sha1_file(buf, size, "blob", sha1);
+	if (info_only) {
+		(void) write_sha1_file_prepare(buf, size, "blob", sha1, hdr, &hdrlen);
+		ret = 0;
+	} else ret = write_sha1_file(buf, size, "blob", sha1);
  	if (size)
  		munmap(buf, size);
  	return ret;
diff --git a/update-cache.c b/update-cache.c
--- a/update-cache.c
+++ b/update-cache.c
@@ -68,7 +68,7 @@ static int add_file_to_cache(char *path)
  		fd = open(path, O_RDONLY);
  		if (fd < 0)
  			return -1;
-		if (index_fd(ce->sha1, fd, &st) < 0)
+		if (index_fd(ce->sha1, fd, &st, 0) < 0)
  			return -1;
  		break;
  	case S_IFLNK:
diff --git a/write-blob.c b/write-blob.c
--- a/write-blob.c
+++ b/write-blob.c
@@ -17,7 +17,7 @@ int main(int argc, char **argv)
  		fd = open(path, O_RDONLY);
  		if (fd < 0 ||
  		    fstat(fd, &st) < 0 ||
-		    index_fd(sha1, fd, &st) < 0)
+		    index_fd(sha1, fd, &st, 0) < 0)
  			die("Unable to add blob %s to database", path);
  		printf("%s\n", sha1_to_hex(sha1));
  	}



Message 1 of 1 in “2/7 Prepare for the next two patches”
  1. 2/7 Prepare for the next two patchesBryan Larsen, Jul 8, 2005

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.