Public/Index/New-GitRepositoryIndex.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<#
.SYNOPSIS
    Builds up an index file containing information GIT information.
.DESCRIPTION
    Builds up an index file containing information GIT information. The cmd
    scans every subdirectory for a containing .git folder, fetches infos
    and adds it to the index file. The index files is used for later
    multi repository operations (e.g. checking out a specifc branch for several
    GIT repos).
.EXAMPLE
   New-GitRepositoryIndex -RootPathOfGitRepositories "C:\temp\folderContainingMultipleGitRepos"
   Creates an index in the given directory under .index_multi_git_posh.
#>

function New-GitRepositoryIndex {
    [CmdletBinding(SupportsShouldProcess = $true,
        PositionalBinding = $true,
        HelpUri = 'http://www.microsoft.com/',
        ConfirmImpact = 'Low')]
    [Alias()]
    [OutputType([String])]
    Param (
        # Param1 help description
        [Parameter(Mandatory = $false,
            Position = 0)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript ( { Test-Path -Path $_ })]
        $RootPathOfGitRepositories = (Get-Location)
    )
    
    begin {
    }
    
    end {
        $indexDirectoryName = ".index_multi_git_posh"
        $gitIndexFilePath = Join-Path -Path $RootPathOfGitRepositories -ChildPath $indexDirectoryName
        if (Test-Path -Path $gitIndexFilePath) {
            throw "$indexDirectoryName already exists under $RootPathOfGitRepositories. Use 'Update-GitRepositoryIndex' to add new GIT repositories"
        }

        # Scan for GIT repositories
        $foundGitRepositories = Get-ChildItem -Path . -Recurse -Depth 2 -Include ".git" -Hidden
        if ($foundGitRepositories.Count -eq 0){
            Write-Warning "No GIT repositories found under $RootPathOfGitRepositories"
            return
        }

        # Create folder for index file
        New-Item -Path $RootPathOfGitRepositories -Name $indexDirectoryName -ItemType Directory -ErrorAction SilentlyContinue

        $index = @{}
        $index.Repositories = @($foundGitRepositories.Parent.FullName | ForEach-Object `
            -Begin { Push-Location }  `
            -Process {
                Set-Location $_
                # Return GIT info that shall be stored in the index file
                @{
                    Path          = (Get-Location).Path
                    RemoteUrl     = git remote -v | Select-String "(?<remote>http(s)?://.*\s)" | Select-Object -first 1 @{Label = "remote"; Expression = { $_.Matches.Captures.Value } } | select-object -ExpandProperty remote
                    DefaultBranch = (git branch -a ) | ForEach-Object { $_ -match "(?<branch>^\*\s.*)" } | Where-Object { $_ } | ForEach-Object { $Matches.branch.Trim('*').Trim()} | Select-Object -First 1
                    Name = Split-Path -Path (git rev-parse --show-toplevel) -Leaf
                }
            } `
            -End { Pop-Location } 
        )

        # Create the index file
        $index | ConvertTo-Json | Out-File -LiteralPath (Join-Path -Path $gitIndexFilePath -ChildPath $IndexFileName)
    }
}