Skip to contentSkip to conversation

git-status: support always/auto/never in colorization

PATCH In review Jeff King 2 participants 2 messages Jun 15, 2016 Documentation

lore
Share & tools
lore

Jeff King

author
#0 lore raw

PATCH 1/1 git-status: support always/auto/never in colorization

---
This is on top of the previous two patches:
git-status: colorize status output (me)
git-status: do not use colors all the time (Matthias)

I was hoping to join the term selection logic with the diff.color logic in git_config_termbool or similar (and a git-repo-config --termbool), but unfortunately that doesn't work since git-repo-config can't do an isatty test (since we call it as git-repo-config). In general, config parsing is a little awkward (and inefficient) in sh. Is there any interest in me converting git-commit/git-status to C builtins (I know Johannes will be happy...)?

 Documentation/config.txt |    4 +++-
 git-commit.sh            |   13 ++++++++-----
 2 files changed, 11 insertions(+), 6 deletions(-)
Documentation/config.txt +3 -1
index 83f4627..43766bd 100644
@@ -224,7 +224,9 @@ showbranch.default::
 
 status.color::
 	A boolean to enable/disable color in the output of
-	gitlink:git-status[1]. Defaults to false.
+	gitlink:git-status[1]. May be set to `true` (or `always`),
+	`false` (or `never`) or `auto`, in which case colors are used
+	only when the output is to a terminal. Defaults to false.
 
 status.color.<slot>::
 	Use customized color for status colorization. `<slot>` is
git-commit.sh +8 -5
index ad0cbb1..2ab1974 100755
@@ -20,11 +20,14 @@ case "$0" in
 *status)
 	status_only=t
 	unmerged_ok_if_status=--unmerged
-	[ "`git-repo-config --bool --get status.color`" = 'true' ] &&
-		([ -t 1 ] || (
-			[ -n "$GIT_PAGER_IN_USE" ] &&
-			[ "`git-repo-config --bool --get pager.color`" != 'false' ]
-		)) && color=true
+	case "`git-repo-config --get status.color`" in
+	  always) color=true ;;
+	  never ) color=false ;;
+	  auto  ) test -t 1 -o \( -n "$GIT_PAGER_IN_USE" -a \
+		     "`git-repo-config --bool --get pager.color`" != false \) \
+		  && color=true ;;
+	  *     ) color="`git-repo-config --bool --get status.color`" ;;
+	esac
 	eval `git-repo-config --get-regexp status.color. \
 	      | while read k v; do
 	          echo color_${k#status.color.}=$v
Signature · 1 line
1.4.2.rc3.gf3bd-dirty
#1 lore raw
Quoted 6 lines · Jeff King

---
This is on top of the previous two patches:
git-status: colorize status output (me)
git-status: do not use colors all the time (Matthias)

Quoted 7 lines

I was hoping to join the term selection logic with the diff.color logic in git_config_termbool or similar (and a git-repo-config --termbool), but unfortunately that doesn't work since git-repo-config can't do an isatty test (since we call it as git-repo-config). In general, config parsing is a little awkward (and inefficient) in sh. Is there any interest in me converting git-commit/git-status to C builtins (I know Johannes will be happy...)?

I was planning to do only the status part in C anyway.