quitting git log from a bash script
Asked Answered
W

3

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.

Watersick answered 1/10, 2012 at 16:10 Comment(0)
K
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

Kaleb answered 1/10, 2012 at 16:15 Comment(6)
Added explanations about | cat -Unexacting
awesome! that's the ticket! but i dont understand why. what exactly is "cat -" doing? and why is it overriding the pager?Watersick
It forces STDOUT to pass by catUnexacting
oh i get it! i missed the fact that you were piping all of git log INTO cat! thanx!!!!Watersick
This creates an unnecessary extra process while resetting the PAGER variable doesn'tRaneeraney
why mess with env variables if you can just say in your command that you don't want a pagerMediatorial
M
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"
Mediatorial answered 1/10, 2012 at 16:16 Comment(5)
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)[mWatersick
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.Mediatorial
had to confirm - works also without issues with cygwin bash + other environments. No experience with mingw though.Mediatorial
weird. i'm using console2 (sourceforge.net/projects/console) as a frontend to mingw. i wounder if that's causing it. i still upvoted your post.Watersick
maybe related to this, custom colors of console2? #2445503 - though you probably don't need it as it's working with cat, tooMediatorial
K
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

Kaleb answered 1/10, 2012 at 16:15 Comment(6)
Added explanations about | cat -Unexacting
awesome! that's the ticket! but i dont understand why. what exactly is "cat -" doing? and why is it overriding the pager?Watersick
It forces STDOUT to pass by catUnexacting
oh i get it! i missed the fact that you were piping all of git log INTO cat! thanx!!!!Watersick
This creates an unnecessary extra process while resetting the PAGER variable doesn'tRaneeraney
why mess with env variables if you can just say in your command that you don't want a pagerMediatorial
R
2

setting, in your script:

export PAGER=

would do the trick

Raneeraney answered 1/10, 2012 at 16:12 Comment(1)
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.Watersick

© 2022 - 2024 — McMap. All rights reserved.