Скрипт Powershell для создания git changelog - PullRequest
0 голосов
/ 26 ноября 2018

Я создаю простой скрипт Powershell для извлечения коммитов из git и создания из них журнала изменений, но я столкнулся с проблемой.Сейчас коммиты сжаты, чтобы поместиться в одну строку, но я не могу добавить «новую строку» за ними, чтобы они могли отображаться в списке с помощью MarkDown.

Это то, что у меня такдалеко (обновлено):

# 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 Ответ

0 голосов
/ 26 ноября 2018

Прежде всего $commits - это массив объектов, поэтому я предлагаю настроить первый оператор if следующим образом:

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

То же самое относится к следующим операторам if.Теперь к вашей проблеме.Вы неправильно обрабатываете вывод команды git log ... как уже указывалось, она возвращает массив объектов.Вместо добавления всего массива в файл выполните итерацию по массиву следующим образом.

$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"
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...