0

We have a psake orchestration that prompts a user with the following message before calling 'git clean -xdf':

About to delete all untracked files. Press 'Y' to continue or any other key to cancel.

We'd like to show this prompt ONLY if there are untracked files in the repository that would be removed by running the clean -xdf.

Any suggestions as how to use posh-git to answer the question "are there any untracked changes in the repository?" from PowerShell?

Here is the existing orchestration, for reference...

task CleanAll -description "Runs a git clean -xdf" {
    Write-Host "About to delete any uncommitted changes.  Press 'Y' to continue or any other key to cancel." -foregroundcolor "yellow"
    $continue = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp").Character
    IF ($continue -eq "Y" -or $continue -eq "y")
    {
        git clean -xdf
    }
    ELSE
    {
        Write-Error "CleanAll canceled."
    }
}
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
  • 1
    `git clean -xdf` doesn't revert changes to tracked files it deletes *untracked* and ignored files. – Etan Reisner Mar 12 '15 at 20:55
  • @EtanReisner - Fair enough. But the question is really about conditionally showing or skipping the prompt, and this doesn't answer that question. I'll update the question to be more "correct." Regardless, thanks for the response. – bopapa_1979 Mar 13 '15 at 17:08
  • 1
    The question could have been about a misunderstanding. If you thought that's what `git clean` did then you might have been trying to do this for no reason since it doesn't. And more to the point how best to answer the question depends (somewhat) on what changes/etc. you actually need to test for. – Etan Reisner Mar 13 '15 at 17:25
  • @EtanReisner, thanks for the critique. Got any suggestions with how to proceed? – bopapa_1979 Mar 13 '15 at 17:39
  • 1
    Use the output from `git status --porcelain` or `git status -z` and look for `??` markers on files. Those are untracked files. I can't write that for powershell off the top of my head though. Alternatively just check whether `git clean -xdn` outputs anything. – Etan Reisner Mar 13 '15 at 18:11
  • @EtanReisner - That is very helpful, thank you. Once I get it working, I'll post the results here for everyone else. Thanks again. Also, I'll take your comments above to heart and work writing better questions to begin with. – bopapa_1979 Mar 17 '15 at 15:02

1 Answers1

0

After help from @EtanReisner, I was able to get the following solution working. It checks for untracked changes and prompts if there are any. Otherwise it just does the git clean -xdf.

$gitStatus = (@(git status --porcelain) | Out-String)

IF ($gitStatus.Contains("??"))
{
    Write-Host "About to delete any untracked files.  Press 'Y' to continue or any other key to cancel." -foregroundcolor "yellow"
    $continue = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp").Character
    IF ($continue -eq "Y" -or $continue -eq "y")
    {
        git clean -xdf
    }
    ELSE
    {
        Write-Error "CleanAll canceled."
    }
}

git clean -xdf
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76