我正在创建一个简单的Powershell脚本,用于从git获取提交并从它们创建变更量,但我遇到了一个障碍。现在提交被浓缩成一行,但是我无法在它们后面添加一个"newline“,这样就可以使用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发布于 2018-11-26 10:23:45
首先,$commits是一个对象数组,因此我建议调整第一个if语句如下:
if ($commits -ne $null -and $commits.count -gt 0) {下面的if语句也是如此。现在来谈谈你的问题。您正在错误地处理git日志命令的输出.如前所述,它返回一个对象数组。而不是将整个数组添加到文件中,而是按如下方式迭代数组。
$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"
}
}https://stackoverflow.com/questions/53478120
复制相似问题