0

I am creating a simple Powershell-script for fetching commits from git and creating a changelog from them, but I've hit a snag. Right now the commits are condensed to fit in one line, but I am not able to add a "newline" behind them, so that they can be shown in a list using MarkDown.

This is what I have so far (updated):

# Getting project location
$location = Get-Location
Write-Host $location

# Getting version number from project
$currentVersion = (Select-String -Path .\package.json -Pattern '"version": "(.*)"').Matches.Groups[1].Value
Write-Host $currentVersion

#Adding header to log file if there are any commits marked with current version number
$commits = git log --grep="ver($currentVersion)"
if (![string]::IsNullOrEmpty($commits)) {
    Add-Content "$location\log.md" "### All changes for version $currentVersion ###`n"

    # Fetching commits based on version number and tags
    $fixed = git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --grep="ver($currentVersion) --grep="(fixed)"
    if (![string]::IsNullOrEmpty($fixed)) {
        Add-Content "$location\log.md" "## Fixed ##`n"
        Add-Content "$location\log.md" "$fixed`n`n"
    }

    $removed = git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --grep="ver($currentVersion) --grep="(breaking)"
    if (![string]::IsNullOrEmpty($removed)) {
        Add-Content "$location\log.md" "## Removed ##`n"
        Add-Content "$location\log.md" "$removed`n`n"
    }

    $added = git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --grep="ver($currentVersion) --grep="(added)"
    if (![string]::IsNullOrEmpty($added)) {
        Add-Content "$location\log.md" "## Added ##`n"
        Add-Content "$location\log.md" "$added`n`n"
    }   
}

#Asking user for new version number
$newVersion = Read-Host "Choose new version number - current version is $currentVersion"

#Running npm-version to update project version number
npm version $newVersion

1 Answer 1

1

First of all $commits is an object array, so I suggest to adjust the first if statement as follows:

if ($commits -ne $null -and $commits.count -gt 0) {

The same applies to the following if statements. Now to your problem. You are treating the output of the git log command incorrectly... as already pointed out it returns an object array. Instead of adding the whole array to the file iterate over the array as follows.

$fixed = git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --grep="ver($currentVersion) --grep="(fixed)"
if ($fixed -ne $null -and $fixed.count -gt 0) {
    Add-Content "$location\log.md" "## Fixed ##`n"
    foreach ($f in $fixed)
    {
        Add-Content "$location\log.md" "$f`n`n"
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - I was under the impression that it was a string, my bad I guess. It is working great now! :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.