-
Notifications
You must be signed in to change notification settings - Fork 1
/
pullandbuild.sh
executable file
·205 lines (153 loc) · 4.96 KB
/
pullandbuild.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
packages_dir="Cosas guays LaTeX"
packages_changed=false
repo_dir="repo"
failed=""
updated=""
cd "$(dirname ${BASH_SOURCE[0]})"
cd $repo_dir
repo_path=$(git remote -v | grep fetch | awk '{print $2}' | head -n 1 | sed 's#.*[:/]\(.*\)/\(.*\).git#\1/\2#g')
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
function packages_install() {
echo "Installing packages..."
cd "$packages_dir"
./install
}
function prebuild() {
mkdir -p tikzgen
}
function build() {
latexmk -pdf -silent -shell-escape "$1" &> /dev/null
}
function report_commit() {
bad_commit="$1"
msg_contents="$2"
commit_api_url="https://api.github.com/repos/$repo_path/commits/$bad_commit"
echo "Retrieving author... $(commit_api_url)"
author_ghname="$(curl $commit_api_url | grep login | awk '{print $2}' | tr -d '"' | tr -d ',' | head -n 1)"
msg_contents="$(echo -e $msg_contents | sed "s/AUTHOR/$author_ghname/g" | sed "s/COMMIT/$bad_commit/g")"
rvm $RUBYVER do /usr/local/bin/ghi open -m "$(echo -e "$msg_contents")"
echo $msg_contents
}
function check_conflicts() {
dir=$(basename "$(pwd)")
texfile="$dir/$1"
cd ..
if ! grep "<<< HEAD" "$texfile" &> /dev/null ; then
return
fi
echo "Conflict markers present in $texfile."
git checkout master@{1}
if ! grep "<<< HEAD" "$texfile" &> /dev/null ; then
echo "Conflict master present before this pull"
git checkout master
cd "$dir"
return
fi
git bisect start master master@{1}
git bisect run grep "<<< HEAD" "$texfile"
bad_commit=$(git rev-parse HEAD)
author=$(git --no-pager show -s --format='%an <%ae>' HEAD)
markers=$(grep -n -C 3 "<<< HEAD" "$texfile" | sed 's/\\/\\\\/g' | iconv -f latin1 -t ascii//TRANSLIT)
git bisect reset
git checkout master
msg_title="Marcadores de conflicto en $texfile"
msg_contents="Marcadores introducidos en commit COMMIT por @AUTHOR"
errors_msg="Marcadores: \n\n \`\`\`\n$markers \n \`\`\` \n"
report_commit $bad_commit "$msg_title\n $msg_contents \n\n $errors_msg \n Mensaje creado automáticamente."
}
function report_build_failed() {
texfile="$1"
[ ! -z "$texfile" ] || return 0
logfile=$(echo "$texfile" | sed 's/.tex/.log/')
errors=$(cat "$logfile" | grep -A 4 '!' | sed 's/\\/\\\\/g' | iconv -f latin1 -t ascii//TRANSLIT)
git checkout master@{1}
target_dir=$(basename "$(pwd)")
if ! latexmk -pdf -silent -shell-escape "$texfile" &> /dev/null; then
echo "Error present before this pull."
git checkout master
return
fi
cd ..
git checkout master
git bisect start master master@{1}
git bisect run bash -c "cd \"$target_dir\"; latexmk -g -pdf -silent -shell-escape \"$texfile\""
bad_commit=$(git rev-parse HEAD)
author=$(git --no-pager show -s --format='%an <%ae>' HEAD)
git bisect reset
cd "$target_dir"
if ! latexmk -pdf -silent -shell-escape "$texfile" &> /dev/null; then
echo "Error not introduced in this commit. Possibly caused by a merge..."
git checkout master
return
fi
latexmk -C
commit_api_url="https://api.github.com/repos/$repo_path/commits/$bad_commit"
echo "Retrieving author name... ($commit_api_url)"
author_ghname="$(curl $commit_api_url | grep login | awk '{print $2}' | tr -d '"' | tr -d ',' | head -n 1)"
msg_title="Fallo de compilación en $texfile"
msg_contents="Error introducido en commit $bad_commit por @$author_ghname."
errors_msg="Log de error: \n\n \`\`\`\n$errors \n \`\`\` \n"
report_commit $bad_commit "$msg_title\n $msg_contents \n\n $errors_msg \n Mensaje creado automáticamente."
}
echo "Latex CD build start $(date)"
if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]]; then
echo "Dirty repo. stashing changes."
git stash save "auto stash $(date)"
fi
echo "Pull from remote repo at $(date)"
git pull
changes=$(git diff --name-only master@{1})
if echo $changes | grep "$packages_dir" &> /dev/null; then
echo "Packages changed."
cwd=$(pwd)
packages_changed=true
packages_install
cd "$cwd"
fi
if [ "$1" = "--force" ]; then
packages_changed=true
fi
cd ..
db_token=$(cat dbtoken)
IFS=$'\n'
dir_num=0
dir_upd=0
dir_err=0
for texfile in $(ls $repo_dir/*/*.tex); do
cwd=$(pwd)
cd "$(dirname $texfile)"
(( dir_num += 1))
echo "Checking $texfile..."
texfile="$(basename $texfile)"
if [ "$packages_changed" = true ]; then
latexmk -C
fi
prebuild
if ! latexmk -pdf -r "$cwd/uptodatecheck.latexmkrc" "$texfile" &>/dev/null ; then
echo "$texfile out of date. Compiling..."
if build "$texfile" ; then
echo "$texfile compile successful."
(( dir_upd += 1))
updated="$updated $texfile"
echo "Uploading $texfile..."
wd=$(pwd)
cd "$cwd"
rvm $RUBYVER do bundle exec ruby dbupload.rb "$db_token" "$wd/${texfile/.tex/.pdf}"
cd "$wd"
else
(( dir_err += 1))
echo "Compilation failed for $texfile"
report_build_failed "$texfile"
failed="$failed $texfile"
fi
fi
check_conflicts "$texfile"
cd "$cwd"
echo
done
echo "Found $dir_num courses, updated $dir_upd, failed $dir_err."
[[ -z "$failed" ]] || echo "Compilation failed for $failed "
[[ -z "$updated" ]] || echo "Updated $updated"
echo "done: $(date)"