# is it kosher for pre-commit to change what's staged?

9 messages from 2010-11-10 to 2010-11-11. Participants: Joey Hess, Junio C Hamano, Jonathan Nieder, Dmitry Potapov, Jan Hudec, Kevin Ballard.
Thread: https://gitlist.dev/t/25701

## Joey Hess, 2010-11-10 17:08

Subject: is it kosher for pre-commit to change what's staged?
Message-ID: <20101110170819.GA3031@gnu.kitenet.net>
URL: https://gitlist.dev/e/20101110170819.GA3031%40gnu.kitenet.net

```
I've noticed that if I make a pre-commit hook change the files that are
staged, those changes are not reflected in the commit message. For
example, if a pre-commit hook git add's somefile, the commit message
won't reflect that. I guess prepare-commit-msg is being run before
pre-commit for some reason?

-- 
see shy jo

```

## Junio C Hamano, 2010-11-10 19:57

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <7v1v6thrzc.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v1v6thrzc.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20101110170819.GA3031@gnu.kitenet.net>

```
Joey Hess <joey@kitenet.net> writes:

> I've noticed that if I make a pre-commit hook change the files that are
> staged, those changes are not reflected in the commit message. For
> example, if a pre-commit hook git add's somefile, the commit message
> won't reflect that. I guess prepare-commit-msg is being run before
> pre-commit for some reason?

My intention was that Documentation/githooks.txt would document things
that are allowed (e.g. "applypatch-msg" explicitly says "The hook is
allowed to edit the message"), and anything that is not specifically
allowed is not.

"Is it kosher" is a difficult question to answer, as something may not be
allowed but there may not be an enforcement mechanism to deny it, iow, it
may happen to work by accident.

In general, pre-anything is about checking and denying and is supposed to
be free of side effects.

```

## Jonathan Nieder, 2010-11-10 20:06

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <20101110200650.GA13439@burratino>
URL: https://gitlist.dev/e/20101110200650.GA13439%40burratino
In-Reply-To: <7v1v6thrzc.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Joey Hess <joey@kitenet.net> writes:

>> I've noticed that if I make a pre-commit hook change the files that are
>> staged, those changes are not reflected in the commit message. For
>> example, if a pre-commit hook git add's somefile, the commit message
>> won't reflect that. I guess prepare-commit-msg is being run before
>> pre-commit for some reason?

I'm guessing it is to allow cancelling a commit before a costly
pre-commit hook runs.

> My intention was that Documentation/githooks.txt would document things
> that are allowed (e.g. "applypatch-msg" explicitly says "The hook is
> allowed to edit the message"), and anything that is not specifically
> allowed is not.
> 
> "Is it kosher" is a difficult question to answer, as something may not be
> allowed but there may not be an enforcement mechanism to deny it, iow, it
> may happen to work by accident.

In this case, isn't it only a half accident?  For example, I think
v1.5.4-rc0~78^2~12 (builtin-commit: fix partial-commit support,
2007-11-18) taught git to support this a little better.

That said, I would be interested to hear the use case, since modifying
staged content on the fly for a commit sounds a little crazy. :)

```

## Joey Hess, 2010-11-10 20:45

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <20101110204542.GA11201@gnu.kitenet.net>
URL: https://gitlist.dev/e/20101110204542.GA11201%40gnu.kitenet.net
In-Reply-To: <20101110200650.GA13439@burratino>

```
Jonathan Nieder wrote:
> In this case, isn't it only a half accident?  For example, I think
> v1.5.4-rc0~78^2~12 (builtin-commit: fix partial-commit support,
> 2007-11-18) taught git to support this a little better.

Partial commits modified by pre-commit still don't work entirely right IME.
After the commit, the index is such that git wants to revert any changes
added to the commit by the pre-commit hook.

> That said, I would be interested to hear the use case, since modifying
> staged content on the fly for a commit sounds a little crazy. :)

Well, with git-annex (which I should probably properly announce here
sometime), large files are not committed to git, but instead a symlink
pointing at the content is committed. That symlink can temporarily be
replaced with a copy of the file when the user wants to modify it, and
if the user then commits, it's nice if it can move away the large file's
content, and arrange for the commit to contain an updated symlink pointing
at the new content.

That said, since git commit stages the whole large file into the index,
which is the kind of expensive operation git-annex exists to avoid,
it's still not very practical to intercept the commit like that. What
I ideally need is a hook that is run before git commit stages anything.


BTW, githooks says pre-commit is "invoked before obtaining the proposed
commit log message", which is a bit confusing. I actually see
prepare-commit-msg and commit-msg both called after pre-commit.

-- 
see shy jo

