Use the default git colouring scheme rather than specific scripts
authorCatalin Marinas <catalin.marinas@gmail.com>
Sat, 30 May 2009 12:13:26 +0000 (13:13 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Sat, 30 May 2009 12:13:26 +0000 (13:13 +0100)
This patch adds the mechanism to check if the output is tty for the
diff and show commands and passes the --color option to git if the
color.diff config option is set auto or true. The patch also changes the
default pager to 'less -FRSX' from the diffcol.sh script.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
contrib/diffcol.sh [deleted file]
examples/gitconfig
setup.py
stgit/commands/common.py
stgit/commands/diff.py
stgit/commands/show.py
stgit/config.py

diff --git a/contrib/diffcol.sh b/contrib/diffcol.sh
deleted file mode 100755 (executable)
index eecc87a..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-#!/bin/bash
-
-# Code copied from Quilt (http://savannah.nongnu.org/projects/quilt)
-#
-# Copyright 2006 - the Quilt authors
-#
-#  This script is free software; you can redistribute it and/or modify
-#  it under the terms of the GNU General Public License version 2 as
-#  published by the Free Software Foundation.
-
-setup_colors()
-{
-       local C="diffhdr=1;36:diffhdradd=1;32:diffadd=32:diffhdrmod=1;35:diffmod=35:diffhdrrem=1;31:diffrem=31:diffhunk=36:diffctx=34:diffcctx=33:default=0"
-       [ -n "$DIFF_COLORS" ] && C="$C:$DIFF_COLORS"
-
-       C=${C//=/=\'$'\e'[}
-       C=col${C//:/m\'; col}m\'
-       #coldefault=$(tput op)
-       eval $C
-}
-
-setup_colors
-
-gawk '{
-       if (/^(Index:|diff --git) /)
-               print "'$coldiffhdr'" $0 "'$coldefault'"
-       else if (/^======*$/)
-               print "'$coldiffhdr'" $0 "'$coldefault'"
-       else if (/^\+\+\+/)
-               print "'$coldiffhdradd'" $0 "'$coldefault'"
-       else if (/^\*\*\*/)
-               print "'$coldiffhdrmod'" $0 "'$coldefault'"
-       else if (/^---/)
-               print "'$coldiffhdrrem'" $0 "'$coldefault'"
-       else if (/^(\+|new( file)? mode )/)
-               print "'$coldiffadd'" $0 "'$coldefault'"
-       else if (/^(-|(deleted file|old) mode )/)
-               print "'$coldiffrem'" $0 "'$coldefault'"
-       else if (/^!/)
-               print "'$coldiffmod'" $0 "'$coldefault'"
-       else if (/^@@ \-[0-9]+(,[0-9]+)? \+[0-9]+(,[0-9]+)? @@/)
-               print gensub(/^(@@[^@]*@@)([ \t]*)(.*)/,
-                       "'$coldiffhunk'" "\\1" "'$coldefault'" \
-                       "\\2" \
-                       "'$coldiffctx'" "\\3" "'$coldefault'", "")
-       else if (/^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/)
-               print "'$coldiffcctx'" $0 "'$coldefault'"
-       else {
-               print
-       }
-}' $1 | less -R -S
index f6e3a79..e235e14 100644 (file)
@@ -42,8 +42,8 @@
        #editor = /usr/bin/vi
 
        # this value overrides the default PAGER environment variable
-       #pager = ~/share/stgit/contrib/diffcol.sh
-       #pager = filterdiff --annotate | colordiff | less -FRX
+       #pager = less -FRSX
+       #pager = filterdiff --annotate | colordiff | less -FRSX
 
        # GIT pull and fetch commands (should take the same arguments as
        # git fetch or git pull).  By default:
index fb67958..73ce2e5 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -58,8 +58,7 @@ def __run_setup():
             ('share/stgit/templates', glob.glob('templates/*.tmpl')),
             ('share/stgit/examples', glob.glob('examples/*.tmpl')),
             ('share/stgit/examples', ['examples/gitconfig']),
-            ('share/stgit/contrib', ['contrib/diffcol.sh',
-                                     'contrib/stgbashprompt.sh']),
+            ('share/stgit/contrib', ['contrib/stgbashprompt.sh']),
             ('share/stgit/completion', ['stgit-completion.bash'])
             ])
 
index dfd7e26..1bc162f 100644 (file)
@@ -97,6 +97,15 @@ def git_commit(name, repository, branch_name = None):
     except libgit.RepositoryException:
         raise CmdException('%s: Unknown patch or revision name' % name)
 
+def color_diff_flags():
+    """Return the git flags for coloured diff output if the configuration and
+    stdout allows."""
+    stdout_is_tty = (sys.stdout.isatty() and 'true') or 'false'
+    if config.get_colorbool('color.diff', stdout_is_tty) == 'true':
+        return ['--color']
+    else:
+        return []
+
 def check_local_changes():
     if git.local_changes():
         raise CmdException('local changes in the tree. Use "refresh" or'
index 568651c..a6cd013 100644 (file)
@@ -68,6 +68,8 @@ def func(parser, options, args):
         rev1 = 'HEAD'
         rev2 = None
 
+    if not options.stat:
+        options.diff_flags.extend(color_diff_flags())
     diff_str = git.diff(args, git_id(crt_series, rev1),
                         rev2 and git_id(crt_series, rev2),
                         diff_flags = options.diff_flags)
index 895943a..b7a8aa9 100644 (file)
@@ -61,6 +61,7 @@ def func(parser, options, args):
         # individual patches or commit ids
         patches = args
 
+    options.diff_flags.extend(color_diff_flags())
     commit_ids = [git_id(crt_series, patch) for patch in patches]
     commit_str = '\n'.join([git.pretty_commit(commit_id,
                                               flags = options.diff_flags)
index dc9c9eb..b6d32b5 100644 (file)
@@ -37,7 +37,8 @@ class GitConfig:
         'stgit.autoimerge':    'no',
         'stgit.keepoptimized': 'no',
         'stgit.extensions':    '.ancestor .current .patched',
-        'stgit.shortnr':        '5'
+        'stgit.shortnr': '5',
+        'stgit.pager':  'less -FRSX'
         }
 
     __cache = None
@@ -109,16 +110,18 @@ class GitConfig:
             if m:
                 result.append(m.group(1))
         return result
+
+    def get_colorbool(self, name, stdout_is_tty):
+        """Invoke 'git config --get-colorbool' and return the result."""
+        return Run('git', 'config', '--get-colorbool', name,
+                   stdout_is_tty).output_one_line()
         
 config=GitConfig()
 
 def config_setup():
     global config
 
-    # Set the PAGER environment to the config value (if any)
-    pager = config.get('stgit.pager')
-    if pager:
-        os.environ['PAGER'] = pager
+    os.environ.setdefault('PAGER', config.get('stgit.pager'))
     # FIXME: handle EDITOR the same way ?
 
 class ConfigOption: