3

In bash I have this:

git verify-pack -v .git/objects/pack/pack-*.idx | grep -v chain | sort -k3nr

giving this output:

...
d0a5144e720923b85489e62fe2f9699c8340a90e blob   5 16 106352184 1 ba72dc312a0fc209570b065fd6206c35f87f1198
58f23716aefb940d7d7ea44c1f3004416ac5f065 blob   4 14 12686887 1 89635906c50a2b6e89a78edc6e20e43015dfaa93
81a12bc733bffe5aab087c277f7f289f82975ac0 tree   4 14 110911883 2 10b9544959d69a4cd13f817c883b0b329c59d6d3
8397914fbc571787ff21320903091e4d2ef50907 tree   4 14 110911733 1 c641e2aee4f50a04b539ca40266b0a8718da1570
fe277f883e6cebe9a74d593b7be650ba7610bdfb blob   4 14 55841383 2 180071e183d47e204255be3009ba53a1d4ad069e
5f282702bb03ef11d7184d19c80927b47f919764 blob   3 12 9966998
.git/objects/pack/pack-44f6fe97b177e9c6b978dfe7879b9cf47a98748b.pack: ok
.git/objects/pack/pack-5009b2f54d6bae62e230c593b496d49e45cc5032.pack: ok
.git/objects/pack/pack-69467a1623c6d40146fae8f2e8da9c6ce0bdb5d8.pack: ok
.git/objects/pack/pack-740b36b772adf621529ca11126536f5727c3666d.pack: ok
.git/objects/pack/pack-923e6174d0fb5eca889bd983a7b2305b21c7b0ad.pack: ok
.git/objects/pack/pack-b1777793d03a38f70ad69f81bedfdd4c0cb82348.pack: ok
.git/objects/pack/pack-bf4951ba56f92700e5686fd9f385cb2cb9436b54.pack: ok
.git/objects/pack/pack-f3a758460a0a075a64e84bbb378162969cbcacf2.pack: ok
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 blob   0 9 13814182

