threads / patch / 28740

patchgit-svn: add hook to allow modifying the subversion commit message

Subject: [PATCH] git-svn: add hook to allow modifying the subversion commit message

## tl;dr

3 messages between Oct 21, 2011 and Nov 1, 2011. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Michael Lutz· Oct 21, 2011, 20:25 UTC · lore

Sometimes modifying the commit message git-svn creates for a subversion commit can be useful, for example if the original message contains meta information not needed in the git clone or information from svn properties should be stored visibly in the commit message.

This change adds a hook 'git-svn-msg' analogue to the 'commit-msg' hook. Additionally to the commit message, the hook is passed the git-svn meta data by an environment variable.

Signed-off-by: Michael Lutz <michi@icosahedron.de>
---
 Documentation/githooks.txt          |   19 +++++++++++++++++++
 git-svn.perl                        |   25 +++++++++++++++++++++++++
 templates/hooks--git-svn-msg.sample |   18 ++++++++++++++++++
 3 files changed, 62 insertions(+), 0 deletions(-)
 create mode 100755 templates/hooks--git-svn-msg.sample
Show changes to 3 files +62 −0

Documentation/githooks.txt, git-svn.perl, templates/hooks--git-svn-msg.sample

diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index 28edefa..dd90c1a 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -45,6 +45,25 @@ the commit after inspecting the message file.
 The default 'applypatch-msg' hook, when enabled, runs the
 'commit-msg' hook, if the latter is enabled.
 
+git-svn-msg
+~~~~~~~~~~~
+
+This hook is invoked by 'git svn' when creating a git commit
+from a subversion commit.  It takes a single parameter, the name
+of the file that holds the original subversion commit message.
+Exiting with non-zero status causes 'git svn' to abbort before
+creating the git commit. Additional, the environment variable
+`GIT_SVN_METADATA=:` is set to the string that would appear
+after the 'git-svn-id:' line.
+
+The hook is allowed to edit the message file in place, and can
+be used to normalize the message into some project standard
+format (if the project has one). It can also be used to refuse
+the commit after inspecting the message file.
+
+The default 'git-svn-msg' hook, when enabled, runs the
+'commit-msg' hook, if the latter is enabled.
+
 pre-applypatch
 ~~~~~~~~~~~~~~
 
diff --git a/git-svn.perl b/git-svn.perl
index b67fef0..030516c 100755
--- a/git-svn.perl
+++ b/git-svn.perl
@@ -3007,6 +3007,31 @@ sub do_git_commit {
 		require Encode;
 		Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
 	}
+
+	# execute commit message hook if present
+	if (-x "$ENV{GIT_DIR}/hooks/git-svn-msg") {
+		print "Calling commit mesasage hook\n";
+
+		# write commit message to file
+		my $msg_file = "$ENV{GIT_DIR}/SVN_COMMIT_EDITMSG";
+		mkfile($msg_file);
+		open my $fh, '>', $msg_file or croak $!;
+		binmode $fh;
+		print $fh $log_entry->{log} or croak $!;
+		close $fh or croak $!;
+
+		$ENV{GIT_SVN_METADATA} = $log_entry->{metadata};
+		system("$ENV{GIT_DIR}/hooks/git-svn-msg", $msg_file);
+		die "git-svn-msg hook failed: $!\n" if $?;
+
+		# read commit message back
+		open $fh, '<', $msg_file or croak $!;
+		binmode $fh;
+		$log_entry->{log} = do { local $/; <$fh> };
+		close $fh or croak $!;
+		unlink $msg_file;
+	}
+
 	print $msg_fh $log_entry->{log} or croak $!;
 	restore_commit_header_env($old_env);
 	unless ($self->no_metadata) {
diff --git a/templates/hooks--git-svn-msg.sample b/templates/hooks--git-svn-msg.sample
new file mode 100755
index 0000000..6584fd8
--- /dev/null
+++ b/templates/hooks--git-svn-msg.sample
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# An example hook script to check the commit log message taken by
+# git svn from a subversion commit.
+#
+# The hook should exit with non-zero status after issuing an
+# appropriate message if it wants to stop the commit.  The hook is
+# allowed to edit the commit message file.
+#
+# GIT_SVN_METADATA will contain the same info as printed after
+# the git-svn-id line.
+#
+# To enable this hook, rename this file to "git-svn-msg".
+
+. git-sh-setup
+test -x "$GIT_DIR/hooks/commit-msg" &&
+	exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
+:
-- 
1.7.5.1
Eric Wong· Nov 1, 2011, 20:28 UTC · re: Michael Lutz · lore

Re: [PATCH] git-svn: add hook to allow modifying the subversion commit message

Michael Lutz <michi@icosahedron.de> wrote:
Show 8 quoted lines
> Sometimes modifying the commit message git-svn creates for a subversion
> commit can be useful, for example if the original message contains meta
> information not needed in the git clone or information from svn properties
> should be stored visibly in the commit message.
> 
> This change adds a hook 'git-svn-msg' analogue to the 'commit-msg' hook.
> Additionally to the commit message, the hook is passed the git-svn meta
> data by an environment variable.

I'm not convinced this is a good feature to support. We already have --no-metadata to remove git-svn-id: lines and I hate that feature because it introduced extra variables for testing/debugging/recovery.

Metadata in the commit message is important, if you want to remove it after-the-fact, there's git-filter-branch.

When dealing with repositories that have been through several systems (e.g. (CVS|Perforce) -> SVN -> git), it's useful to be able to refer to old mailing list archives that only refer to the original system (since neither SVN nor git existed at the time the email was sent).

Michael Lutz· Nov 1, 2011, 20:45 UTC · re: Eric Wong · lore

Re: [PATCH] git-svn: add hook to allow modifying the subversion commit message

Am 01.11.2011 21:28 schrieb Eric Wong:
Show 6 quoted lines
> I'm not convinced this is a good feature to support.  We already have
> --no-metadata to remove git-svn-id: lines and I hate that feature
> because it introduced extra variables for testing/debugging/recovery.
> 
> Metadata in the commit message is important, if you want to remove
> it after-the-fact, there's git-filter-branch.

The actual use case here is to replace a custom hack that adds some info to the git commit message. In this case filter-branch isn't an option because git-svn continuously pulls from the subversion master repository. Additionally, especially for *adding* information, the information might not be available later any more.

The only current metadata (the git-svn-id line) isn't affected by this change as git-svn outputs it separately after the svn commit message that the proposed hook can modify.

I fully realize that this is a rather specialized feature but I wanted to publish it in case somebody else finds a use for this as well.

Michael Lutz

← back to recent threads