Repository navigation
Expand file tree
/
Copy pathsyncOrg.sh
More file actions
executable file
·175 lines (144 loc) · 4.96 KB
/
Copy pathsyncOrg.sh
File metadata and controls
executable file
·175 lines (144 loc) · 4.96 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
set -u -o pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
ORGANIZATION="vczh-libraries"
REPOSITORIES=(
Vlpp
VlppOS
VlppRegex
VlppReflection
VlppParser2
Workflow
GacUI
GacJS
Release
Tools
wGac
iGac
xgSkins
)
red_error() {
printf '\033[31mERROR: %s\033[0m\n' "$*" >&2
}
resolve_git() {
if command -v git >/dev/null 2>&1 && git --version >/dev/null 2>&1; then
GIT="$(command -v git)"
return 0
fi
local desktop_root="/Applications/GitHub Desktop.app/Contents/Resources/app/git"
if [[ -x "$desktop_root/bin/git" ]]; then
GIT="$desktop_root/bin/git"
export GIT_EXEC_PATH="$desktop_root/libexec/git-core"
export GIT_TEMPLATE_DIR="$desktop_root/share/git-core/templates"
export PATH="$desktop_root/bin:$PATH"
return 0
fi
red_error "Git is unavailable. Install Git or GitHub Desktop before running this script."
return 1
}
has_unpushed_commits() {
local repo_path="$1"
local repo="$2"
local ahead_count
if ! "$GIT" -C "$repo_path" show-ref --verify --quiet refs/remotes/origin/master; then
red_error "$repo has no origin/master reference; unpushed commits cannot be verified."
return 0
fi
if ! ahead_count="$("$GIT" -C "$repo_path" rev-list --count refs/remotes/origin/master..HEAD)"; then
red_error "$repo could not be checked for unpushed commits."
return 0
fi
if [[ "$ahead_count" -ne 0 ]]; then
red_error "$repo has $ahead_count unpushed commit(s)."
return 0
fi
return 1
}
sync_repository() {
local repo="$1"
local repo_path="$ROOT_DIR/$repo"
local repo_url="https://github.com/$ORGANIZATION/$repo.git"
local top_level
local branch
local tracked_changes
local untracked_files
printf '\n==> %s\n' "$repo"
if [[ ! -e "$repo_path" ]]; then
printf 'Cloning %s\n' "$repo_url"
if ! "$GIT" clone "$repo_url" "$repo_path"; then
red_error "$repo could not be cloned; continuing with the next repository."
return 1
fi
elif [[ ! -d "$repo_path" ]]; then
red_error "$repo_path exists but is not a directory; continuing with the next repository."
return 1
fi
if ! top_level="$("$GIT" -C "$repo_path" rev-parse --show-toplevel 2>/dev/null)"; then
red_error "$repo_path is not a Git repository; continuing with the next repository."
return 1
fi
if [[ "$top_level" != "$repo_path" ]]; then
red_error "$repo_path is not the root of its Git repository; continuing with the next repository."
return 1
fi
if ! branch="$("$GIT" -C "$repo_path" symbolic-ref --quiet --short HEAD 2>/dev/null)"; then
red_error "$repo is in detached HEAD state; continuing with the next repository."
return 1
fi
if [[ "$branch" != "master" ]]; then
red_error "$repo is on branch '$branch', not 'master'; continuing with the next repository."
return 1
fi
if ! tracked_changes="$("$GIT" -C "$repo_path" status --porcelain --untracked-files=no)"; then
red_error "$repo could not be checked for uncommitted files; continuing with the next repository."
return 1
fi
if [[ -n "$tracked_changes" ]]; then
red_error "$repo has uncommitted tracked files; continuing with the next repository."
return 1
fi
if ! untracked_files="$("$GIT" -C "$repo_path" ls-files --others --exclude-standard)"; then
red_error "$repo could not be checked for untracked files; continuing with the next repository."
return 1
fi
if [[ -n "$untracked_files" ]]; then
red_error "$repo has untracked files; continuing with the next repository."
return 1
fi
if has_unpushed_commits "$repo_path" "$repo"; then
red_error "Skipping $repo and continuing with the next repository."
return 1
fi
if ! "$GIT" -C "$repo_path" fetch; then
red_error "$repo could not be fetched; continuing with the next repository."
return 1
fi
# Fetch can reveal that the local branch contains commits not present on the
# current remote branch, so check again before allowing pull to create a merge.
if has_unpushed_commits "$repo_path" "$repo"; then
red_error "Skipping $repo after fetch and continuing with the next repository."
return 1
fi
if ! "$GIT" -C "$repo_path" pull origin master; then
red_error "$repo could not be pulled; continuing with the next repository."
return 1
fi
printf '%s is synchronized.\n' "$repo"
return 0
}
if ! resolve_git; then
exit 1
fi
failed=0
for repo in "${REPOSITORIES[@]}"; do
if ! sync_repository "$repo"; then
failed=1
fi
done
printf '\n'
if [[ "$failed" -ne 0 ]]; then
red_error "One or more repositories were not synchronized."
exit 1
fi
printf 'All repositories are synchronized.\n'