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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx>
---
 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

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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]