Functions/Get-Repo.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
    .SYNOPSIS
        Gets the latest version of a particular repository from the upstream hosting provider.
 
    .DESCRIPTION
        Used to quickly acquire the latest version of a given repository from a remote hosting provider.
        Will automatically execute a git clone operation if necessary, falling back to git pull if the
        repository already exists locally.
 
    .PARAMETER Repo
        The name of the repository which you would like to fetch.
 
    .PARAMETER Service
        The remote hosting provider from which you would like to fetch the repository.
 
    .PARAMETER Branch
        The repository branch which you owuld like to checkout.
     
    .PARAMETER Path
        The directory within which all of your repositories are stored. Defaults to the value of $GitTool.Directory
        if not specified.
 
    .PARAMETER Reconfigure
        Whether you would like to reconfigure the local repository to use the updated remote address in situations where that has changed.
     
    .PARAMETER Open
        Whether or not to open the repository once it has been fetched.
 
    .EXAMPLE
        Get-Repo sierrasoftworks/git-tool
 
        Synchronizing https://github.com/SierraSoftworks/git-tool
        - Git URL: git@github.com:SierraSoftworks/git-tool.git
        - Target: C:\dev\github.com\SierraSoftworks\git-tool
        Running git pull
        Already up to date.
 
        Name Value
        ---- -----
        WebURL https://github.com/SierraSoftworks/git-tool
        Service github.com
        Repo SierraSoftworks/git-tool
        Path C:\dev\github.com\SierraSoftworks\git-tool
        Exists True
        GitURL git@github.com:SierraSoftworks/git-tool.git
#>

function Get-Repo {
    param(
        [string]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "The name of the repository that you wish to synchronize (e.g. Namespace/RepoName)")]
        $Repo,

        [string]
        [Parameter(HelpMessage = "The service hosting your repository (e.g. github.com)")]
        $Service = $GitTool.Service,

        [string]
        [Parameter(HelpMessage = "The branch to checkout and update (optional).")]
        $Branch = $null,

        [string]
        [Parameter(HelpMessage = "The directory within which your repositories will be checked out (e.g. /src/)")]
        $Path = $GitTool.Directory,

        [switch]
        [Parameter(HelpMessage = "Reconfigure existing local repositories to ensure they use the correct remotes.")]
        $Reconfigure = $false,

        [switch]
        [Parameter(HelpMessage = "Whether to open the repository once it has been updated or not.")]
        $Open = $false
    )

    $info = Get-RepoInfo -Repo $Repo -Service $Service -Path $Path

    Write-Host ""

    Write-Host -NoNewline "Synchronizing "
    Write-Host -ForegroundColor Blue $info.WebURL
    Write-Host -NoNewline " - Git URL: "
    Write-Host -ForegroundColor Red $info.GitURL
    Write-Host -NoNewline " - Target: "
    Write-Host -ForegroundColor Green $info.Path

    if (-not $info.Exists) {
        Write-Host -NoNewline "Running "
        Write-Host -ForegroundColor DarkGray "git clone --recurse-submodules -j8 ${info.GitURL} ${info.Path}"
        git.exe clone --recurse-submodules -j8 $info.GitURL $info.Path
    }
    else {
        Push-Location -Path $info.Path
        try {
            if ($Reconfigure) {
                Write-Host -NoNewline "Running "
                Write-Host -ForegroundColor DarkGray "git remote set-url origin ${info.GitURL}"
                git.exe remote set-url origin $info.GitURL
            }
            
            Write-Host -NoNewline "Running "
            Write-Host -ForegroundColor DarkGray "git pull"
            git.exe pull
        }
        finally {
            Pop-Location
        }
    }

    Push-Location -Path $info.Path
    try {
        if ("" -ne "$Branch") {
            Write-Host -NoNewline "Running "
            Write-Host -ForegroundColor DarkGray "git checkout $Branch"
            git.exe checkout $Branch
        }
    }
    finally {
        Pop-Location
    }

    if ($Open) {
        Set-Location -Path $info.Path
    }

    return $info
}