```

## Dmitry Potapov, 2010-11-10 22:17

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <AANLkTi=W_hwU=3PTTqE4R8Cg5T=GMdMfBsKQ1YE0HWRe@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTi%3DW_hwU%3D3PTTqE4R8Cg5T%3DGMdMfBsKQ1YE0HWRe%40mail.gmail.com
In-Reply-To: <20101110204542.GA11201@gnu.kitenet.net>

```
On Wed, Nov 10, 2010 at 11:45 PM, Joey Hess <joey@kitenet.net> wrote:
>
> That said, since git commit stages the whole large file into the index,
> which is the kind of expensive operation git-annex exists to avoid,
> it's still not very practical to intercept the commit like that. What
> I ideally need is a hook that is run before git commit stages anything.

Then maybe you should look at git smudge&clean filters. They perform
conversation between the working tree and the index.  Though I am not
sure how well they work with big files.


Dmitry

```

## Jan Hudec, 2010-11-11 19:26

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <20101111192648.GE30870@efreet.light.src>
URL: https://gitlist.dev/e/20101111192648.GE30870%40efreet.light.src
In-Reply-To: <AANLkTi=W_hwU=3PTTqE4R8Cg5T=GMdMfBsKQ1YE0HWRe@mail.gmail.com>

```
On Thu, Nov 11, 2010 at 01:17:04 +0300, Dmitry Potapov wrote:
> On Wed, Nov 10, 2010 at 11:45 PM, Joey Hess <joey@kitenet.net> wrote:
> >
> > That said, since git commit stages the whole large file into the index,
> > which is the kind of expensive operation git-annex exists to avoid,
> > it's still not very practical to intercept the commit like that. What
> > I ideally need is a hook that is run before git commit stages anything.
> 
> Then maybe you should look at git smudge&clean filters. They perform
> conversation between the working tree and the index.  Though I am not
> sure how well they work with big files.

The problem point is not big files, but whether they can change the file mode
associated with the entry, since in this case the tree contains a file, but
index should contain a symlink. Looking at the documentation, there does not
seem to be a way to do it.

It might be worth adding support for it now we have a use-case.

-- 
						 Jan 'Bulb' Hudec <bulb@ucw.cz>

```

## Joey Hess, 2010-11-11 20:46

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <20101111204626.GA13937@gnu.kitenet.net>
URL: https://gitlist.dev/e/20101111204626.GA13937%40gnu.kitenet.net
In-Reply-To: <20101111192648.GE30870@efreet.light.src>

```
Jan Hudec wrote:
> The problem point is not big files, but whether they can change the file mode
> associated with the entry, since in this case the tree contains a file, but
> index should contain a symlink. Looking at the documentation, there does not
> seem to be a way to do it.
> 
> It might be worth adding support for it now we have a use-case.

I suppose that if I were using smudge filters, I'd probably not need to
use symlinks at all. I'll think about it.

It belatedly occured to me that I already had something else modifying
the index in pre-commit, and smudge filters cannot handle its use case.

That is, etckeeper (http://kitenet.net/~joey/code/etckeeper). The
pre-commit script there stores some metadata about files that git
usually doesn't track, by putting it in a dotfile, and stages that
dotfile for commit. Which currently works fine, except for the minor
problem that the user doesn't see the dotfile listed in the commit
message.

-- 
see shy jo

```

## Junio C Hamano, 2010-11-11 22:03

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <7vlj4zfrgt.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vlj4zfrgt.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20101111204626.GA13937@gnu.kitenet.net>

```
Joey Hess <joey@kitenet.net> writes:

> ... The
> pre-commit script there stores some metadata about files that git
> usually doesn't track, by putting it in a dotfile, and stages that
> dotfile for commit. Which currently works fine, except for the minor
> problem that the user doesn't see the dotfile listed in the commit
> message.

I am hearing "list of files in the commit message", and am getting
confused.  As far as I know we don't store such a list in the commit
message.

Puzzled.

```

## Kevin Ballard, 2010-11-11 22:06

Subject: Re: is it kosher for pre-commit to change what's staged?
Message-ID: <F0C8038B-0D8C-4A50-9934-9D9CF26FF9F9@sb.org>
URL: https://gitlist.dev/e/F0C8038B-0D8C-4A50-9934-9D9CF26FF9F9%40sb.org
In-Reply-To: <7vlj4zfrgt.fsf@alter.siamese.dyndns.org>

```
On Nov 11, 2010, at 2:03 PM, Junio C Hamano wrote:

> Joey Hess <joey@kitenet.net> writes:
> 
>> ... The
>> pre-commit script there stores some metadata about files that git
>> usually doesn't track, by putting it in a dotfile, and stages that
>> dotfile for commit. Which currently works fine, except for the minor
>> problem that the user doesn't see the dotfile listed in the commit
>> message.
> 
> I am hearing "list of files in the commit message", and am getting
> confused.  As far as I know we don't store such a list in the commit
> message.
> 
> Puzzled.

I believe he means in the # comments at the bottom of the editor. They
contain the output of `git status` as a reminder of what you're
committing.

-Kevin Ballard

```
