Skip to contentSkip to conversation

git svn : hook before 'git svn dcommit'

PATCH In review Frédéric Heitzmann 5 participants 9 messages Jun 15, 2016 1 message not in the archive Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 git svn : hook before 'git svn dcommit'

The 'pre-svn-dcommit' hook is called before 'git svn dcommit', which aborts if return value is not zero. The only parameter given to the hook is the reference given to 'git svn dcommit'. If no paramter was used, hook gets HEAD as its only parameter.

---
I resend the same patch previously sent July 9th. Apparently it did not graduated upstream, and I do not know why. Please someone tell me if something needs to be improved.

 Documentation/git-svn.txt |   14 +++++++++++++-
 git-svn.perl              |   21 +++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletions(-)
Documentation/git-svn.txt +13 -1
index 713e523..ec87ed3 100644
@@ -700,6 +700,18 @@ section because they affect the 'git-svn-id:' metadata line, except
 for rewriteRoot and rewriteUUID which can be used together.
 
 
+HOOKS
+-----
+
+The 'pre-svn-dcommit' hook is called by 'git svn dcommit' and can be used to
+prevent some diff to be committed to a SVN repository. It may typically be
+used to filter some intermediate patches, which were committed into git but
+must not find their way to the SVN repository.
+
+It takes a single parameter, the reference given to 'git svn dcommit'. If the
+hook exists with a non zero-status, 'git svn dcommit' will abort.
+
+
 BASIC EXAMPLES
 --------------
 
@@ -901,7 +913,7 @@ reset) branches-maxRev and/or tags-maxRev as appropriate.
 
 SEE ALSO
 --------
-linkgit:git-rebase[1]
+linkgit:git-rebase[1], linkgit:githooks[5]
 
 GIT
 ---
git-svn.perl +21 -0
index 89f83fd..a537858 100755
@@ -396,6 +396,25 @@ sub init_subdir {
 	$_repository = Git->repository(Repository => $ENV{GIT_DIR});
 }
 
+sub pre_svn_dcommit_hook {
+	my $head = shift;
+
+	my $hook = "$ENV{GIT_DIR}/hooks/pre-svn-dcommit";
+	return 0 if ! -e $hook || ! -x $hook;
+
+	system($hook, $head);
+	if ($? == -1) {
+		print "[pre_svn_dcommit_hook] failed to execute $hook: $!\n";
+		return 1;
+	} elsif ($? & 127) {
+		printf "[pre_svn_dcommit_hook] child died with signal %d, %s coredump\n",
+		($? & 127),  ($? & 128) ? 'with' : 'without';
+		return 1;
+	} else {
+		return $? >> 8;
+	}
+}
+
 sub cmd_clone {
 	my ($url, $path) = @_;
 	if (!defined $path &&
@@ -505,6 +524,8 @@ sub cmd_dcommit {
 		. "or stash them with `git stash'.\n";
 	$head ||= 'HEAD';
 
+	return if pre_svn_dcommit_hook($head);
+
 	my $old_head;
 	if ($head ne 'HEAD') {
 		$old_head = eval {
Signature · 1 line
1.7.6.133.gd3b55a
#1 lore raw
Quoted 4 lines · Frédéric Heitzmann

The 'pre-svn-dcommit' hook is called before 'git svn dcommit', which aborts if return value is not zero. The only parameter given to the hook is the reference given to 'git svn dcommit'. If no paramter was used, hook gets HEAD as its only parameter.

It appears that this is in the same spirit as the pre-commit hook used in "git commit", so it may not hurt but I do not know if having a separate hook is the optimal approach to achieve what it wants to do.

I notice that git-svn users have been happily using the subsystem without need for any hook (not just pre-commit). Does "git svn" need an equivalent of pre-commit hook? If so, does it need equivalents to other hooks as well? I am not suggesting you to add support for a boatload of other hooks in this patch---I am trying to see if this is really a necessary change to begin with.

Eric, do you want this one?

git-svn.perl sub init_subdir { · line 403

Quoted 13 lines
git-svn.perl +5 -0
index 89f83fd..a537858 100755
@@ -396,6 +396,25 @@ sub init_subdir {
 	$_repository = Git->repository(Repository => $ENV{GIT_DIR});
 }
 
+sub pre_svn_dcommit_hook {
+	my $head = shift;
+
+	my $hook = "$ENV{GIT_DIR}/hooks/pre-svn-dcommit";
+	return 0 if ! -e $hook || ! -x $hook;

Why force two stat(), instead of just "if ! -x $hook"? Doesn't it respond to a non-existing $hook with "there is nothing executable there" just fine?

git-svn.perl sub init_subdir { · line 416

Quoted 12 lines
git-svn.perl +12 -0
+	system($hook, $head);
+	if ($? == -1) {
+		print "[pre_svn_dcommit_hook] failed to execute $hook: $!\n";
+		return 1;
+	} elsif ($? & 127) {
+		printf "[pre_svn_dcommit_hook] child died with signal %d, %s coredump\n",
+		($? & 127),  ($? & 128) ? 'with' : 'without';
+		return 1;
+	} else {
+		return $? >> 8;
+	}
+}

Should these messages go to the standard output?

Quoted 12 lines
git-svn.perl +2 -0
 sub cmd_clone {
 	my ($url, $path) = @_;
 	if (!defined $path &&
@@ -505,6 +524,8 @@ sub cmd_dcommit {
 		. "or stash them with `git stash'.\n";
 	$head ||= 'HEAD';
 
+	return if pre_svn_dcommit_hook($head);
+
 	my $old_head;
 	if ($head ne 'HEAD') {
 		$old_head = eval {
#2 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 4 lines · Frédéric Heitzmann

The 'pre-svn-dcommit' hook is called before 'git svn dcommit', which aborts if return value is not zero. The only parameter given to the hook is the reference given to 'git svn dcommit'. If no paramter was used, hook gets HEAD as its only parameter.

It appears that this is in the same spirit as the pre-commit hook used in "git commit", so it may not hurt but I do not know if having a separate hook is the optimal approach to achieve what it wants to do.

Quoted 8 lines

I notice that git-svn users have been happily using the subsystem without need for any hook (not just pre-commit). Does "git svn" need an equivalent of pre-commit hook? If so, does it need equivalents to other hooks as well? I am not suggesting you to add support for a boatload of other hooks in this patch---I am trying to see if this is really a necessary change to begin with.

Eric, do you want this one?

I'm not sure. I feel hooks should be avoided whenever possible, and a git-svn-specific hook for dcommit wouldn't place the same restriction as a server-side SVN hook for svn(1) users.

Preventing certain commits from accidentally hitting the SVN server can be useful, I think. On the other hand, I'm not sure if people who run accidental dcommits would remember to the pre-dcommit hook, either.

Perhaps an interactive option for dcommit would be just as useful?

Test cases are required for any new features of git-svn, though.

Quoted 6 lines
Quoted 6 lines
git-svn.perl +6 -0
+	system($hook, $head);
+	if ($? == -1) {
+		print "[pre_svn_dcommit_hook] failed to execute $hook: $!\n";
+		return 1;
+	} elsif ($? & 127) {
+		printf "[pre_svn_dcommit_hook] child died with signal %d, %s coredump\n",
Quoted 8 lines

git-svn.perl sub init_subdir { · line 416

Quoted 6 lines
git-svn.perl +6 -0
+		($? & 127),  ($? & 128) ? 'with' : 'without';
+		return 1;
+	} else {
+		return $? >> 8;
+	}
+}

Should these messages go to the standard output?

Failure messages should definitely go to stderr.

Signature · 1 line
Eric Wong
#4 lore raw
In reply to a message not in the archive

Hi all.

Maybe I should give some more context to explain why a hook could be a potential improvement.

Let's consider the following workflow :
1) git svn clone from the SVN server, then git checkout -b topic
2) git commit some "reference data", before starting some optimization or code refactoring.
** These reference data are not supposed to find their way to the SVN server ** Committing such "reference data" is just a convenience because git does a great job to show how these data may or may not change during the development process.
3) hack, test, commit ...
3 bis) it may happen that reference data change for some very good reason (for instance some protocol change)
New reference data are then commited.

back to 3 ...

4) Before merging back to master and commitng to SVN, it is necessary to remove commits with reference data (git rebase -i --onto master master topic ...)
5) merge topic branch with master and git svn dcommit

-- end --

It is very easy to forget step 4, and svn commit lots of useless data.

Proposal 1)
* commit reference data with some specific mark in the commit message (e.g. "NO_SVN")
* use pre-svn-dcommit hook to detect such commits

Proposal 2) (not fully feasable for what I know)
* git svn clone to a bare repo
* clone a working repo from the the bare repo.
* steps 2, 3, maybe 3bis, ... then 4
* push commits to the bare repo, while using pre-receive or update hook to look for wrong commits, and abort if so.
* use post-receive hook to trigger git svn dcommit

Main drawback for proposal 2 (appart from needing 2 repo instead of one) is that each time you want to update your working  repo, you have to git svn rebase the bare repo, then git pull.

Proposal 2bis)
* add a pre-send hook on the bare repo, and trigger some git svn rebase with this hook.
I am not sure to see all the potential consequences of such a hook though.

All things begin equal, proposal 1 seems to be the easier path, but it is highly debatable.

--
Fred

ps : I had to resend this email because first attempt included HTML (sic). Very sorry if you receive it twice.

2011/8/17 Eric Wong <[email protected]>

Quoted 53 lines
Quoted 11 lines · Junio C Hamano
Quoted 4 lines · Frédéric Heitzmann

The 'pre-svn-dcommit' hook is called before 'git svn dcommit', which aborts if return value is not zero. The only parameter given to the hook is the reference given to 'git svn dcommit'. If no paramter was used, hook gets HEAD as its only parameter.

It appears that this is in the same spirit as the pre-commit hook used in "git commit", so it may not hurt but I do not know if having a separate hook is the optimal approach to achieve what it wants to do.

Quoted 8 lines

I notice that git-svn users have been happily using the subsystem without need for any hook (not just pre-commit). Does "git svn" need an equivalent of pre-commit hook? If so, does it need equivalents to other hooks as well? I am not suggesting you to add support for a boatload of other hooks in this patch---I am trying to see if this is really a necessary change to begin with.

Eric, do you want this one?

I'm not sure.  I feel hooks should be avoided whenever possible, and a git-svn-specific hook for dcommit wouldn't place the same restriction as a server-side SVN hook for svn(1) users.

Preventing certain commits from accidentally hitting the SVN server can be useful, I think.  On the other hand, I'm not sure if people who run accidental dcommits would remember to the pre-dcommit hook, either.

Perhaps an interactive option for dcommit would be just as useful?

Test cases are required for any new features of git-svn, though.

Quoted 13 lines
Quoted 4 lines

+   system($hook, $head);
+   if ($? == -1) {
+           print "[pre_svn_dcommit_hook] failed to execute $hook: $!\n";
+           return 1;

Quoted 8 lines

+   } elsif ($? & 127) {
+           printf "[pre_svn_dcommit_hook] child died with signal %d, %s coredump\n",
+           ($? & 127),  ($? & 128) ? 'with' : 'without';
+           return 1;
+   } else {
+           return $? >> 8;
+   }
+}

Quoted 1 line

Should these messages go to the standard output?

Failure messages should definitely go to stderr.

--
Eric Wong

#5 lore raw
Quoted 8 lines · Frédéric Heitzmann

4) Before merging back to master and commitng to SVN, it is necessary to remove commits with reference data (git rebase -i --onto master master topic ...)
5) merge topic branch with master and git svn dcommit

