threads / discuss / 13535

How can I tell if a SHA1 is a submodule reference?

Subject: How can I tell if a SHA1 is a submodule reference?

## tl;dr

5 messages between May 15, 2008 and May 15, 2008.

replies: 4people: 4as markdown or json

Robin Luckey· May 15, 2008, 19:39 UTC · lore

I am parsing the output of git-diff-tree to create some code analysis reports.

When a user adds a submodule to a repository, git-diff-tree reports the SHA1 of the commit from the submodule.

However, if I subsequently try to pass this SHA1 to git-cat-file, or indeed any other git command I have tried, I receive an error:

error: unable to find b0f8c354b142e27333abd0f175544b71a0cc444e fatal: Not a valid object name b0f8c354b142e27333abd0f175544b71a0cc444e

This makes sense to me, since these objects are not stored locally; they are stored in the submodule repository.

However, is there a simple and reliable way for me to know which SHA1 hashes refer to such submodule objects? I'd like to simply ignore them.

My ideal feature would be that `git cat-file -t` would respond with `submodule`, but of course this does not happen. Long term, an '-- ingore-submodules' flag for git would be great since I wouldn't see these hashes in the first place.

However, today, given an arbitrary hash, how can I tell whether it is a submodule commit?

Thanks, Robin

Avery Pennarun· May 15, 2008, 20:12 UTC · re: Robin Luckey · lore

Re: How can I tell if a SHA1 is a submodule reference?

On 5/15/08, Robin Luckey <robin@ohloh.net> wrote:
>  However, is there a simple and reliable way for me to know which SHA1
> hashes refer to such submodule objects? I'd like to simply ignore them.

I don't think there's a straightforward way to do this given *just* the SHA1, in the same way that you can't know the path corresponding to a blob given just its SHA1.

However, if you're looking at a tree that refers to a SHA1, the tree will reference a submodule object as a "commit" instead of a "tree" or "blob". git-ls-tree output looks something like this:

160000 commit ba75ff608fabafeaafeb48d55b125440b5a665bc	my_subdir

I think it's reasonable to say that if it's type commit, then it's a submodule. (Note that simply being a submodule doesn't *necessarily* imply that it'll be unavailable; some people like to store all the submodule objects in the local repository.)

Have fun,
Avery
Linus Torvalds· May 15, 2008, 20:13 UTC · re: Robin Luckey · lore

Re: How can I tell if a SHA1 is a submodule reference?

On Thu, 15 May 2008, Robin Luckey wrote:
Show 17 quoted lines
>
> I am parsing the output of git-diff-tree to create some code analysis reports.
> 
> When a user adds a submodule to a repository, git-diff-tree reports the SHA1
> of the commit from the submodule.
> 
> However, if I subsequently try to pass this SHA1 to git-cat-file, or indeed
> any other git command I have tried, I receive an error:
> 
> error: unable to find b0f8c354b142e27333abd0f175544b71a0cc444e
> fatal: Not a valid object name b0f8c354b142e27333abd0f175544b71a0cc444e
> 
> This makes sense to me, since these objects are not stored locally; they are
> stored in the submodule repository.
> 
> However, is there a simple and reliable way for me to know which SHA1 hashes
> refer to such submodule objects? I'd like to simply ignore them.
Only if you still have the tree references they came from.

In other words, once you *only* have the SHA1, you're kind of screwed, because by that time you've lost the context, and the SHA1 itself contains no information about what kind of thing it is.

But when running git-diff-tree, you still have the source of the SHA1, and the tree entry in particular. The tree entry will have a "gitlink" marker, ie the "mode" will be 0160000.

			Linus
Junio C Hamano· May 15, 2008, 20:21 UTC · re: Robin Luckey · lore

Re: How can I tell if a SHA1 is a submodule reference?

Robin Luckey <robin@ohloh.net> writes:
Show 17 quoted lines
> I am parsing the output of git-diff-tree to create some code analysis
> reports.
>
> When a user adds a submodule to a repository, git-diff-tree reports
> the SHA1 of the commit from the submodule.
>
> However, if I subsequently try to pass this SHA1 to git-cat-file, or
> indeed any other git command I have tried, I receive an error:
>
> error: unable to find b0f8c354b142e27333abd0f175544b71a0cc444e
> fatal: Not a valid object name b0f8c354b142e27333abd0f175544b71a0cc444e
>
> This makes sense to me, since these objects are not stored locally;
> they are stored in the submodule repository.
>
> However, is there a simple and reliable way for me to know which SHA1
> hashes refer to such submodule objects? I'd like to simply ignore them.

I presume you are reading "diff-tree --raw" format output. The mode bits for submodules (aka gitlinks) are 160000, as opposed to either 100644 or 100755 for regular files and 120000 for symbolic links.

Robin Luckey· May 15, 2008, 20:56 UTC · re: Junio C Hamano · lore

Re: How can I tell if a SHA1 is a submodule reference?

Thanks for the replies -- checking the mode bits was the ticket.
Robin
On May 15, 2008, at 1:21 PM, Junio C Hamano wrote:
Show 29 quoted lines
> Robin Luckey <robin@ohloh.net> writes:
>
>> I am parsing the output of git-diff-tree to create some code analysis
>> reports.
>>
>> When a user adds a submodule to a repository, git-diff-tree reports
>> the SHA1 of the commit from the submodule.
>>
>> However, if I subsequently try to pass this SHA1 to git-cat-file, or
>> indeed any other git command I have tried, I receive an error:
>>
>> error: unable to find b0f8c354b142e27333abd0f175544b71a0cc444e
>> fatal: Not a valid object name  
>> b0f8c354b142e27333abd0f175544b71a0cc444e
>>
>> This makes sense to me, since these objects are not stored locally;
>> they are stored in the submodule repository.
>>
>> However, is there a simple and reliable way for me to know which SHA1
>> hashes refer to such submodule objects? I'd like to simply ignore  
>> them.
>
> I presume you are reading "diff-tree --raw" format output.  The mode  
> bits
> for submodules (aka gitlinks) are 160000, as opposed to either  
> 100644 or
> 100755 for regular files and 120000 for symbolic links.
>
>

← back to recent threads