簡體   English   中英

使用sed / awk / grep格式化git log輸出

[英]Formatting git log output with sed/awk/grep

摘要/''gist of'版本,

如果我有一組主題[SUB]和正文[BODY]的消息如下, 如果[BODY]存在 ,如何在主題后面添加換行符(並用*替換占位符)

[SUB] some subject. [BODY] some body lines 
with newline chars and !@@# bunch of other *#@ chars
 without [(BODY)] or [(SUB)]... and more stuff
[SUB] Another subject. with no body [BODY] 
[SUB] another [BODY] some body.

我想要將其格式化為

* some subject.

some body lines 
with newline chars and !@@# bunch of other *#@ chars
 without [(BODY)] or [(SUB)]... and more stuff
* Another subject. with no body 
* another 

some body.

我真的想做什么

所以我試圖從git log輸出中自動生成我的CHANGELOG.md文件。 問題是,只有在提交消息的主體非空時才需要添加換行符。

當前代碼如下所示(分為兩行)

git log v0.1.0..v0.1.2 --no-merges --pretty=format:'* %s -- %cn | \
[%h](http://github.com/../../commit/%H) %n%b' | grep -v Minor | grep . >> CHANGELOG.md

和樣本輸出,

* Added run information display (0.1.2) -- ... | [f9b1f6c](http://github.com/../../commit/...) 
+ Added runs page to show a list of all the runs and run inforation, include sorting and global filtering.
+ Updated run information display panel on the run-info page
+ Changed the links and their names around.

* Update README.md -- abc | [2a90998](http://github.com/../../commit/...) 

* Update README.md -- xt | [00369bd](http://github.com/../../commit/...) 

你在這里看到,以*開頭的行是提交,從+開始的行只是第一次提交的主體的一部分。 現在它在所有正文部分前面添加%n (換行符),無論它是否為空。 如果它非空(我甚至可能在刪除空格后),我想添加它

我怎么做到這一點? 我對sedawk了解幾乎不存在,並且嘗試學習並沒有多大幫助。

(我將確保正文中的所有代碼都縮進,因此它不會混淆提交列表與正文中的列表)


我的答案

我確定jthills的答案是正確的(甚至可能是更好的方式),但是當我想弄明白他的意思時,我想到了這個。 希望它能幫助自己或未來的某個人,

我正在粘貼我使用的完整shell腳本,

mv CHANGELOG.md CHANGELOG.md.temp
printf '### Version '$1' \n\n' > CHANGELOG.md
git log $2..$1 --no-merges --pretty=format:'[SUB]%s -- %cn | \
    [%h](http://github.com/<user>/<gitrepo>/commit/%H) [BODY]%b' | grep -v Minor | \
    sed '{:q;N;s/\s*\[BODY\][\n\s]*\[SUB\]/\n\[SUB\]/;b q}' | \
    sed 's/\[SUB\]/* /g' | 
    sed 's/\[BODY\]/\n\n/'>> CHANGELOG.md
cat CHANGELOG.md.temp >> CHANGELOG.md
rm CHANGELOG.md.temp

我基本上使用臨時文件將新的提交日志添加到CHANGELOG.md。 請隨意為這3個sed命令建議更短的版本

git log輸出中標記語法。 這將處理正確插入換行符,其余的你知道:

git log --pretty=tformat:'%s%xFF%x01%b%xFF%x02' \
| sed '1h;1!H;$!d;g              # buffer it all (see comments for details)
       s/\xFF\x01\xff\x02//g     # strip null bodies
       s/\xFF\x01/\n/g           # insert extra newline before the rest
       s/\xFF.//g                # cleanup
'

編輯:引用/逃脫錯別字)

對於您問題中的第一個文件,您可以嘗試以下操作:

awk -f r.awk input.txt 

其中input.txt是輸入文件, r.awk是:

{
    line=line $0 ORS
}

END {
    while (getSub()) {
        getBody()
        print "* " subj
        if (body) {
            print ""
            print body
        }
    }
}

function getBody(ind) {
    ind=index(line,"[SUB]")
    if (ind) {
        body=substr(line,1,ind-1)
        line=substr(line,ind)
    }
    else
        body=line
    sub(/^[[:space:]]*/,"",body)
    sub(/[[:space:]]*$/,"",body)
}

function getSub(ind,ind2) {
    ind=index(line,"[SUB]")
    if (ind) {
        ind=ind+5
        ind2=index(line,"[BODY]")
        subj=substr(line, ind, ind2-ind)
        line=substr(line,ind2+6)
        return 1
    }
    else
        return 0
}

給出輸出:

*  some subject. 

some body lines 
with newline chars and !@@# bunch of other *#@ chars
 without [(BODY)] or [(SUB)]... and more stuff
*  Another subject. with no body 
*  another 

some body.

我用這種方式比預期的更長時間摔跤,只是試圖通過git消息的一些sed調整得到一個git log輸出來格式化/提取我們的JIRA消息。 這是我的解決方案:

logsheet = "!f() { git log --format='%h ^ %<(80,trunc)%s ^ A:%<(20,trunc)%an ^ D:%ad ' --no-merges --date=short $1 | sed -e 's/\\\\([AZ]*-[0-9]*\\\\)/\\\\1 ^/'; }; f"

逃脫,外殼功能搭配! 都需要因為我有一個arg和一個管道。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM