glab clone.sh

From wikieduonline
Jump to navigation Jump to search

#!/bin/bash
export GITLAB_HOST="gitlab.com"
GROUP="your-company/your/group"

echo "Fetching repository list from group $GROUP..."
REPOS=$(glab repo list --group "$GROUP" --include-subgroups --all --per-page 100 --output json \
  | jq -c '.[] | {path: .path_with_namespace, url: .ssh_url_to_repo}')

if [ -z "$REPOS" ]; then
    echo "No repositories found or failed to parse JSON. Ensure you are logged in via 'glab auth login'."
    exit 1
fi

FAILED=()
while IFS= read -r repo; do
    REPO_PATH=$(echo "$repo" | jq -r '.path')
    GIT_URL=$(echo "$repo" | jq -r '.url')
    if [ -d "$REPO_PATH/.git" ]; then
        echo "Updating: $REPO_PATH"
        if ! git -C "$REPO_PATH" fetch --all --prune; then
            FAILED+=("$REPO_PATH (fetch)")
        elif ! git -C "$REPO_PATH" pull --ff-only; then
            FAILED+=("$REPO_PATH (pull)")
        fi
    else
        echo "Cloning: $GIT_URL → $REPO_PATH"
        mkdir -p "$(dirname "$REPO_PATH")"
        if ! git clone "$GIT_URL" "$REPO_PATH"; then
            FAILED+=("$REPO_PATH (clone)")
        fi
    fi
    echo "----------------------------------------"
done < <(echo "$REPOS")

echo ""
if [ ${#FAILED[@]} -gt 0 ]; then
    echo "❌ The following repos failed:"
    printf '  - %s\n' "${FAILED[@]}"
else
    echo "✅ All projects processed successfully!"
fi

See also[edit]

git clone [ -vv | -depth | -c http.sslVerify=false | --mirror | --help ], git_clone.sh, git-auto-fetch

Advertising: