I want to watch
the output of git log --oneline --graph
with all the colors and tried to adapt the solution from Why don't git colours appear when using watch?, but it just renders (nearly) the whole output in orange:
Output using watch -c
$ watch -c git -c color.diff=always log --graph --oneline
Note how from the first commit hash on everything is printed in orange. This is the output of watch from procps-ng 3.3.10
on Ubuntu 16.04.
Normal output = goal
$ git log --graph --oneline
The output with an additional -c color.diff=always
is the same, cat -A
shows the ANSI color sequences in use:
$ git -c color.diff=always log --graph --oneline | cat -A
* ^[[33m8ccd905^[[m L
* ^[[33me4ad4f6^[[m E
* ^[[33mf427d2a^[[m U
* ^[[33me8351c8^[[m Z
* ^[[33mf06747c^[[m I
Question
How can I get watch git log
to show the colors like git log
does?
This was a bug in
procps-ng
(Ubuntu package nameprocps
), fixed in version 3.3.11.Latest Ubuntu LTS (16.04 Xenial) still contains the bug, whereas current regular versions (17.04 Zesty and 17.10 Artful) already ship the fixed version.
The comment by egmont tells about
watch
not recognizing ANSI 39 (reset the foreground color only). It is^[[39m
as pointed by WJAndrea. Others have answered that it is a bug inprocps-ng
.In other git commands, like
diff
, it is noticeable that bold colors work, but any uncolored text that follows keeps the previous color, until a new bold color appears. The reason is that bold colors specify '^[[1;
##m'.So we known where to focus.
First I made a quick regex with
sed
to replace all colors to bold, and was working just fine. It just required a leading bold color, because some git commands output do not start colored and there were a starting regular text until the first color code.I tested during this afternoon, and that massive bold text was taking my attention.
Another problem is that I hard-coded a bold light gray color to replace the uncolored text: people using other color schemes could yell at me.
To solve all, I changed my initial idea to something simpler. Instead of bolding the text, I just need to add a
0
to make the color code valid towatch
. Changed a few characters and voilà! The colors appear as expected, bold texts keep their meaning, and the window hanging around feels better.I tested the performance with a huge commit history (>90k commits), and there is a noticeable delay of some milliseconds, if you use the
--graph
or--date-order
flags, and maybe others, but--oneline
seems safe. Even asking to log only a few commits gives delay, Git has to process the whole history to generate the graph.But that case was extreme. And you don't need a 0.1s update. 3 or 4s looks good.
watch -n #
My current code is the following:
I saved in
~/bin/git-colored
with +x permission and it can be used withgit colored <git command>
Solving the main post:
watch -c 'git colored log --oneline --graph'
I created a gist with this file, where you can see the code history.
TL;DR
There is a bug in a Ubuntu package. The one line code above is a workaround for who can not/don't want to upgrade the system, basically doing what is in here.
A quick workaround I made if you don't have procps-ng 3.3.11: pipe your git output through
sed "s/\x1b\[m/\x1b\[00m/"
, e.g. forgit status
:This replaces empty ANSI escapes (
[m
, not recognized bywatch
) with zeroed ones ([00m
, fully recognized bywatch
).