-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-sync-all
More file actions
executable file
·120 lines (84 loc) · 1.59 KB
/
Copy pathgit-sync-all
File metadata and controls
executable file
·120 lines (84 loc) · 1.59 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace
declare SRC
main() {
set_src "$1"
pushd "${SRC}" >/dev/null
msg1 'Syncing repos...'
sync_groups
popd >/dev/null
}
set_src() {
SRC="$1"
[[ -z "${SRC}" ]] && SRC="${PWD}"
return 0
}
sync_groups() {
local group
while read -r group; do
[[ ! -d "${group}" ]] && continue
pushd "${group}" >/dev/null
sync_group
popd >/dev/null
done < <(ls -A1)
}
sync_group() {
local repo
while read -r repo; do
[[ ! -d "${repo}" ]] && continue
pushd "${repo}" >/dev/null
sync_repo "${repo}"
popd >/dev/null
done < <(ls -A1)
}
sync_repo() {
local -r repo="$1"
! git_is_repo && return 0
! git_is_clean && return 0
msg2 "Syncing '${repo}'..."
if ! git pull --rebase; then
fail $? "Failed to sync repo '${repo}' with 'git pull --rebase'"
fi
return 0
}
git_is_repo() {
git branch &>/dev/null && return 0
return 1
}
git_is_clean() {
git diff --quiet --ignore-submodules HEAD 2>/dev/null
case $? in
0) return 0 ;;
1) return 1 ;;
129) return 1 ;; # not a git repo
esac
}
fail() {
local -r scode="$1"
local -r msg="$2"
err "${msg}"
exit "${scode}"
}
err() {
local -r msg="$1"
echo "[$(timestamp)]: ${msg}" >&2
}
msg1() {
local -r msg="$1"
echo "[$(timestamp)]: ${msg}"
}
msg2() {
local -r msg="$1"
local -r GREEN="$(tput setaf 2)"
local -r RESET="$(tput sgr0)"
echo "${GREEN}${msg}${RESET}"
}
timestamp() {
date +'%Y-%m-%d %H:%M:%S'
# date +'%Y-%m-%dT%H:%M:%S%z'
# date -u +'%Y-%m-%dT%H:%M:%SZ'
}
main "$@"