From: karthik nayak <karthik.188@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v3 2/3] sha1_file: implement changes for "cat-file --literally -t"
Date: Fri, 06 Mar 2015 23:11:10 +0530 [thread overview]
Message-ID: <54F9E6B6.4070105@gmail.com> (raw)
In-Reply-To: <xmqq61af100p.fsf@gitster.dls.corp.google.com>
On 03/06/2015 05:15 AM, Junio C Hamano wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> +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;
>> +}
>
> After calling this function to ask the textual type of an object,
> should the caller free the result it obtains from this function?
>
> oi.typename points at the strbuf on stack and its buf member points
> at an allocated piece of memory. That must be freed.
>
> On the other hand, typename(*oi.typep) is a pointer into static
> piece of memory, which must never be freed.
>
> This patch introduces this function without introducing any caller,
> which makes it unnecessarily harder to judge if this problem is
> caused by choosing a wrong calling convention, and/or if so what
> better calling convention can be used to correct the problem, but
> without looking at the caller that (presumably) will be introduced
> in a later patch, I suspect that the caller should supply a pointer
> to struct object_info, i.e. something along these lines:
>
> struct object_info oi = { NULL };
> struct strbuf sb = STRBUF_INIT;
> enum object_type type;
>
> ...
>
> oi.typename = &sb;
> sha1_object_info_literally(sha1, &oi);
> if (!sb.len)
> that is an error;
> else
> use sb.buf as the name;
>
> strbuf_release(&sb);
I thought I could get the calling function "cat_one_file()" to send
the address to a struct strbuf. Like this ..
struct strbuf sb = STRBUF_INIT;
length = sha1_object_info_literally(sha1, &sb);
if (length < 0)
die("git cat-file --literally -t %s: failed",
obj_name);
printf("%s\n", sb.buf);
strbuf_release(&sb);
return 0;
What do you think? Is this ok?
>
> As sha1_object_info_extended() takes oi and fills oi.typename when
> it is supplied for _all_ types, not just the bogus ones, a caller of
> that function, including sha1_object_info_literally() and its
> caller, shouldn't have to worry about "is that a known one? then
> use typename() to convert the enum type to a string. Otherwise use
> the oi.typename->buf" at all, I would think.
>
>
>
>
I also missed a part where the object given was a packed object.
eg : git cat-file -t --literally HEAD~2
And since I missed that out, it wasnt copying the type to oi.typename,
and oi.typename would end up being empty, I found this while i was
using gdb.
Didn't CC the mailing list the first time, sorry.
next prev parent reply other threads:[~2015-03-06 17:41 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 ` [PATCH v3 2/3] sha1_file: implement changes " Karthik Nayak
2015-03-05 23:45 ` Junio C Hamano
2015-03-06 17:41 ` karthik nayak [this message]
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=54F9E6B6.4070105@gmail.com \
--to=karthik.188@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.