Clone all my own project with powershell

hello,

i’m relatively new on gitlab and i created with an AI (i don’t know powershell language) a powershell script to clone all my projects in 1 click. all worked well but since git for windows update to 2.47.0 my script don’t work anymore and i can’t find how.

i’m currently on:

  • windows 11 pro x64 23h2
  • powershell 7.4.5
  • git for windows 2.47.0

i havn’t any problem if i clone using http like git clone URL_OF_MY_PROJECT.

here my script:

# Define your GitLab access token
$token = "glpat-XXXXXX" # Replace with your access token

# Define the GitLab API URL to retrieve your projects
$url = "https://gitlab.com/api/v4/projects?owned=true&per_page=100&page=1"

# Define the authorization header with the access token
$headers = @{"PRIVATE-TOKEN" = $token}

# Function to check if a directory is a valid Git repository
function Is-GitRepository($path) {
    return Test-Path -Path (Join-Path $path ".git/config")
}

# Retrieve the list of GitLab projects
$response = Invoke-RestMethod -Uri $url -Headers $headers

# Sort the projects by name
$sortedProjects = $response | Sort-Object -Property path

# Clone or update each project using SSH
foreach ($project in $sortedProjects) {
    $repoUrl = $project.ssh_url_to_repo
    $projectName = $project.path
    $currentPath = Join-Path (Get-Location) $projectName

    if (Test-Path -Path $currentPath) {
        if (Is-GitRepository($currentPath)) {
            Set-Location -Path $currentPath
            Write-Host "The directory $projectName already exists and is a Git repository. Updating the repository..."
            $pullResult = git pull
            if ($pullResult -like "*Already up to date.*") {
                $pullResult = $pullResult -replace "Already up to date.", "The repository is already up to date."
            }
            Write-Host $pullResult
            Set-Location -Path ..
        } else {
            Write-Host "The directory $projectName exists but is not a valid Git repository. Deleting the directory..."
            Remove-Item -Recurse -Force -Path $currentPath
            Write-Host "Cloning $repoUrl..."
            git clone $repoUrl
            Start-Sleep -Seconds 5  # Wait for 5 seconds to allow Git to finish its operations

            # Check after cloning
            if (Is-GitRepository($currentPath)) {
                Write-Host "Cloning $repoUrl succeeded and the directory is now a valid Git repository."
            } else {
                Write-Host "Error: The cloned directory $currentPath is still not a valid Git repository."
                Write-Host "Contents of the directory:"
                Get-ChildItem -Path $currentPath
            }
        }
    } else {
        Write-Host "Cloning $repoUrl..."
        git clone $repoUrl
        Start-Sleep -Seconds 5  # Wait for 5 seconds to allow Git to finish its operations

        # Check after cloning
        if (Is-GitRepository($currentPath)) {
            Write-Host "Cloning $repoUrl succeeded and the directory is now a valid Git repository."
        } else {
            Write-Host "Error: The cloned directory $currentPath is still not a valid Git repository."
            Write-Host "Contents of the directory:"
            Get-ChildItem -Path $currentPath
        }
    }
}

Pause

actually this script block here:

Cloning git@gitlab.com:breatfr/PROJECT_NAME.git...
Cloning into 'PROJECT_NAME'...
remote: Enumerating objects: 61, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (31/31), done.

i hope i’m in the good place to get help about this and sorry for my bad english :slight_smile:

Hi,

Unfortunately, this seems to be an issue with git or powershell, since you can clone the project manually from GitLab. Since this is GitLab only forum, topics outside of GitLab are not covered.

I’d suggest you posting your question on another more generic forum. Also, make sure to add the actual error you’re facing, not just the script.