dahlbyk / posh-git

A PowerShell environment for Git
http://dahlbyk.github.io/posh-git/
MIT License
7.7k stars 801 forks source link

Get-GitStatus ignores -GitDir argument for critical data #881

Open AArnott opened 2 years ago

AArnott commented 2 years ago

System Details

Issue Description

The Get-GitStatus function ignores the -GitDir parameter for crucial parts of its work. Specifically it invokes git without setting the current working directory for that process to the path specified.

Note how in the below terminal log, Get-GitStatus claims to return data for ..\Lib.Template but still claims that the branch is master. But after actually switching to that directory, the result changes to the (correct) main branch name.

C:\git\Validation.my [master]> git b
* master
C:\git\Validation.my [master]> get-gitstatus -GitDir ..\Lib.Template\

RepoName     : git
BehindBy     : 0
StashCount   : 0
HasWorking   : False
GitDir       : ..\Lib.Template\
Working      : {}
Branch       : master
Index        : {}
HasUntracked : False
Upstream     :
UpstreamGone : False
AheadBy      : 0
HasIndex     : False

C:\git\Validation.my [master]> pushd ..\Lib.Template\
C:\git\Lib.Template [main]> Get-GitStatus

RepoName     : Lib.Template
BehindBy     : 0
StashCount   : 0
HasWorking   : False
GitDir       : C:\git\Lib.Template\.git
Working      : {}
Branch       : main
Index        : {}
HasUntracked : False
Upstream     :
UpstreamGone : False
AheadBy      : 0
HasIndex     : False
dahlbyk commented 2 years ago

Belated thanks for the bug report. You're absolutely right.

We mostly use -GitDir as an "are we in a git dir?" check and don't pass it to any of the git invocations. Most of our invocations would just need --git-dir, but git status will also require --work-tree.

Tentative plan would be something like: