Thread (9 messages) flat view 9 messages, 3 authors, 2016-06-15
DORMANTno replies

[PATCH 1/2] sha1_file: Add sha1_object_type_literally and export it.

From: Karthik Nayak <hidden>
Date: 2016-06-15 23:03:55
Subsystem: the rest · Maintainer: Linus Torvalds

sha1_object_type_literally takes a sha value and
gives the type of the given loose object, used by
git cat-file -t --literally.

Signed-off-by: Karthik Nayak <redacted>
---
 cache.h     |  1 +
 sha1_file.c | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
diff --git a/cache.h b/cache.h
index 4d02efc..48086b9 100644
--- a/cache.h
+++ b/cache.h
@@ -872,6 +872,7 @@ extern int git_open_noatime(const char *name);
 extern void *map_sha1_file(const unsigned char *sha1, unsigned long *size);
 extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);
 extern int parse_sha1_header(const char *hdr, unsigned long *sizep);
+extern int sha1_object_type_literally(const unsigned char *sha1, char *type);
 
 /* global flag to enable extra checks when accessing packed objects */
 extern int do_check_packed_object_crc;
diff --git a/sha1_file.c b/sha1_file.c
index 69a60ec..34b4810 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2635,6 +2635,33 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
 	return type;
 }
 
+int sha1_object_type_literally(const unsigned char *sha1, char *type)
+{
+	int status = 0;
+	unsigned long mapsize;
+	void *map;
+	git_zstream stream;
+	char hdr[32];
+	int i;
+
+	map = map_sha1_file(sha1, &mapsize);
+	if (!map)
+		return -1;
+	if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
+		status = error("unable to unpack %s header",
+			       sha1_to_hex(sha1));
+
+	for (i = 0; i < 32; i++) {
+		if (hdr[i] == ' ') {
+			type[i] = '\0';
+			break;
+		}
+		type[i] = hdr[i];
+	}
+
+	return status;
+}
+
 static void *read_packed_sha1(const unsigned char *sha1,
 			      enum object_type *type, unsigned long *size)
 {
-- 
2.3.1.129.g11acff1.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help