Foreach - add notation to foreach loop, do i need a nested foreach

Hi, 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


August 25th, 2015 2:38pm

Looks to me like $files is a collection, which means you will need to loop through, maybe something like this will work?

git log master.branch2 --pretty=format:'%H' --reverse | Foreach{
  $hash = $_
  "$hash hash"
  git show $hash -pretty="format:" --name-only | Foreach {
    "$_ file"
  }
}

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:08pm

no that didn't work, is there a way of keeping the structure how it is in my script and adding another foreach. I have tried and i'm not getting anywhere
August 25th, 2015 3:29pm

No that didn't work doesn't help much, where there errors, was the formatting not right?? To keep it as it was then do it this way

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


foreach ($hash in $GitList) {

"$hash hash"

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

foreach($file in $files)
{
  "$file file"
}

}
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:40pm

You've stated what you want your output to eventually look like, but remember that we have no way to know what it currently looks like.

99.9% of us have no way to recreate your input, so you'll have to give us something to go on before we can help you.

August 25th, 2015 3:41pm

Thanks the output shows as below; Except I added the comment 'file' next to the file names but below is the actual output from the command line; 37b970b35681728db2faa4135681728d hash branch2/ScriptPart1.ps1 file 37b970b371a7805a657030f687135681728d hash branch2/ScriptPart2.ps1 file branch2/ScriptPart3.ps1 file 37b970b30a137b970b381e5d5135681728d hash branch2/ScriptPart2.ps1 file 321b2feb5d37b970b32d47f4135681728d hash
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 4:33pm

You are asking for "git" to return a formatted result for printing then trying to use it as a collection of strings. This won't work.

First check "GitHub" for the newest PowerShell module so you can actually query GitHub  Also post your issues in the github forum.  They will help you to learn how to use the PS module.

https://github.com/powerShell

August 26th, 2015 12:11am

Jrv, are you here to make my powershell learning a nightmere. Don't answer that because you will only promote negetivity. It worked just to let you know what i was trying to do. 
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 2:31pm

Jrv, are you here to make my powershell learning a nightmere. Don't answer that because you will only promote negetivity. It worked just to let you know what i was trying to do. 

You still haven't actually given us anything to work with.

If you refuse good advice (using the proper tool for the job) all I can suggest is looking into the format operator and playing with that.

http://ss64.com/ps/syntax-f-operator.html

August 26th, 2015 2:33pm

I am going to make a wild guess and say this is what you are trying to ask:

$GitList = git log master..branch2 --pretty=format:'%H' --reverse
foreach ($hash in $GitList) {
    '{0} hash' -f $hash
     $files = git show $hash --pretty="format:" --name-only
    foreach($file in $files){
        '{0} file' -f $file
    }
}
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 2:52pm

Jrv, are you here to make my powershell learning a nightmere. Don't answer that because you will only promote negetivity. It worked just to let you know what i was trying to do. 

You are misunderstanding my suggestion.  There is a new PowerShell CmdLet set that does all of this for you and returns objects and not strings.  The commandline utility "git" can only return strings and is much harder to use and integrate.

There was no negativity only a suggestion that the newer GIT modules would be more useful.

You should start issues about GIT in the git forums as the members their are more familiar with GIT.  This forum is really about simple scripting issues

August 26th, 2015 2:58pm

Thanks Jrv, but it was more a powershell issue of just adding a comment after a foreach loop, the ticket is now resolved. As i am learning git/powershell i am trying to understand the both at once, and my task is to use only plain git so i can't use the module as of yet. 

But i will use git forums. I also need help with the same script structure to output only files in a 1 dimensional array, can you point me in the right direction for learning one dimensional arrays. More along the lines of populating a variable into an array. 

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 3:26pm

This creates an array:

$GitList = git log master..branch2 --pretty=format:'%H' --reverse
$array=foreach ($hash in $GitList) {
     $files = git show $hash --pretty="format:" --name-only
    foreach($file in $files){
         @($hash,$file)
    }
}

$array





also:  help array

August 26th, 2015 4:15pm

Thanks, but i want to see how i can show only name of files in a one dimensional array without the hash. 

I've read using an operator "+=" and also you have to declare NewArray=@() before a foreach loop right? 

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 4:26pm

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

$GitList is an array of names.

$GitList = git log master..branch2 --pretty=format:'%H' --reverse
$afiles=@()
foreach ($hash in $GitList) {
     $files = git show $hash --pretty="format:" --name-only
    foreach($file in $files){
         $afiles+=$file
    }
}
$afiles

help array

August 26th, 2015 5:19pm

But what I've read about dynamic arrays and the operator += also array@() How would you use the the above to display filenames I know it's in the $gitlist variable Eventually I want to add more to the script so I can get into multi diamentional arrays. But I think it's best to start with a 1-diamentional array to get filenames. Just going round in circles as I want to display filenames only through the dynamic array I know I have to store the filenames in array and then print them out but scripting this is driving me crazy in circles
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 5:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics