git 从TFS克隆所有远程存储库

s1ag04yj  于 2022-12-10  发布在  Git
关注(0)|答案(3)|浏览(269)

有没有办法克隆所有的master分支从所有的项目上传到一个帐户。
我要求每周备份master分支中的所有代码。有没有办法用git,Powershell或者其他方法来完成这个任务?

请注意我需要在Windows环境中执行此任务。

yruzcnhs

yruzcnhs1#

您可以使用PowerShell和TFS Rest API来实现这一点。
首先,用Projects - List API获取项目,然后用Repositories - List API获取每个项目的存储库,并且只克隆主存储库。
例如,执行此操作的简单PowerShell脚本:

$collectionUrl = "http://tfs-server:8080/tfs/collection-name"

$projectsUrl = "$collectionUrl/_apis/projects?api-version=4.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -UseDefaultCredentials -ContentType application/json

$projects.value.ForEach({

    $reposUrl = "$collectionUrl/$($_.name)/_apis/git/repositories?api-version=4.0"
    $repos = Invoke-RestMethod -Uri $reposUrl -Method Get -UseDefaultCredentials -ContentType application/json
    $repos.value.ForEach({
    git clone $_.remoteUrl --branch master --single-branch

    })
})
pgx2nnw8

pgx2nnw82#

我没有在TFS中直接看到该功能,但是如果VSTS API也可用于内部部署TFS示例,则可以:

jdgnovmf

jdgnovmf3#

我已经创建了一个github gist来在Azure DevOps中实现这一点。你可以找到它here。我用它来设置我的新机器后刷新窗口或类似的东西。
原始提交时的代码:

#Ensure you create a PAT, following the instructions here: https://dev.to/omiossec/getting-started-with-azure-devops-api-with-powershell-59nn
#Additional Credit: https://blog.rsuter.com/script-to-clone-all-git-repositories-from-your-vsts-collection/
#I suggest executing from C:/Projects. This script will create a folder for each Team Project/Client with repos within each.
#Finally note that git clone operations count as "Errors" in powershell, and appear red. It's more work than is worth it to change it.

param(
    [string] $email = $(Throw "--Email is required."), #required parameter
    [string] $pat = $(Throw "--PAT is Required"), #required parameter
    [string] $url = $(Throw "--Collection URL is required.") #required parameter pointing to the default collection, URL is generally https://{tenant}.visualstudio.com/defaultcollection
)
$originalDirectory = Get-Location

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $email,$pat)))
$headers = @{
    "Authorization" = ("Basic {0}" -f $base64AuthInfo)
    "Accept" = "application/json"
}

Add-Type -AssemblyName System.Web
$gitcred = ("{0}:{1}" -f  [System.Web.HttpUtility]::UrlEncode($username),$password)

#Write-Host "Retrieving Repositories...`n"
#$resp = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/git/repositories?api-version=1.0" -f $url)
#$repoJson = convertFrom-JSON $resp.Content
#Write-Host $repoJson

Write-Host "Getting Projects..."
$projectsUrl = "$collection/_apis/projects?api-version=4.0"
$projectResponse = Invoke-Webrequest -Headers $headers -Uri ("{0}/_apis/projects?api-version=4.0" -f $url)
$projects = ConvertFrom-Json $projectResponse

$projects.value.ForEach({
    $folderToCreate = Join-Path -Path $originalDirectory -ChildPath $_.name

    if (!(Test-Path $folderToCreate -PathType Container)) {
        Write-Host "Creating folder for Project $($_.name)"
        New-Item -ItemType Directory -Force -Path $folderToCreate
    } else {
        Write-Host "Skipping folder creation for project $($_.name), as it already exists"
    }
    Set-Location $folderToCreate

    $reposUrl = "$url/$($_.name)/_apis/git/repositories?api-version=4.0"
    $reposResponse = Invoke-Webrequest -Headers $headers -Uri $reposUrl
    $repos = ConvertFrom-Json $reposResponse    

    $repos.value.ForEach({
        $name = $_.name
        Write-Host "Cloning $name Repos"

        try {            
            $credUrl = $_.remoteUrl -replace "://", ("://{0}@" -f $gitcred)
            git clone $credUrl --branch master --single-branch    
            #git clone $_.remoteUrl --branch master --single-branch #this will automatically create/use GitForWindows token after a login prompt if you have issues with the upper 2 lines
        }
        catch {
            Write-Host $PSItem.Exception.Message -ForegroundColor RED
            Write-Host "Error at URL $_.remoteUrl"
            Set-Location $originalDirectory
        }        
    })
    Write-Host "Cleaning URL Space encoding for repo folders..."
    Get-ChildItem $folderToCreate | 
        Where {$_.Name -Match '%20'} | 
            Rename-Item -NewName {$_.name -replace '%20',' ' } #Rename-Item  { $_.Name -replace "%20"," " }
    Set-Location $originalDirectory
})

相关问题