3

I'm trying to create a script that would should me the git log and refresh every 1 second. This is what I have:

while :
do
clear
git log --all --decorate --oneline --graph
sleep 1
done

It's not working because git log waits for the user to hit q to quit - and therefor the loop is stuck. How can I fix this?

0

3 Answers 3

6

Based on this answer from SO, you can stop the need to quit by adding --no-pager immediately after the git.

It's not the log command that is waiting for q, it's the less tool that is doing the pagination. By telling git that you don't want the pager, the log command will print all output and then quit immediately.

Of course if there is more than one screen worth, you'll get everything printed out and would have to scroll up to see the most recent commits. You can combat this by limiting the number of commits to log by adding something like -22. That would limit to 22 commits, you could of course pick a different number.

Something like:

while :
do
clear
git --no-pager log --all --decorate --oneline -22 --graph
sleep 1
done
10

You can make that a one-liner with watch:

watch --color -n 3 git log --all --decorate --oneline --graph --color=always

Tweak the -n flag to change the refresh rate.

0

If you want to prevent your screen from flashing due to the execution of each iteration of the loop, I suggest to store the output of git log in a file and to check the difference against the previous iteration before actually re-running the command in your standard output:

GIT_LOG_CMD="git --no-pager log --all --decorate --oneline -22 --graph"
PREV_OUTPUT_FILE="/tmp/git_log_output.txt"

if [ ! -f "$PREV_OUTPUT_FILE" ]; then
  touch "$PREV_OUTPUT_FILE"
else
  > "$PREV_OUTPUT_FILE"
fi

while :
do
  CURRENT_OUTPUT=$($GIT_LOG_CMD)

  if [ "$CURRENT_OUTPUT" != "$(cat $PREV_OUTPUT_FILE)" ]; then
    clear
    $GIT_LOG_CMD
    echo "$CURRENT_OUTPUT" > "$PREV_OUTPUT_FILE"
  fi

  sleep 1
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .