~dricottone/my-utils

ref: 09960992096865d9556323ac5cddf4adbac95d55 my-utils/core/gitstat -rwxr-xr-x 1.8 KiB
09960992Dominic Ricottone New shebangs script; fixes to tar utilities and hugo-post script 2 years ago
                                                                                
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash

name="gitstat"
version="1.0"
usage_message="Usage: gitstat"
help_message=$(/usr/bin/cat <<-EOF
	Fetch updates in git repositories
	Usage: gitstat
	Options:
	 -h, --help     print this message and exit
	 -v, --version  print version number and exit
EOF
)

reset_color="\e[0m"
error_color="\e[1;31m"
highlight_color="\e[1;33m"

. /usr/local/lib/myminiparse.sh

if [[ ! -f ~/.config/gitstat/repos.list ]]; then
  /usr/bin/printf "%s: cannot read repository list at '~/.config/gitstat/repos.list'\n" "$name"
fi

while read repo upstream; do
  code=0

  if ! cd "$repo"; then
    /usr/bin/printf "%s: ERROR: local repository '%s' does not exist\n" "$name" "$repo"
    code=1
  fi

  if [[ "$code" -eq 0 && ! -z "$upstream" ]]; then
    if ! /usr/bin/git checkout "$upstream" >/dev/null 2>&1; then
      /usr/bin/printf "%s: ERROR: upstream branch '%s' does not exist in local repository '%s'\n" "$name" "$upstream" "$repo"
      code=1
    fi
  fi

  if [[ "$code" -eq 0 ]]; then
    if ! /usr/bin/git fetch --quiet >/dev/null 2>&1; then
      /usr/bin/printf "%s: ERROR: failed to sync '%s' to upstream\n" "$name" "$repo"
      code=1
    fi
  fi

  if [[ "$code" -eq 0 ]]; then
    updates_count="$(/usr/bin/git status --porcelain=v2 --branch \
      | /usr/bin/grep -F '# branch.ab' \
      | /usr/bin/sed -e 's/^.*-//')"

    if [[ "$updates_count" = "0" ]]; then
      /usr/bin/printf '%s: nothing to do\n' "$repo"
    else
      /usr/bin/printf '%s: %b%s commits to pull%b\n' "$repo" "$highlight_color" "$updates_count" "$reset_color"
    fi
  fi

  if [[ ! -z "$upstream" ]]; then
    if ! /usr/bin/git checkout dev >/dev/null 2>&1; then
      /usr/bin/printf "%s: ERROR: failed to restore dev branch in local repository '%s'\n" "$name" "$repo"
      code=1
    fi
  fi
done < <(/usr/bin/grep --invert-match --extended-regexp '^( *| *#.*)$' ~/.config/gitstat/repos.list)