-- end --

It is very easy to forget step 4, and svn commit lots of useless data.

I agree.

Quoted 4 lines

Proposal 1)
* commit reference data with some specific mark in the commit message (e.g. "NO_SVN")
* use pre-svn-dcommit hook to detect such commits

The problem with this is hook standardization across committers and even across different machines/directories a committer may use.

Quoted 11 lines

Proposal 2) (not fully feasable for what I know)
* git svn clone to a bare repo
* clone a working repo from the the bare repo.
* steps 2, 3, maybe 3bis, ... then 4
* push commits to the bare repo, while using pre-receive or update hook to look for wrong commits, and abort if so.
* use post-receive hook to trigger git svn dcommit

Main drawback for proposal 2 (appart from needing 2 repo instead of one) is that each time you want to update your working  repo, you have to git svn rebase the bare repo, then git pull.

Proposal 2 is way too complicated, I hate it.

Quoted 2 lines

All things begin equal, proposal 1 seems to be the easier path, but it is highly debatable.

I had Proposal 3 in my original response:

Quoted 2 lines
Quoted 1 line · 2011/8/17 Eric Wong <[email protected]> wrote:

Perhaps an interactive option for dcommit would be just as useful?

1 and 3 can both implemented, but I think 3 would be easier to use/setup/standardize. I suspect it's also easier to train oneself to always use "dcommit -i". Perhaps even default to interactive mode like git-send-email does nowadays.

