-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #80 Get-BuildVariable breaks if git is aliased #81
base: master
Are you sure you want to change the base?
Fix #80 Get-BuildVariable breaks if git is aliased #81
Conversation
f259da7
to
5d8069d
Compare
This attempt at a fix does not pass testing on my local machine. More work is needed and I need to add tests. |
5d8069d
to
4ff88c2
Compare
In testing I discovered that Invoke-Git was also vulnerable to this issue. I will leave the issue and PR title unchanged. The existing tests now pass on my machine. |
@@ -75,7 +75,7 @@ function Get-BuildVariables { | |||
$Path = ( Resolve-Path $Path ).Path | |||
$Environment = Get-Item ENV: | |||
if(!$PSboundParameters.ContainsKey('GitPath')) { | |||
$GitPath = (Get-Command $GitPath -ErrorAction SilentlyContinue)[0].Path | |||
$GitPath = (Get-ChildItem ($env:PATH -split ';') -filter git.exe -ErrorAction SilentlyContinue | Select-Object -First 1).Fullname |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to fix the fact that git.exe is hard coded here. I will either need to change the -filter
to something like -filter "*$GitPath*"
or change the param default to git.exe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've changed my mind on this. If the user provides a git path they should be providing a full path to the git executable. If they provide anything at all this variable is given directly to the Process Start info Filename Parameter anyway, so if they do something like provide an input that it can't use, it will already fail.
If the user does not provide input to the $GitPath parameter, we already know conclusively that we want to search for git.exe. We aren't searching for git.com or git.bat, or anything like that.
This says to me that it is safe to simply leave the $GitPath parameter optional, and there is no need to provide a default value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hiyo! @gaelcolas has an old PR in one of my repos for a similar thing, looking to resolve just git
(this should still pick up git.exe
, but buys us compatibility with systems that don't use .exe
). Might be worth looking into something like this here? (This is a pre-skimming-your-whole-commit-and-my-old-code
comment : P)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot about this PR for a while, and suddenly I get a notice that there was a comment. If @gaelcolas wants to link me the PR that would be great. Otherwise I'll go searching around for it on Monday.
@@ -84,7 +84,7 @@ | |||
# http://stackoverflow.com/questions/8761888/powershell-capturing-standard-out-and-error-with-start-process | |||
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |||
if(!$PSBoundParameters.ContainsKey('GitPath')) { | |||
$GitPath = (Get-Command $GitPath -ErrorAction Stop)[0].Path | |||
$GitPath = (Get-ChildItem ($env:PATH -split ';') -filter git.exe -ErrorAction SilentlyContinue | Select-Object -First 1).Fullname |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
[hub](https://github.com/github/hub) is a tool put out by git that adds commandline tools to help developers work with github from the commandline in a more natural way. It is designed to be used by aliasing git to hub.exe. Hub then handles the additional commands it knows how to implement, and transparently passes through any native git commands to git.exe. The Get-BuildVariable cmdlet does not account for the possibility that git might be aliased, and assumes that the output of Get-Command 'git' can authoritatively be treated as either returning a Path to git or determining that git is not installed on a system. Unfortunately if git is aliased to hub.exe then the result of Get-Command is not an object of type `System.Management.Automation.ApplicationInfo` that has a `Path` property but an object of type `System.Management.Automation.AliasInfo` which does not contain a path either to git or to the executable it's been aliased to. One possible strategy would be to find that git has been alaised and find the path to executable that it has been aliased to, but this seems less than ideal since the third party binary is not what this module really wants to execute, and this module will only ever need the capabilities of the native git.exe, not the extended capabilities of hub. This change switches the method of determining the path to git over to looking at each path in the $env:PATH variable, and returning the path to the first instance of git.exe that it finds. This should be the same method used by Get-Command, and isn't fooled by the presence of aliases. This change should be a fix for RamblingCookieMonster#80
4ff88c2
to
24d3518
Compare
@@ -69,13 +69,13 @@ function Get-BuildVariables { | |||
} | |||
$true | |||
})] | |||
$GitPath = 'git' | |||
$GitPath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hard coded git.exe we are searching for makes this default value unnecessary.
@@ -77,14 +77,14 @@ | |||
} | |||
$true | |||
})] | |||
[string]$GitPath = 'git' | |||
[string]$GitPath |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hard coded git.exe we are searching for makes this default value unnecessary.
I have not been able to find a good way to add another set of tests for the git commands with an invalid alias like The closest I have been able to do is manually test via the following procedure.
|
hub is a tool put out by git that adds
commandline tools to help developers work with github from the
commandline in a more natural way. It is designed to be used by aliasing
git to hub.exe. Hub then handles the additional commands it knows how to
implement, and transparently passes through any native git commands to
git.exe.
The Get-BuildVariable cmdlet does not account for the possibility that
git might be aliased, and assumes that the output of Get-Command 'git'
can authoritatively be treated as either returning a Path to git or
determining that git is not installed on a system. Unfortunately if git
is aliased to hub.exe then the result of Get-Command is not an object of
type
System.Management.Automation.ApplicationInfo
that has aPath
property but an object of type
System.Management.Automation.AliasInfo
which does not contain a path either to git or to the executable it's
been aliased to.
One possible strategy would be to find that git has been aliased and
find the path to executable that it has been aliased to, but this seems
less than ideal since the third party binary is not what this module
really wants to execute, and this module will only ever need the
capabilities of the native git.exe, not the extended capabilities of hub.
This change switches the method of determining the path to git over to
looking at each path in the $env:PATH variable, and returning the path
to the first instance of git.exe that it finds. This should be the same
method used by Get-Command, and isn't fooled by the presence of aliases.
This change should be a fix for #80