首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于创建git changelog的Powershell脚本

用于创建git changelog的Powershell脚本
EN

Stack Overflow用户
提问于 2018-11-26 09:30:19
回答 1查看 651关注 0票数 0

我正在创建一个简单的Powershell脚本,用于从git获取提交并从它们创建变更量,但我遇到了一个障碍。现在提交被浓缩成一行,但是我无法在它们后面添加一个"newline“,这样就可以使用MarkDown在列表中显示它们。

这是我到目前为止(更新)的内容:

代码语言:javascript
复制
# 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-26 10:23:45

首先,$commits是一个对象数组,因此我建议调整第一个if语句如下:

代码语言:javascript
复制
if ($commits -ne $null -and $commits.count -gt 0) {

下面的if语句也是如此。现在来谈谈你的问题。您正在错误地处理git日志命令的输出.如前所述,它返回一个对象数组。而不是将整个数组添加到文件中,而是按如下方式迭代数组。

代码语言:javascript
复制
$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"
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53478120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档