You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@subversion.apache.org by Douglas Bullard <db...@nurflugel.com> on 2009/08/17 23:05:16 UTC

Pre-commit hooks and access to files

I'm investigating whether it's possible to modify the contents of 
files in pre-commit hooks (such as inserting author's names and 
revision numbers in class comments, etc).

The docs for pre-commit hooks say that modification of the 
contents of a transaction are prohibited.  Would this preclude me 
from doing this?

If not, how would I get a handle of the file contents to modify - 
svnlook doesn't seem to do this.

I understand that people have strong opinions on this, but I'm 
investigating whether it's possible, the advisabitlity of doing so 
is another conversation.


Thanks in advance...

Douglas Bullard

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2384562

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Pre-commit hooks and access to files

Posted by Blair Zajac <bl...@orcaware.com>.
Douglas Bullard wrote:
> I'm investigating whether it's possible to modify the contents of 
> files in pre-commit hooks (such as inserting author's names and 
> revision numbers in class comments, etc).

It's possible but you would break the client that just did the commit.

> The docs for pre-commit hooks say that modification of the 
> contents of a transaction are prohibited.  Would this preclude me 
> from doing this?

Yes, it does preclude you from doing this.

Blair

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2384571

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Pre-commit hooks and access to files

Posted by Ryan Schmidt <su...@ryandesign.com>.
On Aug 18, 2009, at 14:06, David Weintraub wrote:

> Let's take Answer #1: You cannot modify the files in the commit  
> itself. You can examine the files with the svnlook command, but you  
> can't modify them. How could you? The working copy is sitting on  
> the developer's computer, and the hook is running on the server.  
> You simply don't have access to the files in order to modify them.

I'm told you actually do have access to the files, if you use the  
Subversion language bindings to access the transaction. But, you must  
not do so.

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2385015

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Pre-commit hooks and access to files

Posted by David Weintraub <qa...@gmail.com>.
On Mon, Aug 17, 2009 at 7:05 PM, Douglas Bullard <db...@nurflugel.com>wrote:

> I'm investigating whether it's possible to modify the contents of
> files in pre-commit hooks (such as inserting author's names and
> revision numbers in class comments, etc).


There are three answers to this particular question:

1). No
2). Yes
3). Don't even think about doing it.

Let's take Answer #1: You cannot modify the files in the commit itself. You
can examine the files with the svnlook command, but you can't modify them.
How could you? The working copy is sitting on the developer's computer, and
the hook is running on the server. You simply don't have access to the files
in order to modify them.

Now, let's look at Answer #2: Although you can't modify that particular
commit itself, it is technically possible to have your hook script do a
fresh checkout, modify the files, and then commit those changes. The end
results are pretty much the same as modifying the files on a commit.

Now, let's look at Answer #3, and why you should never, ever even think of
doing Answer #2:

Modifying the committed files will mean that the user who just did the
commit will now find their working directory out of date. Plus, you now are
going to have two commits for every developer commit: The one the developer
did, and the one the hook script did. It makes tracking down changes quite
difficult.

And, then there's the actual machination involved. First of all, when your
hook script makes the changes, and does a commit, it will end up calling
itself again. This can lead to your hook script going into an infinite loop
as it checks in the changes over and over again. Then, there's the time it
will take for you to do a checkout, modify the files, do a second commit,
and then run through your hook script again. The developer will have to wait
as you do all of this.

And, don't forget that you could have multiple commits going on at once --
especially since now each commit is going to take much longer. That means
you'll need separate working directories for each commit.

Plus, what happens if your modifications actually breaks the build? The
developer makes their changes, tests, and does a submit. Now, suddenly the
code doesn't work and may be impossible to fix until you turn off that hook.


And, you very well know the developer will still blame your hook script even
if they checked in bad code from the start. "It wasn't my fault! That hook
script changed it and broke it!".

So, no you can't modify the commit on a pre-commit hook, and even if you
could, it is still a lousy idea.

If you are simply looking into this for yourself, you now know everything
about the dark art of commit modification and know it should never be
practiced. If a coworker was asking you to investigate this, tell them that
it's simply not possible. Period

I've been a SCM for almost two decades, and I've used lots of SCM tools.
Some tools (like ClearCase) run hook scripts on the client's machine and not
the server, so it is very easy to modify  files being committed since you
have direct access to the working directory. I've fallen for this trap with
disastrous results. It simply is asking for trouble.

If you are trying to figure out who modified or wrote a class, you can use
the "svn blame (aka "svn annotate" and "svn praise") command. This will show
you who modified a particular line and the revision that line was modified
in.

-- 
David Weintraub
qazwart@gmail.com

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2384902

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Pre-commit hooks and access to files

