-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
pull-all
executable file
·92 lines (78 loc) · 2.54 KB
/
pull-all
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
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")"
initial_dir=$(pwd)
# Check if --pull flag is passed
pull_flag=false
if [[ $1 == "--pull" ]]; then
pull_flag=true
fi
handle_dir() {
if [[ -d "$1" ]]; then
cd "$1"
# Format the directory name
dir=$(printf "%-14s" "$(basename $1)")
echo -n "$dir: "
# check if the directory is a git repo
if [[ ! -d ".git" ]]; then
echo "🟡 not a git repo"
cd "$initial_dir"
return
fi
# check if the git repo is dirty
if [[ $(git diff --stat) != '' ]]; then
echo "🟡 repo has uncommitted changes"
cd "$initial_dir"
return
fi
# check if repo has a main branch or master branch
main_or_master=""
if [[ $(git branch --list main) ]]; then
main_or_master="main"
elif [[ $(git branch --list master) ]]; then
main_or_master="master"
else
echo "🟡 no main or master branch"
cd "$initial_dir"
return
fi
# check if repo has an upstream or origin remote
upstream_or_origin=""
if [[ $(git remote -v | grep upstream) ]]; then
upstream_or_origin="upstream"
elif [[ $(git remote -v | grep origin) ]]; then
upstream_or_origin="origin"
else
echo "🟡 no upstream or origin remote"
cd "$initial_dir"
return
fi
git fetch "$upstream_or_origin" "$main_or_master" 2>/dev/null
# if repo is not on main or master branch, don't do anything
current_branch=$(git branch --show-current)
if [[ $current_branch != "$main_or_master" ]]; then
echo "🟣 not on $main_or_master branch ($current_branch)"
cd "$initial_dir"
return
fi
# check if repo is up to date
if [[ $(git rev-list HEAD...$upstream_or_origin/$main_or_master --count) -eq 0 ]]; then
echo "🟢"
cd "$initial_dir"
return
fi
if [[ $pull_flag == true ]]; then
echo -n "pulling changes... "
git pull &>/dev/null && echo "✅ done" || echo "❌ failed"
else
echo "🟡 behind by $(git rev-list HEAD...$upstream_or_origin/$main_or_master --count) commits"
fi
cd "$initial_dir"
fi
}
# check lila-docker itself
handle_dir $(dirname "$(readlink -f "$BASH_SOURCE")")
# check the cloned repos
for dir in ./repos/*; do
handle_dir "$dir"
done