Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: gclient_utils.py

Issue 14854003: Make git-cl more accurately imitate git's editor selection process, and respect $VISUAL. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Make tests use $GIT_EDITOR instead of $EDITOR (previously they failed for me, as $EDITOR was overriā€¦ Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | git_cl.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Generic utils.""" 5 """Generic utils."""
6 6
7 import codecs 7 import codecs
8 import logging 8 import logging
9 import os 9 import os
10 import Queue 10 import Queue
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 work_queue.exceptions.put(sys.exc_info()) 671 work_queue.exceptions.put(sys.exc_info())
672 logging.info('_Worker.run(%s) done' % self.item.name) 672 logging.info('_Worker.run(%s) done' % self.item.name)
673 673
674 work_queue.ready_cond.acquire() 674 work_queue.ready_cond.acquire()
675 try: 675 try:
676 work_queue.ready_cond.notifyAll() 676 work_queue.ready_cond.notifyAll()
677 finally: 677 finally:
678 work_queue.ready_cond.release() 678 work_queue.ready_cond.release()
679 679
680 680
681 def GetEditor(git): 681 def GetEditor(git, git_editor=None):
682 """Returns the most plausible editor to use.""" 682 """Returns the most plausible editor to use.
683
684 In order of preference:
685 - GIT_EDITOR/SVN_EDITOR environment variable
686 - core.editor git configuration variable (if supplied by git-cl)
687 - VISUAL environment variable
688 - EDITOR environment variable
689 - vim (non-Windows) or notepad (Windows)
690
691 In the case of git-cl, this matches git's behaviour, except that it does not
692 include dumb terminal detection.
693
694 In the case of gcl, this matches svn's behaviour, except that it does not
695 accept a command-line flag or check the editor-cmd configuration variable.
696 """
683 if git: 697 if git:
684 editor = os.environ.get('GIT_EDITOR') 698 editor = os.environ.get('GIT_EDITOR') or git_editor
685 else: 699 else:
686 editor = os.environ.get('SVN_EDITOR') 700 editor = os.environ.get('SVN_EDITOR')
687 if not editor: 701 if not editor:
702 editor = os.environ.get('VISUAL')
703 if not editor:
688 editor = os.environ.get('EDITOR') 704 editor = os.environ.get('EDITOR')
689 if not editor: 705 if not editor:
690 if sys.platform.startswith('win'): 706 if sys.platform.startswith('win'):
691 editor = 'notepad' 707 editor = 'notepad'
692 else: 708 else:
693 editor = 'vim' 709 editor = 'vim'
694 return editor 710 return editor
695 711
696 712
697 def RunEditor(content, git): 713 def RunEditor(content, git, git_editor=None):
698 """Opens up the default editor in the system to get the CL description.""" 714 """Opens up the default editor in the system to get the CL description."""
699 file_handle, filename = tempfile.mkstemp(text=True) 715 file_handle, filename = tempfile.mkstemp(text=True)
700 # Make sure CRLF is handled properly by requiring none. 716 # Make sure CRLF is handled properly by requiring none.
701 if '\r' in content: 717 if '\r' in content:
702 print >> sys.stderr, ( 718 print >> sys.stderr, (
703 '!! Please remove \\r from your change description !!') 719 '!! Please remove \\r from your change description !!')
704 fileobj = os.fdopen(file_handle, 'w') 720 fileobj = os.fdopen(file_handle, 'w')
705 # Still remove \r if present. 721 # Still remove \r if present.
706 fileobj.write(re.sub('\r?\n', '\n', content)) 722 fileobj.write(re.sub('\r?\n', '\n', content))
707 fileobj.close() 723 fileobj.close()
708 724
709 try: 725 try:
710 cmd = '%s %s' % (GetEditor(git), filename) 726 editor = GetEditor(git, git_editor=git_editor)
727 if not editor:
728 return None
729 cmd = '%s %s' % (editor, filename)
711 if sys.platform == 'win32' and os.environ.get('TERM') == 'msys': 730 if sys.platform == 'win32' and os.environ.get('TERM') == 'msys':
712 # Msysgit requires the usage of 'env' to be present. 731 # Msysgit requires the usage of 'env' to be present.
713 cmd = 'env ' + cmd 732 cmd = 'env ' + cmd
714 try: 733 try:
715 # shell=True to allow the shell to handle all forms of quotes in 734 # shell=True to allow the shell to handle all forms of quotes in
716 # $EDITOR. 735 # $EDITOR.
717 subprocess2.check_call(cmd, shell=True) 736 subprocess2.check_call(cmd, shell=True)
718 except subprocess2.CalledProcessError: 737 except subprocess2.CalledProcessError:
719 return None 738 return None
720 return FileRead(filename) 739 return FileRead(filename)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 783
765 Python on OSX 10.6 raises a NotImplementedError exception. 784 Python on OSX 10.6 raises a NotImplementedError exception.
766 """ 785 """
767 try: 786 try:
768 import multiprocessing 787 import multiprocessing
769 return multiprocessing.cpu_count() 788 return multiprocessing.cpu_count()
770 except: # pylint: disable=W0702 789 except: # pylint: disable=W0702
771 # Mac OS 10.6 only 790 # Mac OS 10.6 only
772 # pylint: disable=E1101 791 # pylint: disable=E1101
773 return int(os.sysconf('SC_NPROCESSORS_ONLN')) 792 return int(os.sysconf('SC_NPROCESSORS_ONLN'))
OLDNEW
« no previous file with comments | « no previous file | git_cl.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698