Add GIT_EDITOR environment and core.editor configuration variables
authorAdam Roben <aroben@apple.com>
Fri, 20 Jul 2007 05:09:35 +0000 (22:09 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Jul 2007 07:46:34 +0000 (00:46 -0700)
These variables let you specify an editor that will be launched in
preference to the EDITOR and VISUAL environment variables. The order
of preference is GIT_EDITOR, core.editor, EDITOR, VISUAL.

[jc: added a test and config variable documentation]

Signed-off-by: Adam Roben <aroben@apple.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt
Documentation/git-commit.txt
Documentation/git-send-email.txt
git-am.sh
git-commit.sh
git-rebase--interactive.sh
git-send-email.perl
git-sh-setup.sh
git-tag.sh
t/t7005-editor.sh [new file with mode: 0755]
index d0e9a175f4c967f03462e92a0357165ddfb45473..a850d55bf68d3e64486f38bad1adb3fc1b97a81a 100644 (file)
@@ -281,6 +281,14 @@ core.excludesfile::
        of files which are not meant to be tracked.  See
        gitlink:gitignore[5].
 
+core.editor::
+       Commands such as `commit` and `tag` that lets you edit
+       messages by lauching an editor uses the value of this
+       variable when it is set, and the environment variable
+       `GIT_EDITOR` is not set.  The order of preference is
+       `GIT_EDITOR` environment, `core.editor`, `EDITOR` and
+       `VISUAL` environment variables and then finally `vi`.
+
 core.pager::
        The command that git will use to paginate output.  Can be overridden
        with the `GIT_PAGER` environment variable.
index f96142f96a733c9cd71ecffd3325bbb168a8ef38..8e0e7e2d0400b85b05c7e684bc87d9f5f1279e4a 100644 (file)
@@ -244,10 +244,12 @@ on the Subject: line and the rest of the commit in the body.
 
 include::i18n.txt[]
 
-ENVIRONMENT VARIABLES
----------------------
-The command specified by either the VISUAL or EDITOR environment
-variables is used to edit the commit log message.
+ENVIRONMENT AND CONFIGURATION VARIABLES
+---------------------------------------
+The editor used to edit the commit log message will be chosen from the
+GIT_EDITOR environment variable, the core.editor configuration variable, the
+VISUAL environment variable, or the EDITOR environment variable (in that
+order).
 
 HOOKS
 -----
index 293686c31f9c80074d7b6d34fa303f56e19d8113..d243ed1e3a3c4f3e94d01b076e7715d37a1cd204 100644 (file)
@@ -44,8 +44,8 @@ The --cc option must be repeated for each user you want on the cc list.
        value; if that is unspecified, default to --chain-reply-to.
 
 --compose::
-       Use $EDITOR to edit an introductory message for the
-       patch series.
+       Use $GIT_EDITOR, core.editor, $VISUAL, or $EDITOR to edit an
+       introductory message for the patch series.
 
 --from::
        Specify the sender of the emails.  This will default to
index e5e6f2c91c1faf58239ab73672fbb05cda91762f..bfd65dcf955187030e183b11c164abe02e9bbe62 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -364,7 +364,7 @@ do
                [yY]*) action=yes ;;
                [aA]*) action=yes interactive= ;;
                [nN]*) action=skip ;;
-               [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
+               [eE]*) git_editor "$dotest/final-commit"
                       action=again ;;
                [vV]*) action=again
                       LESS=-S ${PAGER:-less} "$dotest/patch" ;;
index 3f3de1729e98f48272b2f9f2eaff05d7a1266b11..92749df1e75de9af6c60a65866f1f68a21b799fe 100755 (executable)
@@ -544,18 +544,9 @@ fi
 
 case "$no_edit" in
 '')
-       case "${VISUAL:-$EDITOR},$TERM" in
-       ,dumb)
-               echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
-               echo >&2 "Please supply the commit log message using either"
-               echo >&2 "-m or -F option.  A boilerplate log message has"
-               echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
-               exit 1
-               ;;
-       esac
        git-var GIT_AUTHOR_IDENT > /dev/null  || die
        git-var GIT_COMMITTER_IDENT > /dev/null  || die
-       ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
+       git_editor "$GIT_DIR/COMMIT_EDITMSG"
        ;;
 esac
 
index f3950767ea6256cd9c2dceabbe7c91fa62e3f3aa..a2d4d09f5d3c5e87dfad639ed7502933d065b323 100755 (executable)
@@ -414,7 +414,7 @@ EOF
                        die_abort "Nothing to do"
 
                cp "$TODO" "$TODO".backup
-               ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
+               git_editor "$TODO" ||
                        die "Could not execute editor"
 
                test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
