Lars Rødal
Lars Rødal

Reputation: 876

Powershell script for creating git changelog

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

Upvotes: 0

Views: 959

Answers (1)

rufer7
rufer7

Reputation: 4109

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"
    }
}

Upvotes: 1

Related Questions