Browse Source

added gitup script

DanielFGray 2 years ago
parent
commit
a483e90bc4
1 changed files with 98 additions and 0 deletions
  1. 98
    0
      gitup

+ 98
- 0
gitup View File

@@ -0,0 +1,98 @@
1
+#!/usr/bin/env bash
2
+
3
+declare -A colors
4
+colors[red]=$(tput setaf 1)
5
+colors[green]=$(tput setaf 2)
6
+colors[blue]=$(tput setaf 4)
7
+colors[reset]=$(tput sgr0)
8
+declare processes=4
9
+declare quiet
10
+
11
+usage() {
12
+  LESS=-FEXR less <<'HELP'
13
+gitup [dir] [OPTIONS]
14
+
15
+search for git repos and update them
16
+
17
+if unspecified, dir defaults to $HOME
18
+
19
+-p [number]    how many processes to run git pull in parallel
20
+-q             quiet level, may be stacked
21
+               first level suppresses output from `git pull`
22
+               second level suppresses job info
23
+-h             print this help
24
+HELP
25
+}
26
+
27
+color() {
28
+  local c
29
+  c="$1"
30
+  shift
31
+  printf '%s' "${colors[$c]}" >&2
32
+  printf '%s\n' "$@" >&2
33
+  printf '%s' "${colors[reset]}" >&2
34
+}
35
+
36
+err() {
37
+  color red "$@" >&2
38
+}
39
+
40
+die() {
41
+  [[ -n "$1" ]] && err "$1"
42
+  exit 1
43
+}
44
+
45
+has() {
46
+  local verbose
47
+  if [[ $1 = '-v' ]]; then
48
+    verbose=1
49
+    shift
50
+  fi
51
+  for c; do c="${c%% *}"
52
+    if ! command -v "$c" &> /dev/null; then
53
+      (( "$verbose" > 0 )) && err "$c not found"
54
+      return 1
55
+    fi
56
+  done
57
+}
58
+
59
+has -v fzf git || die
60
+
61
+while getopts ":hqp:" x; do
62
+  case "$x" in
63
+    h) usage; exit; ;;
64
+    p) processes="$OPTARG" ;;
65
+    q) (( quiet++ )) ;;
66
+  esac
67
+done
68
+shift $(( OPTIND - 1 ))
69
+
70
+dir="$HOME"
71
+if [[ -d "$1" ]]; then
72
+  dir="$1"
73
+fi
74
+
75
+mapfile -t dirs < <(find -L "$dir" -name '.git' -printf '%h\n' 2> /dev/null \
76
+  | fzf --multi --cycle --inline-info)
77
+
78
+(( ${#dirs[@]} > 0 )) || exit
79
+
80
+update() {
81
+  local name dir
82
+  dir="$1"
83
+  name="${dir##*/}"
84
+  (( quiet > 1 )) || color blue ":: updating $name"
85
+  if git -C "$dir" pull ${quiet:+-q}; then
86
+    (( quiet > 1 )) || color green ":: updated $name"
87
+  else
88
+    color red ":: failed to update $name"
89
+  fi
90
+}
91
+
92
+for d in "${dirs[@]}"; do
93
+  (( count++ >= processes )) && wait -n
94
+  update "$d" &
95
+done
96
+wait
97
+
98
+printf '%d repos updated\n' "${#dirs[@]}"

Loading…
Cancel
Save