Public/Get-GitChangedFile.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
function Get-GitChangedFile {
    <#
    .SYNOPSIS
        Get a list of files changed in a git commit

    .FUNCTIONALITY
        CI/CD

    .DESCRIPTION
        Get a list of files changed in a git commit
        Uses git diff to find changed files
        By default it will get you the files changed in the current commit,
        or a specific commit can be specified.
        You can also use LeftRevision, RightRevision, and RangeNotation parameters
        to specify your own range, useful when determing what has changed in a merge
        or what would change in a hypothetical merge

    .PARAMETER Path
        Path to git repo. Defaults to the current working path

    .PARAMETER Commit
        Commit hash

    .PARAMETER Include
        If specified, only return files that are '-like'
        an item in the -Include
        If both Include and Exclude are specified, files that match the Include and don't match the Exclude will be returned
        In other words, the Include is applied first, and then any Exclude patterns are removed

    .PARAMETER Exclude
        If specified, exclude any files that are '-like'
        an item in the -Exclude
        If both Include and Exclude are specified, files that match the Include and don't match the Exclude will be returned
        In other words, the Include is applied first, and then any Exclude patterns are removed

    .PARAMETER Resolve
        If specified, run Resolve-Path on the determined git path and file in question

    .PARAMETER DiffFilter
        If specified, use this string as a value to the --diff-filter parameter of git diff.
        Some examples:
        "A" would only return files added
        "M" would only return files modified
        "AM" would only return files that were added or modified
        "d" would exclude files that have been deleted
        "dm" would exclude files that have been deleted or modified
        More information on the --diff-filter available at https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203

    .PARAMETER LeftRevision
        If specified, use this value as part of a range comparision along with RangeNotation and optionally RightRevision

    .PARAMETER RangeNotation
        If specified, use this value as part of a range comparision along with LeftRevision and/or RightRevision
        If not specified, but either or both of LeftRevision and RightRevision are specified, it will default to triple dot notation.
        Defaults to triple dot (...) notation, which will only show changes in one "direction"
        Double dot (..) notation can also be specified, which shows all changes between the two revisions

    .PARAMETER RightRevision
        If specified, use this value as part of a range comparision along with RangeNotation and optionally LeftRevision

    .PARAMETER RawRevisionString
        If specified, this value will be passed directly to git diff instead of constructing a revsion based on other parameters.
        This gives you more flexibility in how you specify your revisions

    .EXAMPLE
        Get-GitChangedFile
        # Get files changed in the most recent commit
        # Use the current directory as the git repo path

    .EXAMPLE
        Get-GitChangedFile -Path \\Path\To\Git\Repo
        # Get files changed in the most recent commit
        # Use \\Path\To\Git\Repo as the git repo path

    .Example
        Get-GitChangedFile -Commit 3d6b25ebbc6bbf961a4c1045548bc9ff90879bc6

        # Get files changed in commit 3d6b25ebbc6bbf961a4c1045548bc9ff90879bc6,
        # Use the current directory as the git repo path

    .EXAMPLE
        Get-GitChangedFile -LeftRevision "origin/master"
        # Shows the files that are "ahead" of orgin/master
        # These are the same files that you would see if you used the
        # Compare button to compare your current branch to master
        # When one revision is not specified it is assumed to be
        # HEAD (the currently checked out revision)
        # This command is functionally equivalent to:
        # Get-GitChangedFile -LeftRevision "origin/master" -RightRevision "Head"
        # Get-Get-GitChangedFile -LeftRevision "origin/master" -RangeNotation "..." -RightRevision "Head"
        # Get-Get-GitChangedFile -LeftRevision "origin/master" -RangeNotation "..."

    .Example
        Get-GitChangedFile -RawRevisionString "origin/master..."

        # Functionally equivalent to passing "origin/master" as a parameter to LeftRevision as described above

    .LINK
        https://github.com/RamblingCookieMonster/BuildHelpers

    .LINK
        https://git-scm.com/docs/git-diff

    .LINK
        about_BuildHelpers
    #>

    [cmdletbinding(DefaultParameterSetName="All")]
    param(
        [validateScript({ Test-Path $_ -PathType Container })]
        [Parameter(ParameterSetName="All")]
        [Parameter(ParameterSetName="Commit")]
        [Parameter(ParameterSetName="Range")]
        [Parameter(ParameterSetName="RangeLeft")]
        [Parameter(ParameterSetName="RangeRight")]
        [Parameter(ParameterSetName="RawRevision")]
        $Path = $PWD.Path,

        [Parameter(Mandatory,ParameterSetName="Commit",Position=0)]
        [string]$Commit,

        [Parameter(Mandatory,ParameterSetName="Range")]
        [Parameter(Mandatory,ParameterSetName="RangeLeft")]
        [string]$LeftRevision,

        [Parameter(ParameterSetName="Range")]
        [Parameter(ParameterSetName="RangeLeft")]
        [Parameter(ParameterSetName="RangeRight")]
        [ValidateSet("..","...")]
        [string]$RangeNotation="...",

        [Parameter(Mandatory,ParameterSetName="Range")]
        [Parameter(Mandatory,ParameterSetName="RangeRight")]
        [string]$RightRevision,

        [ValidatePattern("^[ACDMRTUXBacdmrtuxb*]+$")]
        [string]$DiffFilter,

        [Parameter(ParameterSetName="RawRevision")]
        [string]$RawRevisionString,

        [string[]]$Include,


        [string[]]$Exclude,

        [switch]$Resolve
    )
    $Path = (Resolve-Path $Path).Path
    try
    {
        $GitPathRaw = Invoke-Git rev-parse --show-toplevel -Path $Path -ErrorAction Stop
    }
    catch
    {
        if ($_ -like "fatal: not a git repository*" )
        {
            throw "Could not find root of git repo under [$Path], are you sure [$Path] is in a git repository?"
        }
        else
        {
            throw $_
        }
    }
    Write-Verbose "Found git root [$GitPathRaw]"
    $GitPath = Resolve-Path $GitPathRaw
    if(Test-Path $GitPath)
    {
        Write-Verbose "Using [$GitPath] as repo root"
    }
    else
    {
        throw "Could not find root of git repo under [$Path]. Tried [$GitPath]"
    }
    if($PSCmdlet.ParameterSetName -eq 'Commit')
    {
        $revisionString = "$Commit^..$Commit"
    }
    elseif ($PSCmdlet.ParameterSetName -like 'Range*')
    {
        $revisionString = $LeftRevision + $RangeNotation + $RightRevision
        Write-Verbose "revision string: $revisionString"
    }
    elseif ($PSCmdlet.ParameterSetName -eq 'RawRevision')
    {
        $revisionString = $RawRevisionString
    }
    else
    {
        $revisionString = "HEAD^..HEAD"
    }
    if ($PSBoundParameters.ContainsKey('DiffFilter'))
    {
        $revisionString += " --diff-filter=$DiffFilter"
    }
    if(-not $revisionString)
    {
        return
    }

    [string[]]$Files = Invoke-Git "diff --name-only $revisionString" -Path $GitPath
    if ($Files) {
        Write-Verbose "Found [$($Files.Count)] files with raw values:`n$($Files | Foreach-Object {"'$_'"} | Out-String)"
        if($Include)
        {
            $Files = Invoke-LikeFilter -Collection $Files -FilterArray $Include -FilterReplace '\','/'
        }
        if($Exclude)
        {
            $Files = Invoke-LikeFilter -Collection $Files -FilterArray $Exclude -FilterReplace '\','/' -Not
        }
        foreach($item in $Files)
        {
            if($Resolve)
            {
                ( Resolve-Path (Join-Path $GitPath $Item) ).Path
            }
            else
            {
                Join-Path $GitPath $Item
            }
        }
    }
    else
    {
        Write-Warning "No files found that match the given criteria"
    }
}