feat: support skills in repository subfolders
This commit is contained in:
1 parent
73c717fd63
commit
8b37a12d0a
2 files changed
+205
-33
No files matched your search
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
# One skill per line: <git url>[=|>|>=<tag or commit>]
|
# One entry per line: <git url>[{/folder,/folder}][=|>|>=<tag, branch or commit>]
|
||||||
# https://git.bjphoster.com/skills/settle
|
# https://git.bjphoster.com/skills/settle
|
||||||
# https://git.bjphoster.com/skills/ship=0.1.0
|
# https://git.bjphoster.com/skills/ship=0.1.0
|
||||||
# https://git.bjphoster.com/skills/ship>=0.1.0
|
# https://git.bjphoster.com/skills/ship>=0.1.0
|
||||||
# https://git.bjphoster.com/skills/settle=be3f9d3646
|
# https://git.bjphoster.com/skills/settle=be3f9d3646
|
||||||
|
# https://github.com/t3dotgg/tstack{/skills/council,/skills/sol-loop}
|
||||||
+203
-32
@@ -1,10 +1,13 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -uo pipefail
|
set -uo pipefail
|
||||||
|
|
||||||
VERSION="0.1.0"
|
VERSION="0.2.0"
|
||||||
SKILLS_FILE="${SKILLS_FILE:-$HOME/.config/skills}"
|
SKILLS_FILE="${SKILLS_FILE:-$HOME/.config/skills}"
|
||||||
HARNESSES_FILE="${HARNESSES_FILE:-$HOME/.config/harnesses}"
|
HARNESSES_FILE="${HARNESSES_FILE:-$HOME/.config/harnesses}"
|
||||||
LOCK_FILE="${SKILLS_AUTOUPDATE_LOCK:-${XDG_CACHE_HOME:-$HOME/.cache}/skills-autoupdate.lock}"
|
LOCK_FILE="${SKILLS_AUTOUPDATE_LOCK:-${XDG_CACHE_HOME:-$HOME/.cache}/skills-autoupdate.lock}"
|
||||||
|
CACHE_DIR="${SKILLS_AUTOUPDATE_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/skills-autoupdate}"
|
||||||
|
MARKER=".skills-autoupdate"
|
||||||
|
LINE_RE='^([^{}=>[:space:]]+)(\{([^{}[:space:]]*)\})?(>=|>|=)?([^[:space:]]*)$'
|
||||||
|
|
||||||
export GIT_TERMINAL_PROMPT=0
|
export GIT_TERMINAL_PROMPT=0
|
||||||
export GIT_ASKPASS=/bin/true
|
export GIT_ASKPASS=/bin/true
|
||||||
@@ -22,6 +25,9 @@ in the skills folder of every harness listed in $HARNESSES_FILE.
|
|||||||
Environment:
|
Environment:
|
||||||
SKILLS_FILE list of skills (default ~/.config/skills)
|
SKILLS_FILE list of skills (default ~/.config/skills)
|
||||||
HARNESSES_FILE list of harnesses (default ~/.config/harnesses)
|
HARNESSES_FILE list of harnesses (default ~/.config/harnesses)
|
||||||
|
SKILLS_AUTOUPDATE_CACHE
|
||||||
|
mirrors of repositories with skills in subfolders
|
||||||
|
(default ~/.cache/skills-autoupdate)
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +141,130 @@ best_tag() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe() {
|
describe() {
|
||||||
git -C "$1" describe --tags --always --exact-match HEAD 2>/dev/null || git -C "$1" rev-parse --short HEAD
|
git -C "$1" describe --tags --exact-match "${2:-HEAD}" 2>/dev/null || git -C "$1" rev-parse --short "${2:-HEAD}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve_ref() {
|
||||||
|
local dir="$1" op="$2" ref="$3" r
|
||||||
|
case "$op" in
|
||||||
|
"") git -C "$dir" rev-parse -q --verify "HEAD^{commit}" ;;
|
||||||
|
"=")
|
||||||
|
for r in "refs/tags/$ref" "refs/tags/v${ref#v}" "refs/remotes/origin/$ref" "refs/heads/$ref" "$ref"; do
|
||||||
|
git -C "$dir" rev-parse -q --verify "$r^{commit}" 2>/dev/null && return 0
|
||||||
|
done
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
r="$(best_tag "$dir" "$op" "$ref")" || return 1
|
||||||
|
git -C "$dir" rev-parse -q --verify "refs/tags/$r^{commit}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
sha() {
|
||||||
|
if command -v sha256sum >/dev/null; then
|
||||||
|
sha256sum
|
||||||
|
else
|
||||||
|
shasum -a 256
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
hash_tree() {
|
||||||
|
(
|
||||||
|
cd "$1" || exit 1
|
||||||
|
find . \( -type f -o -type l \) ! -path "./$MARKER" | LC_ALL=C sort | while IFS= read -r f; do
|
||||||
|
if [ -L "$f" ]; then
|
||||||
|
printf '%s -> %s\n' "$f" "$(readlink "$f")"
|
||||||
|
else
|
||||||
|
printf '%s %s\n' "$f" "$(sha <"$f" | cut -d' ' -f1)"
|
||||||
|
fi
|
||||||
|
done | sha | cut -d' ' -f1
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
marker_get() {
|
||||||
|
sed -n "s/^$2=//p" "$1/$MARKER" | head -n 1
|
||||||
|
}
|
||||||
|
|
||||||
|
cache_path() {
|
||||||
|
printf '%s/%s.git\n' "$CACHE_DIR" "$(printf '%s' "${1%.git}" | sed -E 's|^[A-Za-z][A-Za-z0-9+.-]*://||; s|[^A-Za-z0-9._-]|_|g')"
|
||||||
|
}
|
||||||
|
|
||||||
|
update_cache() {
|
||||||
|
local url="$1" dir="$2"
|
||||||
|
if [ -d "$dir" ]; then
|
||||||
|
git -C "$dir" fetch -q --prune --force origin 2>&1
|
||||||
|
else
|
||||||
|
mkdir -p "$CACHE_DIR" || return 1
|
||||||
|
if ! git clone -q --mirror "$url" "$dir" 2>&1; then
|
||||||
|
rm -rf "$dir"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
sync_subskill() {
|
||||||
|
local harness="$1" dir="$2" url="$3" path="$4" op="$5" ref="$6" cache="$7"
|
||||||
|
local tag before="" target tmp
|
||||||
|
tag="[$harness] ${dir##*/}"
|
||||||
|
|
||||||
|
if [ -z "$cache" ]; then
|
||||||
|
fail "$tag: $url is not available"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [ -e "$dir" ]; then
|
||||||
|
if [ ! -f "$dir/$MARKER" ]; then
|
||||||
|
warn "$tag: $dir exists and was not installed from a subfolder, skipped"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [ "$(marker_get "$dir" url)" != "$url" ] || [ "$(marker_get "$dir" path)" != "$path" ]; then
|
||||||
|
warn "$tag: installed from $(marker_get "$dir" url) /$(marker_get "$dir" path), expected $url /$path, skipped"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [ "$(hash_tree "$dir")" != "$(marker_get "$dir" hash)" ]; then
|
||||||
|
warn "$tag: local changes present, skipped"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
before="$(marker_get "$dir" commit)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! target="$(resolve_ref "$cache" "$op" "$ref")"; then
|
||||||
|
if [ "$op" = "=" ]; then
|
||||||
|
fail "$tag: ref '$ref' not found"
|
||||||
|
elif [ -n "$before" ]; then
|
||||||
|
warn "$tag: no tag $op $ref, left at $(describe "$cache" "$before")"
|
||||||
|
else
|
||||||
|
warn "$tag: no tag $op $ref, not installed"
|
||||||
|
fi
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [ "$target" = "$before" ]; then
|
||||||
|
log "$tag: up to date at $(describe "$cache" "$target")"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [ "$(git -C "$cache" cat-file -t "$target:$path" 2>/dev/null)" != "tree" ]; then
|
||||||
|
fail "$tag: folder /$path not found at $(describe "$cache" "$target")"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
tmp="$(mktemp -d "${dir%/*}/.${dir##*/}.XXXXXX")" || { fail "$tag: cannot create a temporary folder"; return; }
|
||||||
|
chmod 755 "$tmp"
|
||||||
|
if ! git -C "$cache" archive --format=tar "$target:$path" | tar -x -C "$tmp"; then
|
||||||
|
rm -rf "$tmp"
|
||||||
|
fail "$tag: extraction of /$path failed"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
printf 'url=%s\npath=%s\ncommit=%s\nhash=%s\n' "$url" "$path" "$target" "$(hash_tree "$tmp")" >"$tmp/$MARKER"
|
||||||
|
if ! rm -rf "$dir" || ! mv "$tmp" "$dir"; then
|
||||||
|
rm -rf "$tmp"
|
||||||
|
fail "$tag: cannot replace $dir"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
if [ -z "$before" ]; then
|
||||||
|
log "$tag: installed /$path from $url at $(describe "$cache" "$target")"
|
||||||
|
else
|
||||||
|
log "$tag: updated $(describe "$cache" "$before") -> $(describe "$cache" "$target")"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
sync_skill() {
|
sync_skill() {
|
||||||
@@ -186,12 +315,13 @@ sync_skill() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
"=")
|
*)
|
||||||
target="$(git -C "$dir" rev-parse -q --verify "refs/tags/$ref^{commit}" 2>/dev/null ||
|
if ! target="$(resolve_ref "$dir" "$op" "$ref")"; then
|
||||||
git -C "$dir" rev-parse -q --verify "refs/tags/v${ref#v}^{commit}" 2>/dev/null ||
|
if [ "$op" = "=" ]; then
|
||||||
git -C "$dir" rev-parse -q --verify "$ref^{commit}" 2>/dev/null)"
|
fail "$tag: ref '$ref' not found"
|
||||||
if [ -z "$target" ]; then
|
else
|
||||||
fail "$tag: ref '$ref' not found"
|
warn "$tag: no tag $op $ref, left at $(describe "$dir")"
|
||||||
|
fi
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
if [ "$target" != "$(git -C "$dir" rev-parse HEAD)" ] && ! git -C "$dir" checkout -q --detach "$target" 2>&1; then
|
if [ "$target" != "$(git -C "$dir" rev-parse HEAD)" ] && ! git -C "$dir" checkout -q --detach "$target" 2>&1; then
|
||||||
@@ -199,18 +329,6 @@ sync_skill() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
">" | ">=")
|
|
||||||
target="$(best_tag "$dir" "$op" "$ref")"
|
|
||||||
if [ -z "$target" ]; then
|
|
||||||
warn "$tag: no tag $op $ref, left at $(describe "$dir")"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
if [ "$(git -C "$dir" rev-parse "refs/tags/$target^{commit}")" != "$(git -C "$dir" rev-parse HEAD)" ] &&
|
|
||||||
! git -C "$dir" checkout -q --detach "refs/tags/$target" 2>&1; then
|
|
||||||
fail "$tag: checkout of $target failed"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
after="$(git -C "$dir" rev-parse HEAD)"
|
after="$(git -C "$dir" rev-parse HEAD)"
|
||||||
@@ -241,8 +359,8 @@ main() {
|
|||||||
flock -n 9 || { log "another run is in progress, exiting"; exit 0; }
|
flock -n 9 || { log "another run is in progress, exiting"; exit 0; }
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local -a h_names=() h_roots=() s_urls=() s_ops=() s_refs=() s_names=()
|
local -a h_names=() h_roots=() s_urls=() s_paths=() s_ops=() s_refs=() s_names=() paths=() names=() parts=()
|
||||||
local raw line name root real seen="" i j
|
local raw line name root real seen="" seen_names="" fetched="" url op ref p i j k cache
|
||||||
|
|
||||||
while IFS= read -r raw || [ -n "$raw" ]; do
|
while IFS= read -r raw || [ -n "$raw" ]; do
|
||||||
line="$(strip_comment "$raw")"
|
line="$(strip_comment "$raw")"
|
||||||
@@ -276,26 +394,75 @@ main() {
|
|||||||
while IFS= read -r raw || [ -n "$raw" ]; do
|
while IFS= read -r raw || [ -n "$raw" ]; do
|
||||||
line="$(strip_comment "$raw")"
|
line="$(strip_comment "$raw")"
|
||||||
[ -z "$line" ] && continue
|
[ -z "$line" ] && continue
|
||||||
if [[ ! "$line" =~ ^([^=\>[:space:]]+)(\>=|\>|=)?([^[:space:]]*)$ ]]; then
|
if [[ ! "$line" =~ $LINE_RE ]]; then
|
||||||
warn "invalid skill line '$line', skipped"
|
warn "invalid skill line '$line', skipped"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
if [ -n "${BASH_REMATCH[2]}" ] && [ -z "${BASH_REMATCH[3]}" ]; then
|
if [ -n "${BASH_REMATCH[4]}" ] && [ -z "${BASH_REMATCH[5]}" ]; then
|
||||||
warn "missing version in '$line', skipped"
|
warn "missing version in '$line', skipped"
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
s_urls+=("${BASH_REMATCH[1]}")
|
url="${BASH_REMATCH[1]%/}"
|
||||||
s_ops+=("${BASH_REMATCH[2]}")
|
op="${BASH_REMATCH[4]}"
|
||||||
s_refs+=("${BASH_REMATCH[3]}")
|
ref="${BASH_REMATCH[5]}"
|
||||||
name="${BASH_REMATCH[1]%/}"
|
if [ -z "${BASH_REMATCH[2]}" ]; then
|
||||||
name="${name##*/}"
|
name="${url##*/}"
|
||||||
name="${name##*:}"
|
name="${name##*:}"
|
||||||
s_names+=("${name%.git}")
|
paths=("")
|
||||||
|
names=("${name%.git}")
|
||||||
|
else
|
||||||
|
paths=()
|
||||||
|
names=()
|
||||||
|
IFS=, read -r -a parts <<<"${BASH_REMATCH[3]}"
|
||||||
|
for p in "${parts[@]}"; do
|
||||||
|
p="$(printf '%s' "$p" | sed -E 's|/+|/|g; s|^/||; s|/$||')"
|
||||||
|
if [ -z "$p" ] || [[ "/$p/" == */../* || "/$p/" == */./* ]]; then
|
||||||
|
warn "invalid subfolder in '$line', skipped"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
paths+=("$p")
|
||||||
|
names+=("${p##*/}")
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
for k in "${!paths[@]}"; do
|
||||||
|
case "$seen_names" in
|
||||||
|
*"|${names[$k]}|"*)
|
||||||
|
warn "skill name '${names[$k]}' is listed more than once, '$line' skipped"
|
||||||
|
continue
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
seen_names="$seen_names|${names[$k]}|"
|
||||||
|
s_urls+=("$url")
|
||||||
|
s_paths+=("${paths[$k]}")
|
||||||
|
s_ops+=("$op")
|
||||||
|
s_refs+=("$ref")
|
||||||
|
s_names+=("${names[$k]}")
|
||||||
|
done
|
||||||
done <"$SKILLS_FILE"
|
done <"$SKILLS_FILE"
|
||||||
|
|
||||||
[ "${#h_names[@]}" -eq 0 ] && { log "no harness found, nothing to do"; exit 0; }
|
[ "${#h_names[@]}" -eq 0 ] && { log "no harness found, nothing to do"; exit 0; }
|
||||||
[ "${#s_urls[@]}" -eq 0 ] && { log "no skill listed, nothing to do"; exit 0; }
|
[ "${#s_urls[@]}" -eq 0 ] && { log "no skill listed, nothing to do"; exit 0; }
|
||||||
|
|
||||||
|
local -a s_caches=()
|
||||||
|
for j in "${!s_urls[@]}"; do
|
||||||
|
s_caches[j]=""
|
||||||
|
[ -z "${s_paths[$j]}" ] && continue
|
||||||
|
cache="$(cache_path "${s_urls[$j]}")"
|
||||||
|
case "$fetched" in
|
||||||
|
*"|$cache|"*) ;;
|
||||||
|
*"|!$cache|"*) continue ;;
|
||||||
|
*)
|
||||||
|
if ! update_cache "${s_urls[$j]}" "$cache"; then
|
||||||
|
fail "cannot fetch ${s_urls[$j]}"
|
||||||
|
fetched="$fetched|!$cache|"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fetched="$fetched|$cache|"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
s_caches[j]="$cache"
|
||||||
|
done
|
||||||
|
|
||||||
for i in "${!h_names[@]}"; do
|
for i in "${!h_names[@]}"; do
|
||||||
root="$(skills_dir "${h_roots[$i]}")"
|
root="$(skills_dir "${h_roots[$i]}")"
|
||||||
if ! mkdir -p "$root"; then
|
if ! mkdir -p "$root"; then
|
||||||
@@ -303,7 +470,11 @@ main() {
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
for j in "${!s_urls[@]}"; do
|
for j in "${!s_urls[@]}"; do
|
||||||
sync_skill "${h_names[$i]}" "$root/${s_names[$j]}" "${s_urls[$j]}" "${s_ops[$j]}" "${s_refs[$j]}"
|
if [ -z "${s_paths[$j]}" ]; then
|
||||||
|
sync_skill "${h_names[$i]}" "$root/${s_names[$j]}" "${s_urls[$j]}" "${s_ops[$j]}" "${s_refs[$j]}"
|
||||||
|
else
|
||||||
|
sync_subskill "${h_names[$i]}" "$root/${s_names[$j]}" "${s_urls[$j]}" "${s_paths[$j]}" "${s_ops[$j]}" "${s_refs[$j]}" "${s_caches[$j]}"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user