-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-source.sh
More file actions
executable file
·434 lines (383 loc) · 13.5 KB
/
git-source.sh
File metadata and controls
executable file
·434 lines (383 loc) · 13.5 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env bash
set -e
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
SCRIPT_DIR="$ROOT_DIR/scripts"
source "$ROOT_DIR/hosts/common.sh"
source "$SCRIPT_DIR/lib.sh"
source "$SCRIPT_DIR/lib-db.sh"
source "$SCRIPT_DIR/lib-color.sh"
LIST_BRANCHES=false
LIST_TAGS=false
VERBOSE=false
DEBUG=false
URL_ONLY=false
GENERATE_MXE_MAKEFILE=false
GENERATE_MXE_TESTFILE=false
FORCE_DOWNLOAD=false
GH_MODE="tags"
INPUT=""
ARCHIVE_NAME=
ARCHIVE_SUBDIR=
VERSION= # if possible, is the numeric normalized version in X.Y.Z format
ARCHIVE_VERSION= # is the full version after the package name of a tarball name after the - packagen-archiveversion.tar.gz
# =============================================
# Usage function
# =============================================
print_usage() {
echo "Usage: $0 [OPTIONS] <repo_url_or_owner/repo | direct_archive_url>"
echo
echo "Options:"
echo " -b, --list-branches List all branches of the repository"
echo " -t, --list-tags List all tags of the repository"
echo " -v, --verbose Enable verbose output"
echo " --debug Enable debug output"
echo " --tag=<name> Download a specific tag"
echo " --branch=<name> Download a specific branch"
echo " --commit=<name> Download a specific commit"
echo " --url-only Skip host detection; force use INPUT as direct archive URL"
echo " --generate-mxe-makefile Generate MXE .mk file after download"
echo " --generate-mxe-testfile Generate MXE test file for .mk"
echo " --force Redownload archive even if it exists"
echo " -h, --help Show this help message"
echo
echo "Direct archive URLs:"
echo " You can provide a direct URL to a tarball or zip file."
echo " Example: $0 https://example.com/project/releases/latest.tar.gz"
echo "Only works for unsupported hosts."
echo "For known hosts, use a project URL and optionally specify one of: --branch, --tag, or --commit to select a revision."
}
# =============================================
# Parse arguments
# =============================================
while [[ $# -gt 0 ]]; do
case "$1" in
-b|--list-branches) LIST_BRANCHES=true ;;
-t|--list-tags) LIST_TAGS=true ;;
-v|--verbose) export VERBOSE=true ;;
--debug) export DEBUG=true ;;
--generate-mxe-makefile=*)
export GENERATE_MXE_MAKEFILE=true
MXE_ARGS="${1#*=}"
;;
--generate-mxe-makefile)
export GENERATE_MXE_MAKEFILE=true
MXE_ARGS="default"
;;
--generate-mxe-testfile)
export GENERATE_MXE_TESTFILE=true
;;
--url-only)
URL_ONLY=true
;;
--tag=*)
REQUESTED_TAG="${1#*=}"
;;
--tag)
if [[ -z "$2" || "$2" == -* ]]; then
eecho "Error: --tag requires a tag name"
exit 1
fi
REQUESTED_TAG="$2"
shift
;;
--branch=*)
REQUESTED_BRANCH="${1#*=}"
;;
--branch)
if [[ -z "$2" || "$2" == -* ]]; then
eecho "Error: --branch requires a branch name"
exit 1
fi
REQUESTED_BRANCH="$2"
shift
;;
--commit=*)
REQUESTED_COMMIT="${1#*=}"
;;
--commit)
if [[ -z "$2" || "$2" == -* ]]; then
eecho "Error: --commit requires a commit hash"
exit 1
fi
REQUESTED_COMMIT="$2"
shift
;;
--force) FORCE_DOWNLOAD=true ;;
-h|--help) print_usage; exit 0 ;;
-*) eecho "Unknown option: $1"; exit 1 ;;
*) INPUT="$1" ;;
esac
shift
done
[ -n "$INPUT" ] || { print_usage; exit 1; }
REF_OPTION_COUNT=0
USED_REFS=()
add_ref() {
((REF_OPTION_COUNT+=1))
USED_REFS+=("$1")
}
if [[ "$REQUESTED_TAG" =~ ^[0-9a-f]{7,40}$ ]]; then
eecho "Error: ambiguous tag looks like commit hash. Use --commit instead."
exit 1
fi
if [[ -n "$REQUESTED_BRANCH" ]]; then
if is_valid_git_commit_hash "$REQUESTED_BRANCH"; then
eecho "Error: --branch cannot be a commit hash. Use --commit instead."
exit 1
fi
add_ref "--branch"
fi
[[ -n "${REQUESTED_COMMIT:-}" ]] && {
is_valid_git_commit_hash "$REQUESTED_COMMIT" || {
eecho "Invalid commit hash: $REQUESTED_COMMIT"
exit 1
}
add_ref "--commit"
}
if (( REF_OPTION_COUNT > 1 )); then
eecho "Error: multiple ref selectors provided: ${USED_REFS[*]}"
eecho "Use only one of: --tag, --branch, or --commit"
exit 1
fi
# =============================================
# Host detection
# =============================================
HOST=""
OWNER_REPO=""
GIT_URL=""
if ! $URL_ONLY; then
for host in $(get_known_hosts); do
HOST_SCRIPT="$ROOT_DIR/hosts/$host.sh"
[[ -f "$HOST_SCRIPT" ]] || continue
source "$HOST_SCRIPT"
if detect_host "$INPUT"; then
HOST="$host"
break
elif [[ $? -eq 2 ]]; then
exit 1
fi
done
fi
if [[ -z "$HOST" ]]; then
# fallback: URL-only
source "$ROOT_DIR/hosts/url-only.sh"
if detect_host_url_only "$INPUT"; then
HOST="url-only"
iecho "Using generic URL-only handler for: $INPUT"
else
eecho "Unsupported host in URL '$INPUT'"
exit 1
fi
fi
# =============================================
# Final check
# =============================================
if [[ -z "$HOST" ]]; then
eecho "Unsupported host or invalid archive URL:"
eecho " $INPUT"
exit 1
fi
vecho "Detected host: $HOST"
vecho "Repository (owner/name): $OWNER_REPO"
vecho "Git URL: $GIT_URL"
PACKAGE_NAME="${OWNER_REPO##*/}"
vecho "Package name: $PACKAGE_NAME"
# =============================================
# List branches/tags if requested
# =============================================
if $LIST_BRANCHES; then
iecho "Branches for $OWNER_REPO:"
git ls-remote --heads "$GIT_URL" | awk '{print $2}' | sed 's#refs/heads/##'
exit 0
fi
if $LIST_TAGS; then
iecho "Tags for $OWNER_REPO:"
git ls-remote --tags "$GIT_URL" \
| awk '{print $2}' \
| sed 's#refs/tags/##' \
| sed 's/\^{}//' \
| sort -u
exit 0
fi
# =============================================
# Load archive info from DB
# =============================================
init_db
entry=$(get_db_entry "$GIT_URL")
get_entry_field() { echo "$entry" | jq -r "$1 // empty"; }
load_from_db() {
ARCHIVE_URL=$(get_entry_field '.archive_url')
ARCHIVE_FILE=$(get_entry_field '.archive')
ARCHIVE_NAME=$(basename "$ARCHIVE_FILE")
DESCRIPTION=$(get_entry_field '.description')
TAG=$(get_entry_field '.latest_tag')
BRANCH=$(get_entry_field '.default_branch')
COMMIT=$(get_entry_field '.ref_name')
CHECKSUM=$(get_entry_field '.sha256')
BUILD_SYSTEM=$(get_entry_field '.build_system')
LANGUAGE=$(get_entry_field '.language')
HOMEPAGE=$(get_entry_field '.homepage')
}
# =============================================
# Determine type of requested ref
# =============================================
get_ref_type() {
local ref="$1"
if [[ -z "$ref" ]]; then
echo ""
elif [[ "$ref" =~ ^[0-9a-f]{7,40}$ ]]; then
echo "commit"
elif [[ "$ref" =~ ^v?[0-9]+([._-][0-9]+)*$ ]]; then
echo "tag"
else
echo "branch"
fi
}
# =============================================
# Decide whether we need to redownload
# =============================================
should_redownload() {
# Force download always overrides cache
[[ "$FORCE_DOWNLOAD" == true ]] && return 0
# If no DB entry or archive file missing → redownload
[[ -z "$entry" || -z "$ARCHIVE_FILE_DB" || ! -f "$ARCHIVE_FILE_DB" ]] && return 0
# If file exists but is not a valid tar → redownload
if ! tar xOf "$ARCHIVE_FILE_DB" &> /dev/null; then
wecho "Warning: invalid tar file detected, redownloading..."
return 0
fi
# If DB has no ref metadata → cannot trust cache for versioned requests
if [[ -z "$TAG" && -z "$BRANCH" && -z "$COMMIT" ]]; then
return 0
fi
# If no specific ref requested → DB copy is fine
if [[ -z "$REQUESTED_TAG" && -z "$REQUESTED_BRANCH" && -z "$REQUESTED_COMMIT" ]]; then
return 1
fi
# If TAG requested
if [[ -n "$REQUESTED_TAG" ]]; then
[[ -n "$TAG" && "$REQUESTED_TAG" == "$TAG" ]] && return 1
return 0
fi
# If BRANCH requested
if [[ -n "$REQUESTED_BRANCH" ]]; then
[[ -n "$BRANCH" && "$REQUESTED_BRANCH" == "$BRANCH" ]] && return 1
return 0
fi
# If COMMIT requested
if [[ -n "$REQUESTED_COMMIT" ]]; then
[[ -n "$COMMIT" && "$REQUESTED_COMMIT" == "$COMMIT" ]] && return 1
return 0
fi
# Default: anything else → redownload
return 0
}
# =============================================
# Download archive if needed
# =============================================
download_archive_if_needed() {
ARCHIVE_VERSION="${TAG:-${BRANCH:-${COMMIT}}}"
ARCHIVE_NAME=$(get_archive_name "$OWNER_REPO" "$ARCHIVE_VERSION" "$HOST")
ARCHIVE_FILE="$ROOT_DIR/downloads/$ARCHIVE_NAME"
mkdir -p "$ROOT_DIR/downloads/"
download_archive "$ARCHIVE_URL" "$ARCHIVE_FILE"
CHECKSUM=$(compute_sha256 "$ARCHIVE_FILE")
JSON_OUTPUT=$(bash "$SCRIPT_DIR/detect-build.sh" "$ARCHIVE_FILE")
HOMEPAGE=$(get_repo_homepage "$OWNER_REPO")
BUILD_SYSTEM=$(echo "$JSON_OUTPUT" | jq -r '.build_system')
LANGUAGE=$(echo "$JSON_OUTPUT" | jq -r '.language')
if [[ -z "$DESCRIPTION" ]]; then
decho "No description available."
DESCRIPTION=$(get_description_from_tar "$ARCHIVE_FILE")
fi
if [[ -n "$TAG" ]]; then
update_db "$GIT_URL" "$TAG" "" "" "$ARCHIVE_URL" "$ARCHIVE_FILE" "$CHECKSUM" "$PACKAGE_NAME" "$DESCRIPTION" "$BUILD_SYSTEM" "$LANGUAGE" "$HOMEPAGE"
elif [[ -n "$BRANCH" ]]; then
update_db "$GIT_URL" "" "$BRANCH" "" "$ARCHIVE_URL" "$ARCHIVE_FILE" "$CHECKSUM" "$PACKAGE_NAME" "$DESCRIPTION" "$BUILD_SYSTEM" "$LANGUAGE" "$HOMEPAGE"
else
update_db "$GIT_URL" "" "" "$COMMIT" "$ARCHIVE_URL" "$ARCHIVE_FILE" "$CHECKSUM" "$PACKAGE_NAME" "$DESCRIPTION" "$BUILD_SYSTEM" "$LANGUAGE" "$HOMEPAGE"
fi
}
# =============================================
# Load DB and check if we need download
# =============================================
TAG=
BRANCH=
COMMIT=
VERSION=
ARCHIVE_VERSION=
ARCHIVE_FILE_DB=$(get_entry_field '.archive')
load_from_db
if should_redownload; then
if [[ -n "$REQUESTED_TAG" ]]; then
resolve_specific_ref "$OWNER_REPO" "$REQUESTED_TAG" "tag"
elif [[ -n "$REQUESTED_BRANCH" ]]; then
resolve_specific_ref "$OWNER_REPO" "$REQUESTED_BRANCH" "branch"
elif [[ -n "$REQUESTED_COMMIT" ]]; then
resolve_specific_ref "$OWNER_REPO" "$REQUESTED_COMMIT" "commit"
else
resolve_archive "$OWNER_REPO"
fi
normalize_version
download_archive_if_needed
iecho "Downloaded archive: $ARCHIVE_FILE"
else
normalize_version
iecho "Download URL: $ARCHIVE_URL"
iecho "Using archive from DB: $ARCHIVE_FILE"
fi
# Detect top-level folder(s) inside the tar
top_level_dirs=$(tar -tf "$ARCHIVE_FILE" | cut -d/ -f1 | sort -u)
num_dirs=$(echo "$top_level_dirs" | wc -l)
if [ "$num_dirs" -eq 1 ]; then
ARCHIVE_SUBDIR="$top_level_dirs"
decho "Archive subdirectory detected: $ARCHIVE_SUBDIR"
else
eecho "Archive contains $num_dirs top-level entries: $top_level_dirs"
eecho "Expected exactly one top-level folder in the tarball. Aborting."
exit 1
fi
# Show tag, branch, or commit
if [[ -n "$TAG" ]]; then
iecho "Latest Tag: $(bold_bright_green "$TAG")"
elif [[ -n "$BRANCH" ]]; then
iecho "Default Branch: $(bold_bright_green "$BRANCH")"
elif [[ -n "$COMMIT" ]]; then
iecho "Commit/Custom Ref: $(bold_bright_green "$COMMIT")"
fi
# =============================================
# Display summary
# =============================================
iecho "Downloaded file: $(bold_bright_cyan "$ARCHIVE_NAME")"
iecho "Package name: $(bold_bright_green "$PACKAGE_NAME")"
[ -n "$DESCRIPTION" ] && iecho "Package description: $([[ "$DEBUG" = true ]] && echo "$DESCRIPTION" || echo "${DESCRIPTION:0:40}...")"
# If description is empty (custom ref, branch, tag, or commit), set placeholder
if [[ -z "$DESCRIPTION" ]]; then
DESCRIPTION="No description available"
fi
iecho "Homepage: ${HOMEPAGE:-No homepage URL found}"
iecho "Version: $(bold_bright_cyan "$VERSION")" # normalized version to X.Y.Z format
iecho "SHA256: $(bold_bright_cyan "$CHECKSUM")"
iecho "Build system: $(bold_bright_cyan "$BUILD_SYSTEM")"
iecho "Primary language: $(bold_bright_cyan "$LANGUAGE")"
# =============================================
# Optional: generate MXE .mk file
# =============================================
if [[ "$GENERATE_MXE_MAKEFILE" == true || "$GENERATE_MXE_TESTFILE" == true ]]; then
bash "$ROOT_DIR/contrib/mxe/scripts/generate_mxe_mk.sh" \
--mxe_args "$MXE_ARGS" \
--owner_repo "$OWNER_REPO" \
--archive "$ARCHIVE_FILE" \
--archive_subdir "$ARCHIVE_SUBDIR" \
--pkg "$PACKAGE_NAME" \
--tag "$TAG" \
--version "$VERSION" \
--archive_url "$ARCHIVE_URL" \
--checksum "$CHECKSUM" \
--description "$DESCRIPTION" \
--website "$GIT_URL" \
--homepage "$HOMEPAGE" \
--language "$LANGUAGE" \
$( [[ "$DEBUG" == true ]] && echo --debug )
fi
iecho "Done"