-
Notifications
You must be signed in to change notification settings - Fork 1
/
code-updates.sh
executable file
·78 lines (67 loc) · 2.06 KB
/
code-updates.sh
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
#!/usr/bin/env bash
# Author: github.com/danielhoherd
# License: MIT
# Purpose: Iterate code dirs and pull svn and git updates
usage() {
echo "Usage: ${0##*/} [-a] [-x] [zero or more code_workspaces]"
echo " -a Force git gc --aggressive"
echo " -p Number of parallel jobs to run"
echo " -x Enable xtrace"
}
PARALLEL=3
while getopts :ahnp:x option ; do
case "${option}" in
a) aggressive=yes ;;
n) aggressive=no ;;
p) PARALLEL="$OPTARG" ;;
x) set -x ;;
h|*) usage ; exit 1 ;;
esac
done
shift $((OPTIND - 1))
export GC_TYPE='--auto'
export GIT_SSH_COMMAND="ssh -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o KbdInteractiveDevices=no"
trap "kill 0" SIGINT
if [[ $((RANDOM % 500)) -eq 9 ]] || [[ "${aggressive}" == 'yes' ]] ; then
GC_TYPE='--aggressive'
date "+%FT%T%z 'git gc ${GC_TYPE}' selected"
( set -x ; pre-commit gc ; )
fi
for host in gitlab.com github.com ; do
ssh-keyscan "$host" 2>/dev/null |
while read -r line ; do
grep -q "$line" ~/.ssh/known_hosts || echo "$line" >> ~/.ssh/known_hosts
done
done
code_update() {
echo "$(
exec 2>&1
repo=$1
if [[ -d "${repo}" ]] ; then
cd "${repo}" || ( echo "ERROR: cannot cd to ${repo}" ; return 1 ; )
date "+%FT%T%z ${repo}"
if [[ -e .git ]] ; then
rm -f .git/hooks/*.sample
git remote | xargs -r -I {} git remote set-head {} -a
if git config --get remote.origin.url > /dev/null ; then
git pull -q || echo "$(date '+%FT%T%z') Problems with ${PWD}"
git remote prune origin
git gc "${GC_TYPE}"
if [[ -f .pre-commit-config.yaml ]] ; then pre-commit install --install-hooks >/dev/null ; fi
else
date "+%FT%T%z Skipping $repo, remote origin is not working: $?"
fi
elif [[ -e .svn ]] ; then
svn up
fi
fi
)"
}
export -f code_update
DIRS="${HOME}/code"
if [[ "$#" -gt 0 ]] ; then
DIRS=( "$@" )
fi
find "${DIRS[@]}" -mindepth 1 -maxdepth 1 -type d -print0 |
sort -z |
xargs -r -P"${PARALLEL}" -0 -I{} bash -c "code_update {}" \;