7

i'm trying to write a 'live git log' bash script. here's the code so far:

#!/bin/sh
while true;
do
    clear
    git log --graph -10 --all --color --date=short --pretty=format:"%Cred%x09%h %Creset%ad%Cblue%d %Creset %s %C(bold)(%an)%Creset"
    sleep 3
done

my problem is that git log uses a pager and you have to press q to quit or it will just sit there forever. is there a way to code the quit command in bash? i tried echoing q, with no luck. (i saw another post here that suggested echo "q" > /dev/console -- but there is no dev console in my environment)

system: win7 box - emulating bash with mingw (1.7.6.msysget.0)

UPDATE

here's the finished script

#!/bin/sh
while true;
do
    clear
    git log \
    --graph \
    --all \
    --color \
    --date=short \
    -40 \
    --pretty=format:"%C(yellow)%h%x20%C(white)%cd%C(green)%d%C(reset)%x20%s%x20%C(bold)(%an)%Creset" |
    cat -
    sleep 15
done

the -40 is a personal taste. change it to whatever number suits you and your terminal screen size.

xero
  • 4,077
  • 22
  • 39

3 Answers3

23

Adding --no-pager is the way to go.:

git --no-pager log

So the full command would be

git --no-pager log --graph -10 --all --color --date=short --pretty=format:"%Cred%x09%h %Creset%ad%Cblue%d %Creset %s %C(bold)(%an)%Creset"
eis
  • 51,991
  • 13
  • 150
  • 199
  • 1
    ok, that's a start. i had not heard of the --no-pager flag. but try calling that command. the formatting is totally gone, and there are (i assume) escape characters in the output. completely useless output. * [31m 4c29b0d [m2012-10-01[34m (HEAD) [m updated test.php [1m(xero)[m – xero Oct 01 '12 at 16:21
  • it works fine on my Windows 7 box. You have something messed up on your terminal config I guess? I am using it with command prompt though, not through mingw. – eis Oct 01 '12 at 16:24
  • had to confirm - works also without issues with cygwin bash + other environments. No experience with mingw though. – eis Oct 01 '12 at 16:32
  • weird. i'm using console2 (http://sourceforge.net/projects/console/) as a frontend to mingw. i wounder if that's causing it. i still upvoted your post. – xero Oct 01 '12 at 16:48
  • maybe related to this, custom colors of console2? http://stackoverflow.com/questions/2445003/how-do-i-enable-msysgit-colored-output-when-using-console2 - though you probably don't need it as it's working with cat, too – eis Oct 01 '12 at 17:02
5

Try the following code :

git log \
    --graph -10 \
    --all \
    --color \
    --date=short \
    --pretty=format:"%Cred%x09%h %Creset%ad%Cblue%d %Creset %s %C(bold)(%an)%Creset" |
    cat -

edit

| cat - is not specific to git, that works in each use cases when you have a pager and you'd want to print to STDOUT

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
2

setting, in your script:

export PAGER=

would do the trick

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • i tired that after the shebang line and in the while loop, with no luck. actually that looks like it has no effect at all. i tired setting it to nothing, less, more, and askodjaskdlj (which should cause an error i would assume) but i get nothing different in my results. – xero Oct 01 '12 at 16:17