[Top][All Lists]

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

[PATCH 2/2] maint: choose editor for our commit-msg git hook the same wa

From: Stefano Lattarini
Subject: [PATCH 2/2] maint: choose editor for our commit-msg git hook the same way git does
Date: Fri, 15 Feb 2013 14:39:40 +0100

Git honours the GIT_EDITOR environment variable, the "core.editor" Git
configuration variable, and the EDITOR environment variable (in that
order, and defaulting to "vi" if none of them is set) to decide which
editor should be invoked for the user when he has to or want to edit
his commit message.

However, our commit-msg hook, when invoking an editor on behalf of the
user to allow him to fix a non-policy-complaint commit message, only
honoured the EDITOR environment variable.  To avoid potential annoying
inconsistencies, we should really use the same logic used by Git in the
selection of the editor.  Luckily, we don't have to duplicate this
logic (that would be brittle in the long term), as we can rely on the
"git var" command, designed exactly to be used in situations like this.

* scripts/git-hooks ($editor): Adjust definition.
 scripts/git-hooks/commit-msg | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/scripts/git-hooks/commit-msg b/scripts/git-hooks/commit-msg
index 7a11489..559a0ea 100755
--- a/scripts/git-hooks/commit-msg
+++ b/scripts/git-hooks/commit-msg
@@ -6,7 +6,10 @@ use strict;
 use warnings;
 (my $ME = $0) =~ s|.*/||;
-my $editor = $ENV{EDITOR} || 'vi';
+# Emulate Git's choice of the editor for the commit message.
+chomp (my $editor = `git var GIT_EDITOR`);
+# And have a sane, minimal fallback in case of weird failures.
+$editor = "vi" if $? != 0 or $editor =~ /^\s*\z/;
 # Keywords allowed before the colon on the first line of a commit message:
 # program names and a few general category names.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]