Add repo get-latest subcommand: pull latest or clone if missing, with --recursive and --force support
This commit is contained in:
@@ -207,6 +207,99 @@ EOF
|
||||
echo "OK"
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# get-latest
|
||||
# ─────────────────────────────────────────────
|
||||
do_get_latest() {
|
||||
local REPO_NAME="" BRANCH="main" RECURSIVE=false FORCE=false
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--recursive) RECURSIVE=true; shift ;;
|
||||
--force) FORCE=true; shift ;;
|
||||
-*)
|
||||
echo "Unknown option: $1"
|
||||
echo "Usage: $0 get-latest <repo-name> [branch] [--recursive] [--force]"
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
[[ -z "$REPO_NAME" ]] && REPO_NAME="$1" && shift && continue
|
||||
[[ -z "$BRANCH" ]] && BRANCH="$1" && shift && continue
|
||||
echo "Unknown argument: $1"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "$REPO_NAME" ]]; then
|
||||
echo "Usage: $0 get-latest <repo-name> [branch] [--recursive] [--force]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "${AGENT_WORKSPACE:-}" ]]; then
|
||||
echo "Error: script must be executed by pcexec"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local REPO_DIR="${AGENT_WORKSPACE}/${REPO_NAME}"
|
||||
|
||||
if [[ -d "$REPO_DIR" ]]; then
|
||||
# Repo exists locally — update in place
|
||||
if ! git -C "$REPO_DIR" rev-parse --is-inside-work-tree 2>/dev/null; then
|
||||
echo "Error: $REPO_DIR is not a git repository"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local dirty
|
||||
dirty=$(git -C "$REPO_DIR" status --porcelain 2>/dev/null)
|
||||
if [[ -n "$dirty" ]] && [[ "$FORCE" != "true" ]]; then
|
||||
echo "Error: $REPO_DIR has uncommitted changes. Use --force to discard them."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -n "$dirty" ]]; then
|
||||
echo "Discarding uncommitted changes..."
|
||||
git -C "$REPO_DIR" checkout -- . 2>/dev/null || true
|
||||
fi
|
||||
|
||||
echo "Fetching: $REPO_NAME ($BRANCH)"
|
||||
git -C "$REPO_DIR" fetch origin "$BRANCH" 2>/dev/null || true
|
||||
git -C "$REPO_DIR" checkout "$BRANCH" 2>/dev/null || git -C "$REPO_DIR" checkout -b "$BRANCH" "origin/$BRANCH" 2>/dev/null || true
|
||||
|
||||
if [[ "$RECURSIVE" == "true" ]]; then
|
||||
echo "Updating submodules..."
|
||||
git -C "$REPO_DIR" submodule update --init --recursive 2>/dev/null || true
|
||||
fi
|
||||
|
||||
echo "Done: $REPO_DIR updated."
|
||||
else
|
||||
# Repo does not exist locally — clone it
|
||||
echo "Repo not found locally. Looking up URL..."
|
||||
local url=""
|
||||
url=$(bash "$SCRIPT_DIR/repo" list-all 2>/dev/null | grep "| $REPO_NAME |" | awk -F'|' '{print $3}' | tr -d ' ')
|
||||
|
||||
if [[ -z "$url" ]]; then
|
||||
echo "Error: repository '$REPO_NAME' not found in list-all output"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Cloning: $url"
|
||||
git clone "$url" "$REPO_DIR" 2>/dev/null || {
|
||||
echo "Error: git clone failed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
do_config --repo-path "$REPO_DIR"
|
||||
|
||||
if [[ "$RECURSIVE" == "true" ]]; then
|
||||
echo "Updating submodules..."
|
||||
git -C "$REPO_DIR" submodule update --init --recursive 2>/dev/null || true
|
||||
fi
|
||||
|
||||
echo "Done: $REPO_NAME cloned to $REPO_DIR."
|
||||
fi
|
||||
}
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# Dispatch
|
||||
# ─────────────────────────────────────────────
|
||||
@@ -218,6 +311,7 @@ if [[ $# -lt 1 ]]; then
|
||||
echo " add-collaborators --user <u> --repo <r> Add collaborator"
|
||||
echo " list-all List all visible repositories"
|
||||
echo " config --repo-path <path> [--recursive] Configure repo credentials"
|
||||
echo " get-latest <repo-name> [branch] [--recursive] [--force] Pull latest or clone if missing"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -228,5 +322,6 @@ case "$subcommand" in
|
||||
add-collaborators) do_add_collaborators "$@" ;;
|
||||
list-all) do_list_all "$@" ;;
|
||||
config) do_config "$@" ;;
|
||||
get-latest) do_get_latest "$@" ;;
|
||||
*) echo "Unknown command: $subcommand"; exit 1 ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user