aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/git/check-repos.sh
blob: 5180fc9c479c4e41ea7fde40c2c79a501490a72a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /bin/bash

LIST=~/.repos
[[ $1 == -n ]] && {
  COUNT_ONLY=true 
  count=0
}


# Only works when file ~/.repos exists and readable
if [ ! -r $LIST ]; then
  echo File ~/.repos not found/readable
  exit 1
fi


while read repo remote; do
  [[ "$repo" =~ ^[[:space:]]*#.* ]] && continue

  # In case repo is consists of variable like $HOME
  # Use eval to get git information
  eval cd $repo 2>/dev/null || {
    echo Repo $repo is inaccessible
    exit 1
  }

  # Changes in working dir, not yet to be a commit
  changes="$(git -c color.status=always status --short)"

  # Diff between from local repo and remote
  cherry="$([ -n "`git remote`" ] && git cherry)"

  if [[ $COUNT_ONLY == true ]]; then
    # If '-n' is specified, only count repo with changes/local-diff
    [[ -n "$changes" || -n "$cherry" ]] && (( count++ ))
  else
    # Or, just print their status
    echo Check $repo
    [[ -n "$changes" ]] && echo "$changes"
    [[ -n "$cherry" ]] && echo -e "\e[31m[ahead]\e[0m"
  fi
done <$LIST


# If '-n' is specified, print number of repos with changes/local-diff
[[ $COUNT_ONLY == true ]] && echo $count

exit 0