Gitfetcher

Why I will not do git fetch or git pull ?

Because I have automated it.

gitfetcher.gif

Highly inspired from bash-git-prompt. If you are already using it (or equivalent of it), below custom script may be useful to you.

Problem Statement:

So created a script, which will do git fetch

$ cat /path/to/gitfetcher.sh
#!/usr/bin/env bash

function fetch_in_background() {
  {
    eval "git fetch --quiet" &> /dev/null
  }&
}

GIT_REPO="$PWD/.git"
if [[ -e $GIT_REPO ]]; then
  FETCH_HEAD="$GIT_REPO/FETCH_HEAD"
  if [[ -e "${FETCH_HEAD}" ]]; then
    
    FETCH_TIMEOUT=5 #minutes after which check again
    
    perl -e '((time - (stat("'"${FETCH_HEAD}"'"))[9]) / 60) > '"${FETCH_TIMEOUT}"' && exit(0) || exit(1)'
    x="$?"
    if [[ $x == 0 ]]; then
      if [[ -n $(git remote show) ]]; then
        (
          fetch_in_background
          disown -h
        )
      fi
    fi
  else 
    # echo ".git/FETCH_HEAD not found"
    fetch_in_background
    disown -h
  fi
fi

And include the above script as prompter in your bash/zsh/fish configfile like

### Fetch in background
export PATH="$PATH:path/to/gitfetcher.sh" # configure gitfetcher.sh in the path
export PROMPT_COMMAND="gitfetcher.sh; $PROMPT_COMMAND"
### Fetch in background

You can find the source code of above script here and sample to see how I configured in my bashrc file

Comments

comments powered by Disqus