Private/Add-GitFile.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
function Add-GitFile
{
    [CmdletBinding(ConfirmImpact = "Medium", SupportsShouldProcess)]
    Param(
        [Parameter(ValueFromPipeline)]
        $InputObject,

        [switch]$Commit
    )

    if ($InputObject)
    {
        $path = ConvertTo-Path $InputObject;
        if ($Commit -and ($path) -and (Test-Path $path -PathType Leaf) -and (Test-Git))
        {
            if ($PSCmdlet.ShouldProcess($InputObject, "git-add"))
            {
                try
                {
                    Split-Path $path -Parent | Push-Location;
                    &git add $path | Out-Null;
                    Write-Verbose "Staged '$path'.";
                    return $path;
                }
                finally { Pop-Location; }
            }
            else { return $path; }
        }
    }

    return $false;
}