【问题标题】:Clone git repositories and include the owner in the folder structure克隆 git 存储库并将所有者包含在文件夹结构中
【发布时间】:2018-12-23 22:02:32
【问题描述】:

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

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

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

编辑:与其他问题相比,我正在寻找一种工具,可以根据源自动将存储库放置在正确的文件夹结构中,例如Github,以及用户/组织,例如角度。

【问题讨论】:

标签: git repository


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    gitclone(){ gitstrip="${1#*//}"; gitpath="${gitstrip#*/}"; git clone $1 ${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

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

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2018-07-17
      • 1970-01-01
      • 2012-10-24
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多