Unfortunately interactive dcommit requires more effort to implement.

Signature · 1 line
Eric Wong
#6 lore raw

2011/8/17 Eric Wong <[email protected]>:

Quoted 8 lines
Quoted 2 lines
Quoted 1 line · 2011/8/17 Eric Wong <[email protected]> wrote:

Perhaps an interactive option for dcommit would be just as useful?

1 and 3 can both implemented, but I think 3 would be easier to use/setup/standardize.  I suspect it's also easier to train oneself to always use "dcommit -i".  Perhaps even default to interactive mode like git-send-email does nowadays.

Quoted 4 lines

Unfortunately interactive dcommit requires more effort to implement.

--
Eric Wong

It seems that proposal 3 is somehow equivalent to

$ git rebase -i --onto remotes/trunk remotes/trunk
... check commits, maybe remove some of them, ...
$ git svn dcommit

note : is the SVN remote branch always named "remote/trunk" ? If not, is there a way to guess its name ?

--
Fred

#8 lore raw
In reply to Frédéric Heitzmann
Quoted 46 lines · Frédéric Heitzmann

Hi all.

Maybe I should give some more context to explain why a hook could be a potential improvement.

Let's consider the following workflow :
1) git svn clone from the SVN server, then git checkout -b topic
2) git commit some "reference data", before starting some optimization or code refactoring.
** These reference data are not supposed to find their way to the SVN server ** Committing such "reference data" is just a convenience because git does a great job to show how these data may or may not change during the development process.
3) hack, test, commit ...
3 bis) it may happen that reference data change for some very good reason (for instance some protocol change)
New reference data are then commited.

back to 3 ...

4) Before merging back to master and commitng to SVN, it is necessary to remove commits with reference data (git rebase -i --onto master master topic ...)
5) merge topic branch with master and git svn dcommit

-- end --

It is very easy to forget step 4, and svn commit lots of useless data.

Proposal 1)
* commit reference data with some specific mark in the commit message (e.g. "NO_SVN")
* use pre-svn-dcommit hook to detect such commits

Proposal 2) (not fully feasable for what I know)
* git svn clone to a bare repo
* clone a working repo from the the bare repo.
* steps 2, 3, maybe 3bis, ... then 4
* push commits to the bare repo, while using pre-receive or update hook to look for wrong commits, and abort if so.
* use post-receive hook to trigger git svn dcommit

Main drawback for proposal 2 (appart from needing 2 repo instead of one) is that each time you want to update your working  repo, you have to git svn rebase the bare repo, then git pull.

Quoted 8 lines

Proposal 2bis)
* add a pre-send hook on the bare repo, and trigger some git svn rebase with this hook.
I am not sure to see all the potential consequences of such a hook though.

All things begin equal, proposal 1 seems to be the easier path, but it is highly debatable.

I have written a local script for exactly the problem you described after looking for a git svn dcommit hook I could use (as you did).
I attached it, so feel free to use it. Simply add it to your bin and run it
with git dcommit instead of git svn dcommit

Pls read the comment for further explanation how this script is used.

#!/bin/bash
# Copyright © Peter Baumann, 2011
#
# Wrapper script around git svn dcommit, which adds some useful functionality #
# This script will prevent accidentally commiting some commits not yet ready

