从 TFS 克隆所有远程存储库
Cloning all remote repositories from TFS
有没有办法从 1 个帐户下上传的所有项目中克隆所有 master
分支。
我有一个要求,每周备份master
分支中的所有代码。
有没有办法使用 git、Powershell 或任何其他方式来完成此操作?
请注意 我需要在 Windows 环境中执行此任务。
我没有直接在 TFS 中看到该功能,但如果 VSTS API 也可用于您的本地 TFS 实例,您可以:
- 对指定帐户使用 API to get all projects, and then all repositories
- 使用该列表循环并拉取这些存储库的本地克隆中的每个主分支,然后您可以将其存档。
您可以使用 PowerShell 和 TFS Rest API。
首先,获取带有 Projects - List API, then get to each project the repositories with Repositories - List API 的项目并仅克隆 master。
例如,执行此操作的简单 PowerShell 脚本:
$collection = "http://tfs-server:8080/tfs/collection-name"
$projectsUrl = "$collection/_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
})
})
我创建了一个 github 要点来在 Azure DevOps 中实现这一点。你可以找到它here。在刷新 windows 或类似的东西后,我用它来设置我的新机器。
原始提交时的代码:
#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
})
有没有办法从 1 个帐户下上传的所有项目中克隆所有 master
分支。
我有一个要求,每周备份master
分支中的所有代码。
有没有办法使用 git、Powershell 或任何其他方式来完成此操作?
请注意 我需要在 Windows 环境中执行此任务。
我没有直接在 TFS 中看到该功能,但如果 VSTS API 也可用于您的本地 TFS 实例,您可以:
- 对指定帐户使用 API to get all projects, and then all repositories
- 使用该列表循环并拉取这些存储库的本地克隆中的每个主分支,然后您可以将其存档。
您可以使用 PowerShell 和 TFS Rest API。
首先,获取带有 Projects - List API, then get to each project the repositories with Repositories - List API 的项目并仅克隆 master。
例如,执行此操作的简单 PowerShell 脚本:
$collection = "http://tfs-server:8080/tfs/collection-name"
$projectsUrl = "$collection/_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
})
})
我创建了一个 github 要点来在 Azure DevOps 中实现这一点。你可以找到它here。在刷新 windows 或类似的东西后,我用它来设置我的新机器。
原始提交时的代码:
#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
})