index 7552caca4bbbb38b161cfb47f94d4577627ccbb3..a09b1c96509e2587966fd2b1c5691e216f56ba8b 100755 (executable)
@@ -49,8 +49,8 @@ sub usage {
    --bcc          Specify a list of email addresses that should be Bcc:
                  on all the emails.
 
-   --compose      Use \$EDITOR to edit an introductory message for the
-                  patch series.
+   --compose      Use \$GIT_EDITOR, core.editor, \$EDITOR, or \$VISUAL to edit
+                 an introductory message for the patch series.
 
    --subject      Specify the initial "Subject:" line.
                   Only necessary if --compose is also set.  If --compose
@@ -341,8 +341,7 @@ sub expand_aliases {
 EOT
        close(C);
 
-       my $editor = $ENV{EDITOR};
-       $editor = 'vi' unless defined $editor;
+       my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
        system($editor, $compose_filename);
 
        open(C2,">",$compose_filename . ".final")
index 4ed07e9ddca885e8a4f184a68bb6363fdc7c56e9..c51985e4c36022a1f58146e49cc82336c39df05a 100755 (executable)
@@ -28,6 +28,21 @@ set_reflog_action() {
        fi
 }
 
+git_editor() {
+       GIT_EDITOR=${GIT_EDITOR:-$(git config core.editor || echo ${VISUAL:-${EDITOR}})}
+       case "$GIT_EDITOR,$TERM" in
+       ,dumb)
+               echo >&2 "No editor specified in GIT_EDITOR, core.editor, VISUAL,"
+               echo >&2 "or EDITOR. Tried to fall back to vi but terminal is dumb."
+               echo >&2 "Please set one of these variables to an appropriate"
+               echo >&2 "editor or run $0 with options that will not cause an"
+               echo >&2 "editor to be invoked (e.g., -m or -F for git-commit)."
+               exit 1
+               ;;
+       esac
+       "${GIT_EDITOR:-vi}" "$1"
+}
+
 is_bare_repository () {
        git rev-parse --is-bare-repository
 }
index 1c25d88c72b0645e6e619a05b1fbbbc091752444..5ee3f50a3c083aa0685ad696fd1bc39b3ffbe607 100755 (executable)
@@ -177,7 +177,7 @@ if [ "$annotate" ]; then
         ( echo "#"
           echo "# Write a tag message"
           echo "#" ) > "$GIT_DIR"/TAG_EDITMSG
-        ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR"/TAG_EDITMSG || exit
+        git_editor "$GIT_DIR"/TAG_EDITMSG || exit
     else
         printf '%s\n' "$message" >"$GIT_DIR"/TAG_EDITMSG
     fi
diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
new file mode 100755 (executable)
index 0000000..28643b0
--- /dev/null
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+test_description='GIT_EDITOR, core.editor, and stuff'
+
+. ./test-lib.sh
+
+for i in GIT_EDITOR core_editor EDITOR VISUAL vi
+do
+       cat >e-$i.sh <<-EOF
+       echo "Edited by $i" >"\$1"
+       EOF
+       chmod +x e-$i.sh
+done
+unset vi
+mv e-vi.sh vi
+PATH=".:$PATH"
+unset EDITOR VISUAL GIT_EDITOR
+
+test_expect_success setup '
+
+       msg="Hand edited" &&
+       echo "$msg" >expect &&
+       git add vi &&
+       test_tick &&
+       git commit -m "$msg" &&
+       git show -s --pretty=oneline |
+       sed -e "s/^[0-9a-f]* //" >actual &&
+       diff actual expect
+
+'
+
+TERM=dumb
+export TERM
+test_expect_success 'dumb should error out when falling back on vi' '
+
+       if git commit --amend
+       then
+               echo "Oops?"
+               exit 1
+       else
+               : happy
+       fi
+'
+
+TERM=vt100
+export TERM
+for i in vi EDITOR VISUAL core_editor GIT_EDITOR
+do
+       echo "Edited by $i" >expect
+       unset EDITOR VISUAL GIT_EDITOR
+       git config --unset-all core.editor
+       case "$i" in
+       core_editor)
+               git config core.editor ./e-core_editor.sh
+               ;;
+       [A-Z]*)
+               eval "$i=./e-$i.sh"
+               export $i
+               ;;
+       esac
+       test_expect_success "Using $i" '
+               git commit --amend &&
+               git show -s --pretty=oneline |
+               sed -e "s/^[0-9a-f]* //" >actual &&
+               diff actual expect
+       '
+done
+
+unset EDITOR VISUAL GIT_EDITOR
+git config --unset-all core.editor
+for i in vi EDITOR VISUAL core_editor GIT_EDITOR
+do
+       echo "Edited by $i" >expect
+       case "$i" in
+       core_editor)
+               git config core.editor ./e-core_editor.sh
+               ;;
+       [A-Z]*)
+               eval "$i=./e-$i.sh"
+               export $i
+               ;;
+       esac
+       test_expect_success "Using $i (override)" '
+               git commit --amend &&
+               git show -s --pretty=oneline |
+               sed -e "s/^[0-9a-f]* //" >actual &&
+               diff actual expect
+       '
+done
+
+test_done