# into SVN. Commits starting with (case insensitive) debug, wip, fixup are
# considered not appropriate for putting them into SVN. The main reason for
# this functionality is the specific workflow I use. I always have some 
# internal debug commits (e.g. enhanced debug logging) or simply work in progress
# commits which should never be put into SVN. 
#
# To avoid putting those into SVN, I rebase all commits so that my WIP/DEBUG commits 
# are on top of the commits ment for SVN.
# Calling this script via "git dcommit" after the rebase makes sure only commits
# beneath the WIP commits are considered for SVN. 
# Furthermore, a shortlog of commits ment for SVN is shown and the user has
# to confirm before actually putting them into SVN.
#
# If this script is called via a specific commit (e.g. via its SHA1) as parameter,
# then only commits beneath and including the commit itself are committet to SVN.

SUBDIRECTORY_OK=Yes

. git-sh-setup
require_work_tree
cd_to_toplevel || die "foo"

# Upstream ref
upstream=remotes/trunk

# Stop at this commit
last=
if [ ! -z $1 ]; then

	$(git rev-parse $1^{commit})
	echo $last

fi

# The latest git commit we want to commit SVN
commit=

# Remembers the original head
orig_head=
if branch=$(git symbolic-ref -q HEAD)
then

	orig_head=${branch#refs/heads/}

else
orig_head='(detached head)'
fi

function run()

{
	#echo "DEBUG: $@"
	$@
}	

IFS='
'
for c in $(git log --reverse --pretty="%H %s" HEAD --not "${upstream}"); do

	# Split the log output into its fields
	sha1="${c:0:40}"
	msg="${c:41}"

	# Check if the commit subject matches (case insenstive) to one of the
	# following patterns. Leading whitespace is fine
	# 	debug
	#	wip
	#	fixup!
	if echo "$msg"|egrep -i -q '^\s*(debug|wip|fixup)'; then
		break
	fi
	commit=${sha1}

	if [ "x${commit}" = "x${last}" ]; then
		break
	fi

done

if [ "x${commit}" == "x" ]; then
die "Nothing to commit - Perhaps you have only stuff not ready for SVN?"
fi

echo ">>>> Committing the folling GIT commits to SVN <<<<" git --no-pager log --pretty=oneline ${commit} --not "${upstream}" echo

# Show the latest commit we are going to submit to SVN #git show ${commit}

echo
echo "Commiting to SVN (y/N)?"
read yesno || die "Aborting"

if [ "x${yesno}" == "xy" ] || [ "x$yesno" == "xY" ]; then

	run git checkout -q "${commit}" || die "Checkout failed"
	run git svn dcommit || "Aborting - git svn dcommit failed!"

	if [ "${orig_head}" != "(detached head)" ]; then
		echo "DO THIS:"
		run git checkout "${orig_head}" && run git rebase "${upstream}"
	else
		echo "You have started this script being on a detached HEAD."
		echo "Please rebase manually!"
	fi

fi

#7 lore raw
In reply to Frédéric Heitzmann
Quoted 9 lines · Frédéric Heitzmann

2011/8/17 Eric Wong <[email protected]>:

Quoted 8 lines
Quoted 2 lines
Quoted 1 line · 2011/8/17 Eric Wong <[email protected]> wrote:

Perhaps an interactive option for dcommit would be just as useful?

1 and 3 can both implemented, but I think 3 would be easier to use/setup/standardize.  I suspect it's also easier to train oneself to always use "dcommit -i".  Perhaps even default to interactive mode like git-send-email does nowadays.

Quoted 6 lines
Quoted 1 line

Unfortunately interactive dcommit requires more effort to implement.

It seems that proposal 3 is somehow equivalent to

$ git rebase -i --onto remotes/trunk remotes/trunk
... check commits, maybe remove some of them, ...
$ git svn dcommit

No, it would just prompt before making every commit (showing the log message), like git-send-email can before sending every message.

Quoted 2 lines

note : is the SVN remote branch always named "remote/trunk" ? If not, is there a way to guess its name ?

git svn dcommit --dry-run will print the URL from which you can infer the remote branch. There might be an easier way, but I can't remember... (I don't see SVN much nowadays, most projects I've cared about migrated to git)

Signature · 1 line
Eric Wong