0

Using posh-git in powershell I am able to list all the items in the working tree that have changed, by using the collection variable $GitStatus.Working, I can use this list in a where clause to filter a directory listing like so

dir | where {$GitStatus.Working -contains $_.Name}

this works really well to display all the files in the current directory that are altered but not yet in the index, however when I run this same command with the -recurse directive the file(s) no longer match. I don't understand why. Any ideas?

(Note Posh-GiTDir from Scott Hanselman suffers from the same problem, not surprisingly as he uses basically the same technique, to show the "Git" column)

Edit: This started when I noticed that the Posh-GitDir Git column did not display anything when I did a recursive directory listing. It's useful to be able to quickly see what's changed "from this folder down", I looked at how Posh-GitDir gets its info to try and debug why it wasn't working for a recurse.

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92

1 Answers1

1

That is because you are using $_.Name, but when you do git status, which is what $GitStatus would give you from Posh-Git, it will give the directory and the file name. So when you are considering the current folder only ( dir without -recurse ) it works out find as it will only be the names. But when you consider files within directories, they will be relavtive paths and will not match with $_.name

And btw, $GitStatus.Working is SUPPOSED to show the files in the repo that are in working directory but not added. Why are you trying to get a list of all files and filtering it with the items from $GitStatus.Working

If you want FileInfo object from the $GitStatus.Working, try doing:

$GitStatus.Working | gi

Edited to show the final solution based on this answer:
So for my purposes, what I did was....

$working = $GitStatus.Working | gi | %{$_.FullName}
dir -recurse | where {$working -contains $_.FullName}

(edited the edit, the previous snippet was not correct.)

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • That was my first thought as well. However the Name field does not appear to be relative with a -recurse (and yes I am aware that this is another problem...i.e. potential false positives) – Tim Jarvis Mar 20 '12 at 23:54
  • @TimJarvis - Name property is just the name of the file yes, but $GitStatus.Working will give you `path/file1` and you are seeing if it has `file1`. So it will not work. – manojlds Mar 20 '12 at 23:58
  • mmm, true. :-) now I am confused why it works when in the current directory. In this case I am comparing path/file with file as well. Surely neither should work. But it's got to be a path issue as I just noticed that if I change to the dir with the wrong case, that also breaks it. (posh-gitdir also has that problem) – Tim Jarvis Mar 21 '12 at 00:21
  • Seems like what I need to do is get the output from the $GitStatus collections, pre-pend the drive:\gitdir and convert the / to \ then match on FullName....sound about right? – Tim Jarvis Mar 21 '12 at 00:23
  • @TimJarvis - Hmmm, I donno what your exact case is, but note that `git status` give `dir1` if you create a new directory `dir1` with file `b`. It will not show `dir1/b`. See if this has any relevance. Also check my updated answer. Update your question with sample situation so that I can explain it. – manojlds Mar 21 '12 at 00:24
  • @TimJarvis - See my updated answer- why are touching your nose around your head? :) – manojlds Mar 21 '12 at 00:25
  • "touching my nose around my head" what the hell is that supposed to mean? - I Updated my question. – Tim Jarvis Mar 21 '12 at 00:48