threads / discuss / 10851

Integrating with hooks

Subject: Integrating with hooks

## tl;dr

6 messages between Nov 13, 2007 and Nov 15, 2007.

replies: 5people: 4as markdown or json

Todd A. Jacobs· Nov 13, 2007, 17:37 UTC · lore

I've created some bash functions which handle tagging some files with revision information, but even after reading the git manual I'm not really sure how to integrate them so that they remove revision expansion before each check-in (to avoid cluttering the repository with keyword substitutions), and add them back (with the current commit info) after each commit.

These are the functions:
    # Show some kind of useful revision string, like the RCS $Id$ string. I
    # think commit hash, filename, hostname containing the repository, and
    # timestamp should be plenty of information to track down a given file.
    git-id () {
	for file in "$@"; do
	    _date=$(date +'%F %T %Z')
	    git log -1 \
		--pretty=format:"[%h] \"$file\" $(hostname -f) ($_date)" \
		"$file"
	done
    }
    # Replace the $Id$ keyword string in the file itself.
    git-export () {
	for file in "$@"; do
	    echo Modifying $file...
	    _id=$(git-id "$file")
	    sed -ri 's/\$(Id|Revision).*\$/$Id: '"$_id"' $/' "$file"
	done
    }
    # Clean the $Id$ keyword string to prevent cluttering the repository
    # with keyword-revision diffs when we check the file back in.
    git-unexport () {
	for file in "$@"; do
	    echo Resetting $file...
	    sed -ri 's/\$Id.*\$/$Id$/' "$file"
	done
    }
How do I hook this in the way I want so that it's handled automatically?
-- 
"Oh, look: rocks!"
	-- Doctor Who, "Destiny of the Daleks"
Jakub Narebski· Nov 13, 2007, 23:07 UTC · re: Todd A. Jacobs · lore

Re: Integrating with hooks

[Cc: Todd A. Jacobs <nospam@codegnome.org>, git@vger.kernel.org]
Todd A. Jacobs wrote:
Show 8 quoted lines
> I've created some bash functions which handle tagging some files with
> revision information, but even after reading the git manual I'm not
> really sure how to integrate them so that they remove revision expansion
> before each check-in (to avoid cluttering the repository with keyword
> substitutions), and add them back (with the current commit info) after
> each commit.
> 
> These are the functions:
[...]
> How do I hook this in the way I want so that it's handled automatically?
Take a look at gitattributes(5), namely 'filter' attribute.

Although instead of implementing it "by hand", perhaps it would be enough to use 'ident' and/or 'export-subs' attribute.

P.S. Because of the way git updates files, and git thinks about files it is I think universally regarded to be bad idea to put in a file any Id that depend on commit data.

-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Todd A. Jacobs· Nov 15, 2007, 01:18 UTC · re: Jakub Narebski · lore

Re: Integrating with hooks

On Wed, Nov 14, 2007 at 12:07:29AM +0100, Jakub Narebski wrote:
> Take a look at gitattributes(5), namely 'filter' attribute.

Thanks, I took a look at the man page you suggested. The "ident" feature almost does what I want, but doesn't seem to take any sort of format string. So, I thought I'd explore "filter," but can't really find any examples of how to implement the smudge and clean commands, which seem to be what I'm really trying to do here.

Is there an example somewhere that you can point me to? The man page doesn't really show any examples of how to implement the filter attribute, so I'm a little unsure how to proceed.

-- 
"Oh, look: rocks!"
	-- Doctor Who, "Destiny of the Daleks"
Jakub Narebski· Nov 15, 2007, 01:43 UTC · re: Todd A. Jacobs · lore

Re: Integrating with hooks

Todd A. Jacobs wrote:
Show 7 quoted lines
> On Wed, Nov 14, 2007 at 12:07:29AM +0100, Jakub Narebski wrote:
> 
>> Take a look at gitattributes(5), namely 'filter' attribute.
> 
> Thanks, I took a look at the man page you suggested. The "ident" feature
> almost does what I want, but doesn't seem to take any sort of format
> string. 

The `ident` feature provides only sensible Id for a file (if you want to avoid potentially rewriting _all_ and not only changed files on checkout / switching branch / reset --hard). All Ids which have commit info, like commit id, commit date, author are not sensible in mentioned sense.

Show 7 quoted lines
> So, I thought I'd explore "filter," but can't really find any 
> examples of how to implement the smudge and clean commands, which seem
> to be what I'm really trying to do here.
> 
> Is there an example somewhere that you can point me to? The man page
> doesn't really show any examples of how to implement the filter
> attribute, so I'm a little unsure how to proceed.

Cannot help you there, but see examples of `diff` and `merge` attributes, it should I think be similar.

-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Johannes Schindelin· Nov 15, 2007, 01:43 UTC · re: Todd A. Jacobs · lore

Re: Integrating with hooks

Hi,
On Wed, 14 Nov 2007, Todd A. Jacobs wrote:
Show 7 quoted lines
> On Wed, Nov 14, 2007 at 12:07:29AM +0100, Jakub Narebski wrote:
> 
> > Take a look at gitattributes(5), namely 'filter' attribute.
> 
> Thanks, I took a look at the man page you suggested. The "ident" feature 
> almost does what I want, but doesn't seem to take any sort of format 
> string.
Note that the ident feature will give you _only_ the blob sha1.  
I guess that you're more interested in the export-subst feature.

Hth, Dscho

Johannes Sixt· Nov 15, 2007, 07:57 UTC · re: Todd A. Jacobs · lore

Re: Integrating with hooks

Todd A. Jacobs schrieb:
Show 9 quoted lines
> On Wed, Nov 14, 2007 at 12:07:29AM +0100, Jakub Narebski wrote:
> 
>> Take a look at gitattributes(5), namely 'filter' attribute.
> 
> Thanks, I took a look at the man page you suggested. The "ident" feature
> almost does what I want, but doesn't seem to take any sort of format
> string. So, I thought I'd explore "filter," but can't really find any
> examples of how to implement the smudge and clean commands, which seem
> to be what I'm really trying to do here.

A clean and smudge filter processes one file at a time. It reads the old content from stdin and writes the result to stdout.

There is a tiny example in the test suite, t/t0021-conversion.sh, look for rot13.sh.

-- Hannes

← back to recent threads