gpt4 book ai didi

arrays - powershell 切断 repo 名称

转载 作者:行者123 更新时间:2023-12-02 23:08:29 26 4
gpt4 key购买 nike

我在文件中有一个字符串:

git@github.com:myorg/Myrepo.git
git@github.com:myorg/Mysecondrepo.git
git@github.com:myorg/Myrepo-old.git

我只需要从这个字符串中删除 repo 名称(Myrepo、Mysecondrepo、Myrepo-old)。如何在 Powershell 中执行此操作?

我试过:
foreach($link in $gitlink)
{
$s = $link
$s.Substring(0, $s.IndexOf('/'))
echo $s
}

但它总是返回我 git@github.com:myorg。如何重写这段代码?

最佳答案

您可以使用带有(捕获组)的正则表达式来直接剥离所有内容并保留 repo 名称。

$Repos = (Get-Content '.\gitlinks.txt') -replace '^.*/(.*)\.git$','$1'
$Repos
Myrepo
Mysecondrepo
Myrepo-old

编辑:从 scrrenshot 合并到您的脚本中
## Q:\Test\2018\07\19\SO_51423853.ps1
Set-Location 'C:\a\'

ForEach($link in $gitlink){
git clone $link
$repodir = $link -replace '^.*/(.*)\.git$','$1'
$repodir
Push-Location $repodir
git --no-pager --oneline --before 2018-07-1 --after 2012-06-10 |
Out-File -FilePath c:\a\commits.txt
Pop-Location
}

关于arrays - powershell 切断 repo 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51422278/

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