12

When running git status -sb I see:

enter image description here

I want to watch (from procps-ng 3.3.3) a repository. The --color option is supposed to keep colours.


Interestingly, it works with ls:

$ watch --color "ls --color"

Showing:

output from ls


However for git the colours disappear:

$ watch --color "git status -sb"

enter image description here


So, why does watch show colours from ls but not from git output?

Braiam
  • 66,947
  • 30
  • 177
  • 264
Drew Noakes
  • 5,628
  • 5
  • 41
  • 55

3 Answers3

10

git uses a configuration value to determine whether to show coloured output or not.

For example:

git config --global color.ui auto

This sets the colour setting to auto globally. In auto mode, git will determine whether it's a real terminal before sending colour codes, as Oli suggested.

You can force this global value to always, however a better idea may be to apply it to a particular command:

git -c color.status=always status -sb

Putting it all together:

watch --color git -c color.status=always status -sb
Drew Noakes
  • 5,628
  • 5
  • 41
  • 55
6

The following statements are true:

  • watch runs the command in a new shell, sh.
  • .bashrc aliases ls as ls --color=auto to enable colours.
  • sh doesn't inherit or use bash aliases.

So when watch runs ls, it's not asking for colours, it's just running the plain old version. You can circumvent this but—as aditya points out—you also need to enable colours on watch for it to process them properly.

A working example for ls is:

watch --color -- ls --color=always

If you don't pass --color to watch, you'll see a bunch of ugly colour codes inline.


ls --color is interpreted as ls --color=always.

ls --color=auto does not print colour in watch. This suggests that it's inferring colour support from the terminal itself.

For more on the reason why, we can test if the watch shell thinks its a real terminal:

$ bash -c '[[ -t 1 ]] && echo "real terminal"'
real terminal

$ watch -- "bash -c '[[ -t 1 ]] && echo "real terminal"'"
# ... nothing.

I suspect that some applications are looking at that (or similar) to tell if they should turn on colours or not.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
Oli
  • 289,791
  • 117
  • 680
  • 835
  • 3
    But the OP wants to know why does this not work with `git status -sb`, even though it works with `ls --color`. – jobin Feb 20 '14 at 15:04
  • This makes sense. However there's no alias for `git`. The colouration is [set within git's configuration directly](http://stackoverflow.com/a/13075208/24874). So this doesn't seem to be an aliasing issue. – Drew Noakes Feb 20 '14 at 15:06
  • 1
    Bingo. I'd set my colours to `auto` which, as you correctly surmised, was causing git to avoid the colour output. Using `always` instead has solved this problem. Many thanks! – Drew Noakes Feb 20 '14 at 15:10
0

It works if git (--color) and watch (-c) are told to use colors:

watch -cd "git branch -va --color"
sjas
  • 341
  • 3
  • 10