Based on: Equivalent of git verify-pack -v | sort | tail and the suggestions below I am trying to do the same from PowerShell:

  git verify-pack -v (Get-ChildItem ".git\objects\pack\pack-*.idx")`
  | Select-String -NotMatch -Pattern "chain" `
  | % { $_.Line } `
  | Sort {[int]($_ -split ' ')[3]}

The last part of the result is this:

    ...
    f583c1c4736334f88a379f35ba74966cb61eea32 commit 1026 392 652028
    9a92d4985f549b587410145b0c59953f2e58d0a1 commit 641 408 870110
    e061e2ddfea5fe4c400a5277ff084ab05603a366 commit 1436 411 533973
    15160a6ca1c7137fc8b52676291d2e5429428949 commit 1351 414 877215
    bac4a21a5a4c7da14276415c2e8de999d20609e6 commit 1587 420 908493
    a2af58831c77adb0904cb99561ddf80b73994b38 commit 845 420 444487
    ced62e9fe611f449510d5c0a637d7a673f9b20b9 commit 774 427 480249
    22ccd25bb0e6de82373d0b95f3131d3592cedecf commit 1821 438 907649
    534017b3e8707f1524a3ac1f13934729245d54dd commit 1303 443 36133
    a46ea0dd61280c65d3ef3d69559341eb9029358b commit 1161 451 973617
    8328ecd726d18a46ea72da04c19af987e6715944 commit 1424 459 398884
    45bd595436e20a98221847040194ae5ab5660a8e commit 1835 463 758557
    319455259cad21e73e17890f4e9cba9c06e845bf commit 1343 472 449402
    177de1b8f8320877b029d82e49b9dbc3b507fb37 commit 1983 483 468022
    e266ad16939806066d3f4c7baca0afc88d67bfe6 commit 1455 497 309591
    45d338ef45f320289c03cd96e5f1fb3ec505b6d9 commit 1645 517 583296
    eb4c15e3a1e582f9a5c20073a9c24dd79825abf1 commit 2137 530 57469
    3043a3041960397925480dd6a5f30b9498adeed9 commit 6153 718 1011624
    f5ea0c33d06eac001f7d3b2830b1ccd841a92247 commit 7255 1130 137037

Which is not the same as the output from bash.

I have verified that the bash and powershell commands up to this point outputs the same:

# Bash
git verify-pack -v .git/objects/pack/pack-*.idx | grep -v chain

#PowerShell
git verify-pack -v (Get-ChildItem ".git\objects\pack\.git\objects\pack\pack-*.idx") | Select-String -NotMatch -Pattern "chain"

Any suggestions to why the sort call changes the output in PowerShell?

I made a minimal test using the below file test.small.input:

b97916c219f1f0d229dc93eadb8e1bee8e021a66 blob   24 33 132341747 1 f8b3ab8251d3e013d194c3d999d0725be96c899e
c62cc937c9a30f04900b50feb7d9b3ac350ed4f3 blob   31 38 132341780 1 f8b3ab8251d3e013d194c3d999d0725be96c899e
e3034004302aeb24899dc93460379244cca9009f tree   262 215 132341818
ab4c1f12673e2556dce2266846a8f0533aa2ab7e tree   443 304 132342033
31e2c7a673f5e05c0c9b3d48e07eee0c2e205edb tree   55 70 132342337 1 ab4c1f12673e2556dce2266846a8f0533aa2ab7e

Sort using bash:

sort C:/tmp/test.small.input -k3nr

or

sort C:/tmp/test.small.input -k4nr

gives:

ab4c1f12673e2556dce2266846a8f0533aa2ab7e tree   443 304 132342033
e3034004302aeb24899dc93460379244cca9009f tree   262 215 132341818
31e2c7a673f5e05c0c9b3d48e07eee0c2e205edb tree   55 70 132342337 1 ab4c1f12673e2556dce2266846a8f0533aa2ab7e
c62cc937c9a30f04900b50feb7d9b3ac350ed4f3 blob   31 38 132341780 1 f8b3ab8251d3e013d194c3d999d0725be96c899e
b97916c219f1f0d229dc93eadb8e1bee8e021a66 blob   24 33 132341747 1 f8b3ab8251d3e013d194c3d999d0725be96c899e

Sorting the same file using PowerShell:

  Get-Content c:\tmp\test.small.input | Sort {[int]($_ -split ' ')[4]} -Descending

gives:

ab4c1f12673e2556dce2266846a8f0533aa2ab7e tree   443 304 132342033
e3034004302aeb24899dc93460379244cca9009f tree   262 215 132341818
31e2c7a673f5e05c0c9b3d48e07eee0c2e205edb tree   55 70 132342337 1 ab4c1f12673e2556dce2266846a8f0533aa2ab7e
c62cc937c9a30f04900b50feb7d9b3ac350ed4f3 blob   31 38 132341780 1 f8b3ab8251d3e013d194c3d999d0725be96c899e
b97916c219f1f0d229dc93eadb8e1bee8e021a66 blob   24 33 132341747 1 f8b3ab8251d3e013d194c3d999d0725be96c899e

So seems there is some differences in how the PowerShell/Split/index and bash index works.

Community
  • 1
  • 1
u123
  • 15,603
  • 58
  • 186
  • 303

1 Answers1

2

I don't have anything in the pack directories on my git repos so I can't quite test this, but I suspect the reason is that Select-String does not return [string]s, it returns match objects.

You have to be careful in PowerShell when comparing the output of a command against the output of something else. It might look the same, but it may not be the same. Generally PowerShell is returning some kind of object, and certain objects control how they are displayed. Sometimes the output is specifically formatted to look similar to a bash or command prompt command that it seeks to replicate.

To check, you can use Get-Member or the .GetType() method.

git verify-pack -v (Get-ChildItem ".git\objects\pack\.git\objects\pack\pack-*.idx") | 
    Select-String -NotMatch -Pattern "chain" | Get-Member

Be careful with this implementation though; because of the way the pipeline works, an array will be unrolled and Get-Member will give the info for each object in the array (unique only, if it's 100 strings, it's only going to show the info once). This may be what you want, or you might want to to know the collection type.

In that case, you can use it this way:

Get-Member -InputObject (git verify-pack -v (Get-ChildItem ".git\objects\pack\.git\objects\pack\pack-*.idx") | 
    Select-String -NotMatch -Pattern "chain")

# or

$returnValue = git verify-pack -v (Get-ChildItem ".git\objects\pack\.git\objects\pack\pack-*.idx") | Select-String -NotMatch -Pattern "chain"
Get-Member -InputObject $returnValue

If you confirm that you're not getting strings, you can move onto a solution that works correctly (probably piping to Select-Object -ExpandProperty or ForEach-Object).

briantist
  • 45,546
  • 6
  • 82
  • 127
  • `Select-String` indeed returns custom objects (not only the match, but also context around it, etc.). You can get strings by using `Select-String ... | % Line`. Or simply use `Get-Content` and filter with `-match`. – Joey Jun 06 '16 at 13:29
  • I just tried to add the % Line as you suggested but it has not effect. The output is the same if I use that or not. – u123 Jun 06 '16 at 17:58
  • @u123 I think Joey's code was slightly off there, it would be `Select-String | % { $_.Line }`, which you could also accomplish with `Select-String | Select-Object -ExpandProperty Line`. – briantist Jun 06 '16 at 18:02
  • I updated based on your corrections, now I get something else, but still not the same as the bash result. – u123 Jun 06 '16 at 18:10
  • @briantist: In recent PowerShell versions you can just pass a property name to `ForEach-Object` which will then be expanded. (PowerShell v5 here) – Joey Jun 06 '16 at 18:25
  • @Joey ah right I forgot about that; Personally, I strongly dislike that syntax (but I must keep it in mind for potential [code golfing](http://codegolf.stackexchange.com/)). – briantist Jun 06 '16 at 18:27
  • @briantist: That's where it comes in very handy, indeed ;-) – Joey Jun 06 '16 at 18:33