Functions/Start-GitRebase.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
Import-Module "$PSScriptRoot/New-LibGit2SharpRebaseOptions.psm1" -Scope Local

function Start-GitRebase {
    [CmdletBinding()]
    param (
        # The commit to begin rebasing from, defaults to rebase all reachable commits
        # This is the current base of $Branch
        [Parameter(Position = 0)]
        [string] $Upstream,

        # The terminal commit to rebase, defaults to rebase the current branch
        # This is the branch that will be "moved"
        [Parameter(Position = 1)]
        [Alias('FriendlyName')]
        [Alias('HeadRef')] # PSGitHub.PullRequest
        [string] $Branch,

        # The branch to rebase onto, defaults to rebase onto the given upstream
        [string] $Onto,

        [Parameter()]
        [LibGit2Sharp.CheckoutFileConflictStrategy] $ConflictStrategy,

        [Parameter()]
        [string] $RepoRoot
    )

    process {
        $cancel = $false
        $repo = Find-GitRepository -Path $RepoRoot -Verify
        if (-not $repo) {
            return
        }
        $RepoRoot = $repo.Info.Path
        $branchObject = if ($Branch) { Get-GitBranch -Name $Branch } else { $null }
        $upstreamObject = if ($Upstream) { Get-GitBranch -Name $Upstream } else { $null }
        $ontoObject = if ($Onto) { Get-GitBranch -Name $Onto } else { $null }
        $signature = New-GitSignature -RepoRoot $RepoRoot
        $committer = [LibGit2Sharp.Identity]::new($signature.Name, $signature.Email)
        $options = New-LibGit2SharpRebaseOptions
        $options.OnCheckoutNotify = {
            param([string]$Path, [LibGit2Sharp.CheckoutNotifyFlags]$NotifyFlags)
            Write-Information "$($NotifyFlags): $Path"
            return -not $cancel -and -not $PSCmdlet.Stopping
        }
        if ($PSBoundParameters.ContainsKey('ConflictStrategy')) {
            $options.FileConflictStrategy = $ConflictStrategy
        }
        try {
            $repo.Rebase.Start($branchObject, $upstreamObject, $ontoObject, $committer, $options)
        } finally {
            $cancel = $true
        }
    }
}