Is there a way to watch a .git folder, and update my git log whenever it changes?

I have been trying to create an automatically updating git log --graph --no-pager. I made some strides using fswatch but I ran into the issue of my git history being actually bigger than my screen can display. I tried adding | head -n $((`tput lines` - 1)) but the output is still overflowing because of line wrapping. I then tried using tput rmam in order to disable wrapping, but now I can't usually see where HEAD points to.

Anyone could help me? Ideally, I'd like to remove --no-pager, but I don't know how to make that work with fswatch. Here is what I have so far:

function gwatch() {
  tput rmam
  location=$(git rev-parse --show-toplevel)
  clear;
  git --no-pager graph --color | head -n $((`tput lines` - 1));
  tput smam

  fswatch -or $location/.git | while read MODFILE
  do
    tput rmam
    clear;
    git --no-pager graph --color | head -n $((`tput lines` - 1));
    tput smam
  done
}

Answers

I have a less elegant solution than yours but simple: using watch.

watch -n 1  "sh -c 'git log | head -n $(tput lines)'"

watch will do the paging for you but you won't be able to browse the log.

But if you want that, you can use the following code:

#!/bin/bash

gitwatch()
{
    while true; do
        sleep 3
        fswatch -xr $location/.git/objects | while read events; do
            if echo $events | grep -q Created; then
                pid=$(pgrep -t $(ps -o tty= -p $$) $PAGER)
                kill $pid
                break
            fi
        done
    done
}

location=$(git rev-parse --show-toplevel)

gitwatch &

while true; do
    git log
done


I put a sleep because we have multiple creations of files in one modification in git. This code needs a better solution to catch a modification in git than watching all git objects!

Instead of -x and then grep with Created, you can use -o --event=Created. However, this approach is less responsive and seems not to work all the time.

Posted on by rools

Relevant tags