diff --git a/.gitignore b/.gitignore index 4ebc714..f15703f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /frontend_dist/ *.tsbuildinfo .idea/ +/third_party/ diff --git a/CMakeLists.txt b/CMakeLists.txt index b1c47d3..19f41bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(adminive_package_includes "/frontend/" "/README.md" "/scripts/" + "/thirty_party/" ) set(adminive_package_excludes "/.git/" diff --git a/third_party/init.bash b/third_party/init.bash new file mode 100644 index 0000000..92c784a --- /dev/null +++ b/third_party/init.bash @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$root" +origin="$(git remote get-url origin)" +origin="${origin%/}" +remote_root="$(printf '%s\n' "$origin" | sed -E 's#/[^/]+(\.git)?$##')" +get_default_branch() { + local repo="$1" + local branch + branch="$(git ls-remote --symref "$repo" HEAD | awk '$1 == "ref:" { + sub("^refs/heads/", "", $2) + print $2 + exit + }')" + if [ -z "$branch" ]; then + printf '无法获取远程仓库默认分支: %s\n' "$repo" >&2 + return 1 + fi + printf '%s\n' "$branch" +} +fetch_repo() { + local repo="$1" + local dir="$2" + local branch="${3-}" + if [ -z "$branch" ]; then + branch="$(get_default_branch "$repo")" + fi + if [ -d "$dir" ]; then + git -C "$dir" rev-parse --is-inside-work-tree >/dev/null + git -C "$dir" remote set-url origin "$repo" + git -C "$dir" fetch origin "$branch" + git -C "$dir" checkout "$branch" + git -C "$dir" pull --ff-only origin "$branch" + else + git clone --branch "$branch" --single-branch "$repo" "$dir" + fi +} +fetch_repo_ex() { + local name="$1" + local branch="${2-}" + local repo="${remote_root}/${name}" + fetch_repo "$repo" "third_party/${name}" "$branch" +} +fetch_repo_ex "Structive"