0

I need some help with my script; I have done the first part but the second part I'm not entirely sure whether a second foreach is needed. Firstly the script outputs files and hash's from a branch. Next to the hash I have managed to add a notation which i can change later to output "hash" the second part I want to put "file" next to each file outputted, below I have included how the output should be and it is like that; but I just can't seem to get "file" notation next to each filename.

$GitList = git log master..branch2 --pretty=format:'%H' --reverse


foreach ($hash in $GitList) {
    "$hash hash"
    $files = git show $hash --pretty="format:" --name-only
    $files
}


#-------------------------------output should be like below

37b970b35681728db2faa4135681728d hash
branch2/ScriptPart1.ps1 file
37b970b371a7805a657030f687135681728d hash
branch2/ScriptPart2.ps1 file
branch2/ScriptPart3.ps1 file
37b970b30a137b970b381e5d5135681728d hash
branch2/ScriptPart2.ps1 file
321b2feb5d37b970b32d47f4135681728d hash

Thanks in advance

1
  • I don't know Powershell, but yes, you're going to have to loop over the list of $files.
    – Schwern
    Aug 25, 2015 at 19:31

2 Answers 2

1

I would just do a quick and dirty foreach-object and be done with it... Something along the lines of:

$files | Foreach-Object { "{0} file" -f $_ }
0
1

Your main problem might be, that the return value of the line:

git show $hash --pretty="format:" --name-only

is handled by PowerShell as a string array with two items of which the first one contains an empty string.

Adressing the second item directly would be a way to achieve what you want to do here:

$GitList = git log master --pretty=format:'%H' --reverse

foreach ($hash in $GitList) {
    "$hash hash"
    (git show $hash --pretty="format:" --name-only)[1] + ' file'
}

By the way: Depending on your scenario you may consider using PSObjects as output. Especially if you plan to further process the output of your script by PowerShell:

foreach ($hash in $GitList) {
    $file = (git show $hash --pretty="format:" --name-only)[1]
    New-Object -Type psobject -Property @{'Hash'= $hash; 'File' = $file}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.