3

I am using a script to checkout and update a list of branches. I am able to retrieve git credentials from the Windows Credential Manager and I need to respond to the prompt for git credentials using them.

How do I send text to the git user prompt from my script? I have tried ECHO and piping the text directly to the git call but neither method is working (the prompt still appears and nothing is typed in).

[PSCredential]$cred = Get-StoredCredential gitCred
$cur_head = "$(git rev-parse --abbrev-ref HEAD)"
$cred.Username |  git pull origin ${cur_head} -q

or

ECHO $cred.Username |  git pull origin ${cur_head} -q

Note: I am using the windows credential manager to store my git credentials, I do not want to put them in any other credential manager

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Ridiculon
  • 323
  • 4
  • 12

1 Answers1

3

In a couple of my scripts I use this:

$env:GIT_USER="."
$env:GIT_PASS=$token
Invoke-Git config --global credential.helper "!f() { echo \`"username=`${GIT_USER}`npassword=`${GIT_PASS}\`"; }; f"

To pass an arbitrary username/password from the environment of the running powershell session. The code above passes a Personal Access Token, but you can easily set the environment variables with a different value.

You could also install the Git Cretential Manager (Core), which would automatically fetch the creds from the windows credential manager.

For those interested in the implementation of Invoke-Git:

function Invoke-Git {
<#
.Synopsis
Wrapper function that deals with PowerShells peculiar error output when Git uses the error stream.
.Example
Invoke-Git ThrowError
$LASTEXITCODE
#>
    [CmdletBinding()]
    param(
        [parameter(ValueFromRemainingArguments=$true)]
        [string[]]$Arguments
    )

    & {
        [CmdletBinding()]
        param(
            [parameter(ValueFromRemainingArguments=$true)]
            [string[]]$InnerArgs
        )
        if ($isDebug) { "git $InnerArgs" }
        git $InnerArgs
    } -ErrorAction SilentlyContinue -ErrorVariable fail @Arguments

    if ($fail) {
        $fail.Exception
    }

}
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 1
    Nice, but it's worth clarifying what `Invoke-Git` is, given that it's not a standard cmdlet. Is it just a wrapper around the `git` executable with error handling? Out of curiosity: what is the benefit of defining the `f` function instead of using just the `echo` command alone? – mklement0 Apr 12 '22 at 17:58
  • This looks useful over all, I would also like to know where `Invoke-Git` is coming from! – Ridiculon Apr 12 '22 at 18:12
  • 1
    Subbing `git` for `Invoke-Git` allows me to run it, and it works perfectly – Ridiculon Apr 12 '22 at 18:31
  • 1
    Why a function... No clue, I'm not a king in bash and someone pointed me to this ages ago and it works. Why change it ;). – jessehouwing Apr 12 '22 at 21:07
  • @jessehouwing, thanks for updating. Fair enough re function :) From what I can tell (using direct invocation of `git`), the following will do (the `!` prefix tells `git` that the value is a shell command): ``git config --global credential.helper "! echo \`"username=`${GIT_USER}`npassword=`${GIT_PASS}\`""`` – mklement0 Apr 12 '22 at 22:49