gpt4 book ai didi

c# - PowerShell 脚本上的 Git 命令 - 授权

转载 作者:行者123 更新时间:2023-12-02 23:46:39 34 4
gpt4 key购买 nike

我想知道在这种情况下最好做什么。我想编写一个脚本来创建本地存储库,然后可以从 VSTS 获取并推送到 TFS 以创建备份。

使用 GIT Bash,我能够按照以下说明完成这项工作。

git clone --mirror https://github.com/exampleuser/repository-to-mirror.git
cd repository-to-mirror.git
git remote set-url --push origin https://github.com/exampleuser/mirrored
git fetch -p origin
git push --mirror

我想在 Windows 10 上使用 ISE 创建一个 Powershell 脚本来执行此操作。我安装了 git-posh 以便能够编写命令。

但是当我按照 GIT 的指示编写命令时( https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ )

    git clone https://github.com/username/repo.git
Username: your_username
Password: your_token

我收到错误:

远程:TF401019:名称或标识符为 Logic-Test1.git 的 Git 存储库不存在或您没有您正在尝试的操作的权限。

用户名::术语“用户名:”不被识别为 cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

如何进行身份验证以便我可以克隆它?另外,有没有办法不必为每个命令键入身份验证?

最佳答案

此博客Script to clone all Git repositories from your VSTS collection提供了如何使用 powershell 从 VSTS 克隆 git 存储库的示例,您可以检查它并按照它执行其他命令。

首先,创建一个PowerShell脚本文件:

# Read configuration file
Get-Content "CloneAllRepos.config" | foreach-object -begin {$h=@{}} -process {
$k = [regex]::split($_,'=');
if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) {
$h.Add($k[0], $k[1])
}
}
$url = $h.Get_Item("Url")
$username = $h.Get_Item("Username")
$password = $h.Get_Item("Password")

# Retrieve list of all repositories
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{
"Authorization" = ("Basic {0}" -f $base64AuthInfo)
"Accept" = "application/json"
}

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

$resp = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/git/repositories?api-version=1.0" -f $url)
$json = convertFrom-JSON $resp.Content

# Clone or pull all repositories
$initpath = get-location
foreach ($entry in $json.value) {
$name = $entry.name
Write-Host $name

$url = $entry.remoteUrl -replace "://", ("://{0}@" -f $gitcred)
if(!(Test-Path -Path $name)) {
git clone $url
} else {
set-location $name
git pull
set-location $initpath
}
}

然后使用您的配置与脚本一起创建一个配置文件:

[General]
Url=https://myproject.visualstudio.com/defaultcollection
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1b49284938f808c84dc94928493a1858e8c80888fcf828e8c" rel="noreferrer noopener nofollow">[email protected]</a>
Password=YourAccessToken

关于c# - PowerShell 脚本上的 Git 命令 - 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48269071/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com