-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource-origin.func
More file actions
73 lines (62 loc) · 2.58 KB
/
Copy pathsource-origin.func
File metadata and controls
73 lines (62 loc) · 2.58 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
#!/usr/bin/env bash
# Copyright (c) 2021-2026 community-scripts ORG
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
# ==============================================================================
# SOURCE-ORIGIN.FUNC — Git remote → COMMUNITY_SCRIPTS_URL helpers
# ==============================================================================
# Loaded by core/build.func when running from a local checkout.
# CT scripts do NOT source this directly — they use a one-liner to build.func.
#
# Explicit COMMUNITY_SCRIPTS_URL / COMMUNITY_SCRIPTS_DIR overrides always win.
# ==============================================================================
[[ -n "${_CS_SOURCE_ORIGIN_LOADED:-}" ]] && return
_CS_SOURCE_ORIGIN_LOADED=1
cs_git_remote_to_raw_base() {
# arg: git remote URL, branch → stdout: raw content base (no trailing slash)
local remote="${1:-}" branch="${2:-main}"
local owner repo host
[[ -z "$remote" ]] && return 1
remote="${remote%.git}"
# git@github.com:owner/repo | https://github.com/owner/repo
if [[ "$remote" =~ git@github\.com:([^/]+)/([^/]+)$ ]] ||
[[ "$remote" =~ https?://github\.com/([^/]+)/([^/]+)$ ]] ||
[[ "$remote" =~ ssh://git@github\.com/([^/]+)/([^/]+)$ ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
echo "https://raw.githubusercontent.com/${owner}/${repo}/${branch}"
return 0
fi
# Gitea: https://git.community-scripts.org/owner/repo
# raw: https://git.community-scripts.org/owner/repo/raw/branch/<branch>
if [[ "$remote" =~ https?://([^/]+)/([^/]+)/([^/]+)$ ]]; then
host="${BASH_REMATCH[1]}"
owner="${BASH_REMATCH[2]}"
repo="${BASH_REMATCH[3]}"
case "$host" in
git.community-scripts.org | gitea.* | *.gitea.*)
echo "https://${host}/${owner}/${repo}/raw/branch/${branch}"
return 0
;;
esac
fi
return 1
}
cs_detect_origin_from_git() {
local root="${1:-}"
local remote branch ref url
[[ -z "$root" || ! -d "$root" ]] && return 1
command -v git >/dev/null 2>&1 || return 1
[[ -d "$root/.git" || -f "$root/.git" ]] || return 1
remote="$(git -C "$root" remote get-url origin 2>/dev/null || true)"
[[ -z "$remote" ]] && return 1
branch="$(git -C "$root" rev-parse --abbrev-ref HEAD 2>/dev/null || echo HEAD)"
if [[ "$branch" == "HEAD" ]]; then
ref="$(git -C "$root" rev-parse --short HEAD 2>/dev/null || true)"
[[ -n "$ref" ]] && branch="$ref"
fi
[[ -z "$branch" ]] && branch="main"
url="$(cs_git_remote_to_raw_base "$remote" "$branch" 2>/dev/null || true)"
[[ -z "$url" ]] && return 1
echo "$url"
return 0
}