2

I am currently trying to create a PowerShell script that performs the same functionality as cd, but also checks for a .git folder (new directory is a git repository) and then if true subsequently fetches and performs git status.

I am trying to debug at the moment in PowerShell ISE, but in the debugger my script skips straight over the blocks inside of the if..else statements. Is this a syntax error, or should the if..else work correctly?

function gd {
    #set parameters taken from program (only file location)
    Param(
        [Parameter(Position=0)]
        [String]$location
    )

    #get current directory location
    [String]$Cl = $(Get-Location)

    Set-Location $location

    [String]$Nl = $(Get-Location)

    if ($Cl -eq $Nl) {
        return
    } else {
        Get-ChildItem -Hidden | Where-Object {
            $_.Name -eq ".git"
        } | Write-Output "Eureka!";
        git fetch;
        git status;
        return
        Write-Output "No .git found here!"
    }
}

P.S: I am aware that the long Where-Object pipe is awful (and indubitably non-functional as-is), but this is my 1st script. Would welcome any help with it but my main issue is the execution skipping the if/else code blocks.

6
  • Why not making $location mandatory or defaulting to current location and checking if (Test-Path (Join-Path $location ".git")) ? Commented May 20, 2018 at 21:27
  • I did want to make $location mandatory but don't know how to enter multiple param constraints at once. However Test-path is a good idea, I'll give that a go. Commented May 20, 2018 at 21:50
  • Your test-path is better semantics in the script, but I still can't my if statements to execute properly unfortunately Commented May 20, 2018 at 22:18
  • 1
    Do you want to run git fetch and git status only when you cd into the root of a git tree? Or for subfolders as well? Commented May 21, 2018 at 0:43
  • Addendum: judging from your initial code formatting you seem to be under the impression that git fetch and git status were part of the pipeline and would only run if Where-Object found a match. That is not the case. Both commands will be run every time $Cl and $Nl are not the same. Commented May 21, 2018 at 8:37

2 Answers 2

1

Hi you have an error in your Where-Object pipe which should have been an another if block. See my modified code and it works for me.

function gd {
    #set parameters taken from program (only file location)
    Param(
        [Parameter(Position=0)]
        [String]$location
    )
    #get current directory location
    [String]$Cl = $(Get-Location)

    Set-Location $location
    $location
    [String]$Nl = $(Get-Location)

    if ($Cl -eq $Nl) {
    return
    } else {
        if(Get-ChildItem -Hidden | Where-Object {
            $_.Name -eq ".git"
        } ) 
    {
        Write-Output "Eureka!"
        git fetch;
        git status;
        }
        else{
        Write-Output "No .git found here!"
    }
    }
}
gd D:\<git-folder>

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou I'll give this a try on the Powershell ISE, possibly the incorrect pipe is stopping it from taking the expected execution path
1

Just use Test-Path to check for a .git subfolder. I'd also recommend checking if the repository actually was cloned before calling git fetch.

function Set-LocationGit {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true)]
        [String]$Location
    )

    if ($Location -eq $PWD.Path) {
        return  # path not changed => nothing to do
    }

    Set-Location $Location

    if (Test-Path -LiteralPath '.\.git' -Type Container) {
        if (git config --get 'remote.origin.url') { git fetch }
        git status
    }
}

2 Comments

I appreciate this solution, but not sure I understand all of it such as the flags on Test-Path. Only really wanted to make a simple script but I am sure this solution is likely more efficient/accurate. One thing I am curious about is how do you know that git config --get returns a boolean value?
git config --get does not return a boolean value. But PowerShell doesn't need it to in the first place. The parameters for Test-Path are explained in the documentation as well as the cmdlet help (Get-Help Test-Path).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.