From: Karthik Nayak <karthik.188@gmail.com>
To: git@vger.kernel.org
Cc: Karthik Nayak <karthik.188@gmail.com>
Subject: [PATCH v3 2/3] sha1_file: implement changes for "cat-file --literally -t"
Date: Thu, 5 Mar 2015 23:49:20 +0530 [thread overview]
Message-ID: <1425579560-18898-1-git-send-email-karthik.188@gmail.com> (raw)
In-Reply-To: <54F89D90.6090505@gmail.com>
Add "sha1_object_info_literally()" which is to be used when
the "literally" option is given to get the type of object
and return it. It internally uses "sha1_object_info_extended()".
Add "unpack_sha1_header_literally()" to unpack sha headers
which may be greater than 32 bytes, which is the threshold
for a regular object header. This code is borrowed from the
suggestions given by Junio C Hamano, it has been tested by me.
Modify "sha1_loose_object_info()" to include a flag argument
and also include changes to call "unpack_sha1_header_literally()"
when the literally flag is passed. Also copies the obtained
type to the typename field of object_info.
Modify "sha1_object_info_extended()" to call the function
"sha1_loose_object_info()" with flags.
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
sha1_file.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 76 insertions(+), 7 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 69a60ec..b9e3922 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1564,6 +1564,36 @@ int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long ma
return git_inflate(stream, 0);
}
+static int unpack_sha1_header_literally(git_zstream *stream, unsigned char *map,
+ unsigned long mapsize,
+ struct strbuf *header)
+{
+ unsigned char buffer[32], *cp;
+ unsigned long bufsiz = sizeof(buffer);
+ int status;
+
+ /* Get the data stream */
+ memset(stream, 0, sizeof(*stream));
+ stream->next_in = map;
+ stream->avail_in = mapsize;
+ stream->next_out = buffer;
+ stream->avail_out = bufsiz;
+
+ git_inflate_init(stream);
+
+ do {
+ status = git_inflate(stream, 0);
+ strbuf_add(header, buffer, stream->next_out - buffer);
+ for (cp = buffer; cp < stream->next_out; cp++)
+ if (!*cp)
+ /* Found the NUL at the end of the header */
+ return 0;
+ stream->next_out = buffer;
+ stream->avail_out = bufsiz;
+ } while (status == Z_OK);
+ return -1;
+}
+
static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
{
int bytes = strlen(buffer) + 1;
@@ -2524,13 +2554,16 @@ struct packed_git *find_sha1_pack(const unsigned char *sha1,
}
static int sha1_loose_object_info(const unsigned char *sha1,
- struct object_info *oi)
+ struct object_info *oi,
+ int flags)
{
- int status;
+ int status = 0;
unsigned long mapsize, size;
void *map;
git_zstream stream;
char hdr[32];
+ struct strbuf hdrbuf = STRBUF_INIT;
+ char *hdrp;
if (oi->delta_base_sha1)
hashclr(oi->delta_base_sha1);
@@ -2557,10 +2590,25 @@ static int sha1_loose_object_info(const unsigned char *sha1,
return -1;
if (oi->disk_sizep)
*oi->disk_sizep = mapsize;
- if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
- status = error("unable to unpack %s header",
- sha1_to_hex(sha1));
- else if ((status = parse_sha1_header(hdr, &size)) < 0)
+ if ((flags & LOOKUP_LITERALLY)) {
+ if (unpack_sha1_header_literally(&stream, map, mapsize, &hdrbuf) < 0)
+ status = error("unable to unpack %s header with --literally",
+ sha1_to_hex(sha1));
+ hdrp = hdrbuf.buf;
+ } else {
+ if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
+ status = error("unable to unpack %s header",
+ sha1_to_hex(sha1));
+ }
+ hdrp = hdr;
+ }
+ if (status)
+ ; /* We're already checking for errors */
+ else if ((flags & LOOKUP_LITERALLY)) {
+ size_t typelen = strcspn(hdrbuf.buf, " ");
+ strbuf_add(oi->typename, hdrbuf.buf, typelen);
+ }
+ else if ((status = parse_sha1_header(hdrp, &size)) < 0)
status = error("unable to parse %s header", sha1_to_hex(sha1));
else if (oi->sizep)
*oi->sizep = size;
@@ -2568,6 +2616,10 @@ static int sha1_loose_object_info(const unsigned char *sha1,
munmap(map, mapsize);
if (oi->typep)
*oi->typep = status;
+ if (oi->typename && 0 <= status && typename(status))
+ strbuf_addstr(oi->typename, typename(status));
+ if (hdrp == hdrbuf.buf)
+ strbuf_release(&hdrbuf);
return 0;
}
@@ -2594,7 +2646,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
if (!find_pack_entry(real, &e)) {
/* Most likely it's a loose object. */
- if (!sha1_loose_object_info(real, oi)) {
+ if (!sha1_loose_object_info(real, oi, flags)) {
oi->whence = OI_LOOSE;
return 0;
}
@@ -2635,6 +2687,23 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
return type;
}
+const char *sha1_object_info_literally(const unsigned char *sha1)
+{
+ enum object_type type;
+ struct strbuf sb = STRBUF_INIT;
+ struct object_info oi = {NULL};
+
+ oi.typename = &sb;
+ oi.typep = &type;
+ if (sha1_object_info_extended(sha1, &oi, LOOKUP_LITERALLY) < 0)
+ return NULL;
+ if (*oi.typep > 0) {
+ strbuf_release(oi.typename);
+ return typename(*oi.typep);
+ }
+ return oi.typename->buf;
+}
+
static void *read_packed_sha1(const unsigned char *sha1,
enum object_type *type, unsigned long *size)
{
--
2.3.1.167.g7f4ba4b.dirty
next prev parent reply other threads:[~2015-03-05 18:19 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-05 18:16 [PATCH v3 0/3] cat-file: add "--literally" option karthik nayak
2015-03-05 18:18 ` [PATCH v3 1/3] cache: modify for "cat-file --literally -t" Karthik Nayak
2015-03-08 22:25 ` Eric Sunshine
2015-03-09 12:56 ` karthik nayak
2015-03-05 18:19 ` Karthik Nayak [this message]
2015-03-05 23:45 ` [PATCH v3 2/3] sha1_file: implement changes " Junio C Hamano
2015-03-06 17:41 ` karthik nayak
2015-03-06 19:28 ` Junio C Hamano
2015-03-07 10:04 ` karthik nayak
2015-03-08 8:09 ` Junio C Hamano
2015-03-08 8:48 ` karthik nayak
2015-03-08 9:03 ` Junio C Hamano
2015-03-08 10:49 ` karthik nayak
2015-03-08 19:09 ` Junio C Hamano
2015-03-09 13:08 ` karthik nayak
2015-03-05 18:19 ` [PATCH v3 3/3] cat-file: add "--literally" option Karthik Nayak
2015-03-08 22:50 ` Eric Sunshine
2015-03-09 12:53 ` karthik nayak
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1425579560-18898-1-git-send-email-karthik.188@gmail.com \
--to=karthik.188@gmail.com \
--cc=git@vger.kernel.org \
/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.
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 NNTP newsgroup(s).