-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha.cli
executable file
·287 lines (242 loc) · 8.97 KB
/
a.cli
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Default values
DEFAULT_ZOTERO_USER_ID=""
DEFAULT_ZOTERO_API_KEY=""
DEFAULT_ZOTERO_GROUP_ID="4678293"
DEFAULT_BIBTEX_FILE="references.bib"
COLLECTION_ID=""
# Read environment variables or set defaults
ZOTERO_USER_ID=${ZOTERO_USER_ID:-$DEFAULT_ZOTERO_USER_ID}
ZOTERO_API_KEY=${ZOTERO_API_KEY:-$DEFAULT_ZOTERO_API_KEY}
ZOTERO_GROUP_ID=${ZOTERO_GROUP_ID:-$DEFAULT_ZOTERO_GROUP_ID}
BIBTEX_FILE=${BIBTEX_FILE:-$DEFAULT_BIBTEX_FILE}
# Function to parse command-line arguments
parse_args() {
while [[ "$#" -gt 0 ]]; do
case $1 in
--zotero-user-id) ZOTERO_USER_ID="$2"; shift ;;
--zotero-api-key) ZOTERO_API_KEY="$2"; shift ;;
--zotero-group-id) ZOTERO_GROUP_ID="$2"; shift ;;
--bibtex-file) BIBTEX_FILE="$2"; shift ;;
--help)
echo "Usage: $0 [options] {setup|create|list|delete|update-bibtex} [version]"
echo "Options:"
echo " --zotero-user-id Zotero User ID"
echo " --zotero-api-key Zotero API Key"
echo " --zotero-group-id Zotero Group ID (optional, omit if using user library)"
echo " --bibtex-file Path to the BibTeX file (default: your_bibtex_file.bib)"
echo "Commands:"
echo " setup : Setup hooks for commit, checkout, and merge"
echo " create : Create a new release with the provided version"
echo " list : List all existing releases"
echo " delete : Delete the release with the provided version"
echo " update-bibtex : Fetch and update the BibTeX file from Zotero"
echo " version : Optional argument specifying the version for create and delete commands"
exit 0 ;;
esac
shift
done
}
# Function to setup hooks
setup() {
for i in commit checkout merge; do
cp hooks/post-commit .git/hooks/post-$i
chmod +x .git/hooks/post-$i
done
echo "Hooks setup successfully."
git checkout
if ! test -f .git/gitHeadInfo.gin; then
cp .git/gitHeadInfo.gin gitHeadLocal.gin
git add gitHeadLocal.gin
git commit -m "Created gitHeadLocal.gin for initial setup"
fi
}
# Function to create a release
create_release() {
VERSION=$1
if [ -z "$VERSION" ]; then
echo "Usage: $0 create vx.y.z"
exit 1
fi
# Check if the tag already exists
if git rev-parse "$VERSION" >/dev/null 2>&1; then
echo "Error: Tag $VERSION already exists."
exit 1
fi
# Check the format of the version number
if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+\.[0-9]+)?$ ]]; then
echo "Error: Version number should be in the format vx.y.z or vx.y.z-pre.a."
exit 1
fi
# Tag the repository with the provided version
git tag -a "$VERSION" -m "Release $VERSION"
# Checkout the tag to trigger post-commit hook to update gitinfo2 info file
git checkout
# Show the reltag line of .git/gitHeadInfo.gin
grep reltag .git/gitHeadInfo.gin
cp .git/gitHeadInfo.gin gitHeadLocal.gin
git add gitHeadLocal.gin
git commit -m "Updated gitHeadLocal.gin for release $VERSION"
git tag -f -a "$VERSION" -m "Release $VERSION"
# Push the changes and the tags
git push origin --follow-tags
echo "Release $VERSION created and pushed successfully."
}
# Function to list releases
list_releases() {
git tag --sort=-creatordate | head -n $1
}
# Function to clean latex build files
clean() {
# clean latex build files
rm -f *.aux *.bbl *.blg *.log *.out *.pyg *.fls *.synctex* *.toc *.fdb_latexmk *.fls *.idx *.ilg *.ind *.chl *.lof *.lot *.pdf
}
# Function to delete a release
delete_release() {
VERSION=$1
if [ -z "$VERSION" ]; then
echo "Usage: $0 delete vx.y.z"
exit 1
fi
# Delete the tag locally
git tag -d "$VERSION"
# Delete the tag remotely
# git push origin --delete "$VERSION"
echo "Release $VERSION deleted successfully."
}
# Function to fetch and update BibTeX file from Zotero
update_bibtex() {
if [ -z "$ZOTERO_GROUP_ID" ]; then
if [ -z "$ZOTERO_USER_ID" ]; then
echo "Zotero group ID and user ID are not set, one of them is required."
echo "If Group ID is set, User ID is not required."
exit 1
fi
fi
if [ -z "$ZOTERO_API_KEY" ]; then
echo "Zotero API key is not set."
exit 1
fi
echo "Fetching BibTeX entries from Zotero..."
# Define the URL to fetch BibTeX
if [ -z "$ZOTERO_GROUP_ID" ]; then
URL="https://api.zotero.org/users/$ZOTERO_USER_ID/items?format=biblatex"
else
URL="https://api.zotero.org/groups/$ZOTERO_GROUP_ID/items?format=biblatex"
fi
start=0
limit=100
has_more=true
echo "" > "$BIBTEX_FILE"
while $has_more; do
response=$(curl -s -H "Zotero-API-Key: $ZOTERO_API_KEY" "$URL&start=$start&limit=$limit")
if [ -z "$response" ]; then
echo "No more items to fetch."
has_more=false
break
fi
echo "$response" >> "$BIBTEX_FILE"
# Check if we need to fetch more items
num_items=$(echo "$response" | grep -c "@") # Counting the number of BibTeX entries
if [ "$num_items" -lt "$limit" ]; then
has_more=false
else
start=$((start + limit))
fi
done
echo "BibTeX entries updated successfully in $BIBTEX_FILE."
}
update_bibtex2() {
if [ -z "$ZOTERO_GROUP_ID" ]; then
if [ -z "$ZOTERO_USER_ID" ]; then
echo "Zotero group ID and user ID are not set, one of them is required."
echo "If Group ID is set, User ID is not required."
exit 1
fi
fi
if [ -z "$ZOTERO_API_KEY" ]; then
echo "Zotero API key is not set."
exit 1
fi
echo "Fetching BibTeX entries from Zotero..."
# Define the URL to fetch BibTeX
if [ -z "$ZOTERO_GROUP_ID" ]; then
URL="https://api.zotero.org/users/$ZOTERO_USER_ID/items?format=bibtex"
else
URL="https://api.zotero.org/groups/$ZOTERO_GROUP_ID/items?format=bibtex"
fi
start=0
limit=100
has_more=true # Initialize the loop control variable
echo "" > "$BIBTEX_FILE"
while $has_more; do
# Send the request
response=$(curl -s -w "%{http_code}" -H "Zotero-API-Key: $ZOTERO_API_KEY" "$URL&start=$start&limit=$limit")
# Separate the response content and the status code
http_code=$(echo "$response" | tail -n1)
content=$(echo "$response" | sed '$d') # Everything except the last line (status code)
# Check if the request was successful (status code 200)
if [ "$http_code" -ne 200 ]; then
echo "Error fetching data: HTTP status $http_code"
exit 1
fi
if [ -z "$content" ]; then
echo "No more items to fetch."
has_more=false
break
fi
# Append the content to the BibTeX file
echo "$content" >> "$BIBTEX_FILE"
# Check if there are more items by inspecting the Link header or counting the items
num_items=$(echo "$content" | grep -c "@") # Counting BibTeX entries
echo "downloaded $num_items items"
if [ "$num_items" = "0" ]; then
has_more=false
else
start=$((start + limit))
fi
echo "start=$start, has_more=$has_more"
done
echo "BibTeX entries updated successfully in $BIBTEX_FILE."
}
# Main script logic
parse_args "$@"
case "$1" in
setup)
setup
;;
clean)
clean
;;
create)
create_release "$2"
;;
list)
list_releases "${2:-5}"
;;
delete)
delete_release "$2"
;;
update-bibtex)
update_bibtex2
;;
*)
echo "Usage: $0 [options] {setup|create|list|delete|update-bibtex} [version]"
echo "Options:"
echo " --zotero-user-id Zotero User ID (optional, omit if using group id given)"
echo " --zotero-api-key Zotero API Key"
echo " --zotero-group-id Zotero Group ID (optional, omit if using user id given)"
echo " --bibtex-file Path to the BibTeX file (default: your_bibtex_file.bib)"
echo "Commands:"
echo " setup : Setup hooks for commit, checkout, and merge"
echo " clean : Clean latex build files"
echo " create : Create a new release with the provided version"
echo " list : List all existing releases"
echo " delete : Delete the release with the provided version"
echo " update-bibtex : Fetch and update the BibTeX file from Zotero"
echo " version : Optional argument specifying the version for create and delete commands"
exit 1
;;
esac