Posted by Daniel Becroft <dj...@gmail.com>.
On Tue, Aug 18, 2009 at 9:05 AM, Douglas Bullard<db...@nurflugel.com>
wrote:
> I'm investigating whether it's possible to modify the contents of
> files in pre-commit hooks (such as inserting author's names and
> revision numbers in class comments, etc).
>
> The docs for pre-commit hooks say that modification of the
> contents of a transaction are prohibited.  Would this preclude me
> from doing this?
>
> If not, how would I get a handle of the file contents to modify -
> svnlook doesn't seem to do this.
>
> I understand that people have strong opinions on this, but I'm
> investigating whether it's possible, the advisabitlity of doing so
> is another conversation.

You can examine the file contents in the pre-commit hook using:

svnlook cat
>

However, as to modification of the file, I do not believe that is possible
(and is certainly not advisable).

If it is author/revision information you are after, have you considered
using SVN's keyword substitution?

http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html

Cheers,
Daniel B.

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2384566

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Pre-commit hooks and access to files

Posted by Ryan Schmidt <su...@ryandesign.com>.
On Aug 17, 2009, at 18:19, Andy Levy wrote:

> On Mon, Aug 17, 2009 at 19:05, Douglas Bullard wrote:
>
>> I'm investigating whether it's possible to modify the contents of
>> files in pre-commit hooks (such as inserting author's names and
>> revision numbers in class comments, etc).
>>
>> The docs for pre-commit hooks say that modification of the
>> contents of a transaction are prohibited.  Would this preclude me
>> from doing this?
>
> It's probably technically possible (though rather tricky &
> error-prone), but it's a very, very bad idea for reasons that have
> been covered many times on this list.
>
> 1) The committer's working copy is immediately out of sync with what
> is in the repository
> 2) The committer doesn't *really* know what's being committed in  
> his name
> 3) A bad substitution could break code that was working when you began
> the commit.
> 3a) There's no good way to do any validation of what you're  
> committing.
> 4) What happens if, for another reason, your commit fails?

Andy, you're supposing that Douglas is doing a second commit in a  
post-commit hook, after the user's commit, to make additional  
changes. That would be a way to do it, though problematic for the  
reasons you stated.

But my reading of his request is that he is wanting to modify the  
user's original transaction as it occurs, in the pre-commit hook,  
which is much worse, because after the commit of the revision (call  
it "R"), the committer's working copy will not be marked as out of  
date -- will think it is up-to-date -- yet will not contain exactly  
what's in the repository. What the working copy thinks revision R is  
and what the repository thinks revision R is will be two different  
things. Therefore on subsequent updates of the working copy, the diff  
that the server sends to the client may not apply properly on the  
client and cause the update to fail.


>> If not, how would I get a handle of the file contents to modify -
>> svnlook doesn't seem to do this.
>
> Use keywords. http://svnbook.red-bean.com/en/1.5/ 
> svn.advanced.props.special.keywords.html
>
>> I understand that people have strong opinions on this, but I'm
>> investigating whether it's possible, the advisabitlity of doing so
>> is another conversation.
>
> Theoretically, it's possible to fall out of an airplane and survive.
> But that doesn't mean you should do it.

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2384586

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].

Re: Pre-commit hooks and access to files

Posted by Andy Levy <an...@gmail.com>.
On Mon, Aug 17, 2009 at 19:05, Douglas Bullard<db...@nurflugel.com> wrote:
> I'm investigating whether it's possible to modify the contents of
> files in pre-commit hooks (such as inserting author's names and
> revision numbers in class comments, etc).
>
> The docs for pre-commit hooks say that modification of the
> contents of a transaction are prohibited.  Would this preclude me
> from doing this?

It's probably technically possible (though rather tricky &
error-prone), but it's a very, very bad idea for reasons that have
been covered many times on this list.

1) The committer's working copy is immediately out of sync with what
is in the repository
2) The committer doesn't *really* know what's being committed in his name
3) A bad substitution could break code that was working when you began
the commit.
3a) There's no good way to do any validation of what you're committing.
4) What happens if, for another reason, your commit fails?

> If not, how would I get a handle of the file contents to modify -
> svnlook doesn't seem to do this.

Use keywords. http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html

> I understand that people have strong opinions on this, but I'm
> investigating whether it's possible, the advisabitlity of doing so
> is another conversation.

Theoretically, it's possible to fall out of an airplane and survive.
But that doesn't mean you should do it.

------------------------------------------------------
http://subversion.tigris.org/ds/viewMessage.do?dsForumId=1065&dsMessageId=2384570

To unsubscribe from this discussion, e-mail: [users-unsubscribe@subversion.tigris.org].