threads / discuss / 6847

suggested feature: someone mails me a blob, git please tell me what it is

Subject: suggested feature: someone mails me a blob, git please tell me what it is

## tl;dr

6 messages between Feb 16, 2007 and Feb 17, 2007.

replies: 5people: 6as markdown or json

Mike Coleman· Feb 16, 2007, 16:23 UTC · lore
Here's a suggestion for a git feature, if git doesn't already have it.
 Since git generally doesn't use RCS-style keyword cookies (like
"$Id$"), it'd be nice to have some other way to identify an installed
file.  Perhaps you'd want to know exactly what rev a user is running,
for example.

It seems like it should be fairly easy, given a blob (the file), for git to describe what it knows about it. For example, it could provide a list of commits that it's a part of, etc. It'd be *really* nice if only the output of the sha1sum command on the blob were needed. (I can't recall exactly how git's blob SHA1's are computed--maybe this isn't feasible.)

Mike
Nicolas Pitre· Feb 16, 2007, 16:48 UTC · re: Mike Coleman · lore

Re: suggested feature: someone mails me a blob, git please tell me what it is

On Fri, 16 Feb 2007, Mike Coleman wrote:
Show 9 quoted lines
> Here's a suggestion for a git feature, if git doesn't already have it.
> Since git generally doesn't use RCS-style keyword cookies (like
> "$Id$"), it'd be nice to have some other way to identify an installed
> file.  Perhaps you'd want to know exactly what rev a user is running,
> for example.
> 
> It seems like it should be fairly easy, given a blob (the file), for
> git to describe what it knows about it.  For example, it could provide
> a list of commits that it's a part of, etc.  

That is possible, but would be expensive similar to git-fsck-objects. All objects for each commit would need to be searched for the matching sha1.

> It'd be *really* nice if only the output of the sha1sum command on the 
> blob were needed.  (I can't recall exactly how git's blob SHA1's are 
> computed--maybe this isn't feasible.)

GIT adds a header of its own before object data. You therefore need to use git-hash-object not sha1sum to get the right sha1 value.

Nicolas
Matthieu Moy· Feb 16, 2007, 17:14 UTC · re: Mike Coleman · lore

Re: suggested feature: someone mails me a blob, git please tell me what it is

"Mike Coleman" <tutufan@gmail.com> writes:
> It seems like it should be fairly easy, given a blob (the file), for
> git to describe what it knows about it.  For example, it could provide
> a list of commits that it's a part of, etc.  It'd be *really* nice if
> only the output of the sha1sum command on the blob were needed. 

"git cat-file -p" does almost this : given the sha1sum for a blob, it will give the header and the content of the blob ...

> (I can't recall exactly how git's blob SHA1's are computed--maybe
> this isn't feasible.)

... but this is where it doesn't work : http://www.kernel.org/pub/software/scm/git/docs/v1.5.0/git.html#Discussion explains that the sha1sum used by git is the one of the blob _plus_ its header, so it's not the one you get with "sha1sum file".

But it should be possible to recompute the git sha1sum by recomputing the magic formula sha1sum(<ascii type without space> + <space> + <ascii decimal size> + <byte\0> + <binary object data>).

-- 
Matthieu
Linus Torvalds· Feb 16, 2007, 17:37 UTC · re: Matthieu Moy · lore

Re: suggested feature: someone mails me a blob, git please tell me what it is

On Fri, 16 Feb 2007, Matthieu Moy wrote:
> 
> But it should be possible to recompute the git sha1sum by recomputing
> the magic formula sha1sum(<ascii type without space> + <space> +
> <ascii decimal size> + <byte\0> + <binary object data>).
Yes. Try this:
	[torvalds@woody git]$ ls -l Makefile
	-rw-rw-rw- 1 torvalds torvalds 27586 Feb 14 13:35 Makefile
	[torvalds@woody git]$ ( echo -en "blob 27586\0" ; cat Makefile ) | sha1sum
	ebecbbd9c28390654ed9fea2ff4ebf6a5a317c70  -
	[torvalds@woody git]$ git ls-tree HEAD Makefile
	100644 blob ebecbbd9c28390654ed9fea2ff4ebf6a5a317c70    Makefile
and notice how the SHA1's match (ebecbb..).

Now, finding all of the copies of that particular object in history is pretty expensive. You'll basically have to do something like

	git rev-list --all |
		while read i
		do
			files=$(git ls-tree -r $i | grep $SHA1)
			if [ -n "$files" ]
			then
				git cat-file -p $i
				echo $files
				echo
			fi
		done

which is quite expensive. You can probably start from "git fsck" and ask it to print out where it found something (but then it will print out only the first time it found it - because it avoids walking trees more than once it won't report on it unless if finds it in two trees that are different).

			Linus
Shawn O. Pearce· Feb 17, 2007, 06:10 UTC · re: Mike Coleman · lore

Re: suggested feature: someone mails me a blob, git please tell me what it is

Mike Coleman <tutufan@gmail.com> wrote:
Show 12 quoted lines
> Here's a suggestion for a git feature, if git doesn't already have it.
> Since git generally doesn't use RCS-style keyword cookies (like
> "$Id$"), it'd be nice to have some other way to identify an installed
> file.  Perhaps you'd want to know exactly what rev a user is running,
> for example.
> 
> It seems like it should be fairly easy, given a blob (the file), for
> git to describe what it knows about it.  For example, it could provide
> a list of commits that it's a part of, etc.  It'd be *really* nice if
> only the output of the sha1sum command on the blob were needed.  (I
> can't recall exactly how git's blob SHA1's are computed--maybe this
> isn't feasible.)

Rather than embedding something like "$Id$" on a per-file basis use the output of git-describe in your build process to insert the string into your executable file(s), and then provide a way to let the user extract it, or insert it with a unique enough pattern that you can grep for it in the output of `strings`.

This is what git does itself. We replace GIT_VERSION in git.c with the output of git-describe during compile time (C preprocessor macro). The value can be printed by the user with `git version`. We also embed it into some Perl scripts.

git-gui does the same thing, but is a Tcl/Tk script.
-- 
Shawn.
Andy Parkins· Feb 17, 2007, 21:40 UTC · re: Shawn O. Pearce · lore

Re: suggested feature: someone mails me a blob, git please tell me what it is

On Saturday 2007, February 17, Shawn O. Pearce wrote:
> Rather than embedding something like "$Id$" on a per-file basis
> use the output of git-describe in your build process to insert the

Actually this isn't entirely enough. The /only/ thing I miss from subversion is $Id$; but not in source files - that's easily solved with a bit of git-describe magic in the Makefile - no, I miss embedding it inkscape generated .svg files, so that my title block on diagrams gets automatically assigned a version code.

Q: How lazy am I?  A: I can't be bothered to say... 
Andy
-- 
Dr Andrew Parkins, M Eng (Hons), AMIEE
andyparkins@gmail.com

← back to recent threads