首页 > 解决方案 > 克隆 git 存储库并将所有者包含在文件夹结构中

问题描述

我正在寻找一种克隆 git 存储库的方法,例如从 GitHub,并在下载的文件夹结构中包含所有者或组织。

例如,当从当前文件夹中的angular-cli组织克隆存储库时angular,我想让它像这样在我当前的工作目录中克隆它:angular/angular-cli.

我曾尝试在谷歌上搜索解决方案,但找不到,因为基本上所有结果都只是告诉我如何克隆存储库。当然我可以做到,但我想要一些工具来帮助我自动化这个过程。也许是 bash 或 powershell 脚本,甚至是直接内置于 git 中的东西。

编辑:与另一个问题相反,我正在寻找一种工具,该工具可以根据源(例如 Github)和用户/组织(例如 Angular)自动将存储库放置在正确的文件夹结构中。

标签: gitrepository

解决方案


更新: 由于我刚刚对这个旧问答获得了愉快的支持,所以我想更新我的帖子。我最近开始将此脚本放入 PowerShell 模块 (GitManagement) 中,可在此处找到:https ://github.com/totkeks/PowerShell-Modules


我自己在 powershell 中构建了一个解决方案。您可以通过名称和用于解析 URL 并为存储库构建相应路径的正则表达式配置不同的 Git 提供程序。

<#
    .DESCRIPTION
    Clone git repositories including the project/user/organization/... folder structure
#>
Param(
    [parameter(Mandatory = $true)]
    [String]
    $Url
)

#------------------------------------------------------------------------------
# Configuration of available providers
#------------------------------------------------------------------------------
$GitProviders = @{
    "Azure"  = {
        if ($args[0] -Match "https://(?:\w+@)?dev.azure.com/(?<Organization>\w+)/(?<Project>\w+)/_git/(?<Repository>[\w-_]+)") {
            return [io.path]::Combine($Matches.Organization, $Matches.Project, $Matches.Repository)
        }
    }

    "GitHub" = {
        if ($args[0] -Match "https://github\.com/(?<UserOrOrganization>\w+)/(?<Repository>[\w-_]+)\.git") {
            return [io.path]::Combine($Matches.UserOrOrganization, $Matches.Repository)
        }
    }
}


#------------------------------------------------------------------------------
# Find the right provider and clone the repository
#------------------------------------------------------------------------------
$Match = $GitProviders.GetEnumerator() |
    Select-Object @{n = "Provider"; e = {$_.Key}}, @{n = "Path"; e = {$_.Value.invoke($Url)}} |
    Where-Object { $_.Path -ne $null } |
    Select-Object -First 1

if ($Match) {
    Write-Host "Found match for provider: $($Match.Provider)"

    if ($Global:ProjectsDir) {
        $TargetDirectory = [io.path]::Combine($Global:ProjectsDir, $Match.Provider, $Match.Path)
    }
    else {
        Write-Error "No projects directory configured. Aborting."
    }

    git clone $Url $TargetDirectory
}
else {
    Write-Error "No match found for repository url: $Url"
}

推荐阅读