Support GIT_EDITOR='vim -c "set textwidth=80"'

If there are shell special characters in the editor string, we must
use /bin/sh to parse and execute it, rather than trying to rely on
a simple split(' ').  This avoids vim starting up with two empty
buffers, due to a misparsed command line.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2009-07-02 12:45:47 -07:00
parent 37dbf2bf0f
commit 1dcb58a7d0

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
import os import os
import re
import sys import sys
import subprocess import subprocess
import tempfile import tempfile
@ -38,6 +39,7 @@ class Editor(object):
if e: if e:
return e return e
if cls.globalConfig:
e = cls.globalConfig.GetString('core.editor') e = cls.globalConfig.GetString('core.editor')
if e: if e:
return e return e
@ -69,21 +71,32 @@ least one of these before using this command."""
Returns: Returns:
new value of edited text; None if editing did not succeed new value of edited text; None if editing did not succeed
""" """
editor = cls._GetEditor().split() editor = cls._GetEditor()
if editor == ':':
return data
fd, path = tempfile.mkstemp() fd, path = tempfile.mkstemp()
try: try:
os.write(fd, data) os.write(fd, data)
os.close(fd) os.close(fd)
fd = None fd = None
if re.compile("^.*[$ \t'].*$").match(editor):
args = [editor + ' "$@"']
shell = True
else:
args = [editor]
shell = False
args.append(path)
try: try:
rc = subprocess.Popen(editor + [path]).wait() rc = subprocess.Popen(args, shell=shell).wait()
except OSError, e: except OSError, e:
raise EditorError('editor failed, %s: %s %s' raise EditorError('editor failed, %s: %s %s'
% (str(e), cls._GetEditor(), path)) % (str(e), editor, path))
if rc != 0: if rc != 0:
raise EditorError('editor failed with exit status %d: %s %s' raise EditorError('editor failed with exit status %d: %s %s'
% (rc, cls._GetEditor(), path)) % (rc, editor, path))
fd2 = open(path) fd2 = open(path)
try: try: