克隆 git 个存储库并将所有者包含在文件夹结构中

Clone git repositories and include the owner in the folder structure

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

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

我尝试使用谷歌搜索解决方案,但找不到,因为基本上所有结果都只是告诉我如何克隆存储库。我当然可以这样做,但我希望有一些工具可以帮助我自动执行此过程。可能是 bash 或 powershell 脚本,甚至是直接内置到 git 中的东西。

编辑:与另一个问题相反,我正在寻找一种工具,它可以根据来源自动将存储库放置在正确的文件夹结构中,例如Github,以及 user/organization 例如Angular.

更新: 由于我刚刚对这个旧的问答获得了愉快的支持,所以我想更新我的帖子。我最近开始将此脚本放入 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"
}

我刚开始使用 git 并且想知道同样的事情。我想出了这个 bash:

gitclone(){ gitstrip="${1#*//}"; gitpath="${gitstrip#*/}"; git clone  ${gitpath%.git};}

我想让它与子模块一起工作。这些是我的测试用例:

https://gitlab.com/sane-project/frontend/xsane.git
https://gitlab.com/sane-project/frontend/xsane
https://gitlab.com/sane-project/backends.git
https://gitlab.com/sane-project/backends

这似乎有效,但我还没有测试过这些。