2012-02-24 17 views
0

我想格式git日誌輸出到asciidoc格式的更改日誌。我已經使用git log --format做到了這一點。接下來,我需要在提交消息中添加一個bug編號到主題中。結合2行,當後續行匹配模式

下面的輸入是使用

 git log --reverse --no-merges $1..$2 --format='* %s%n+%n%b' | \ 
     sed -e '/^Change-Id:.*$/d' | sed -e '/^Signed-off-by:.*$/d' 

輸入示例生成:

 * This is subject without issue number 
     + 
     There will be multiple lines of text and multiple paragraphs. 

     2nd paragraph of the commit message. 


     * This is commit with issue number 
     + 
     There can be multiple lines of comment message. 

     2nd paragraph of the commit message. A line with Bug: issue ### 
     will be the last line. I need to combine the issue ### with 
     the subject line. 

     Bug: issue 1234 

     * This is commit with issue number in Issue: 1235 format 
     + 
     There can be multiple lines of comment message. 

     2nd paragraph of the commit message. A line with Issue: ### 
     will be the last line. I need to combine the issue ### with 
     the subject line. 

     Issue: 1235 

期望輸出

 * This is subject without issue number 
     + 
     There will be multiple lines of text and multiple paragraphs. 

     2nd paragraph of the commit message. 


     * issue 1234 This is commit with issue number 
     + 
     There can be multiple lines of comment message. 

     2nd paragraph of the commit message. A line with Bug: issue ### 
     will be the last line. I need to combine the issue ### with 
     the subject line. 

     * issue 1235 This is commit with issue number in Issue: 1235 format 
     + 
     There can be multiple lines of comment message. 

     2nd paragraph of the commit message. A line with Issue: ### 
     will be the last line. I need to combine the issue ### with 
     the subject line. 

我想知道是否這可以使用awk中來完成。你能提供可以完成的Awk代碼嗎?如果不是什麼其他選擇?我想創建一個生成所需輸出的shell腳本。

回答

0
awk ' 
    $1 == "*" { 
     if (entry) { 
      print subject 
      print entry 
      entry = "" 
     } 
     subject = $0 
     next 
    } 
    $1 == "Bug:" { 
     sub(/\*/, "* issue " $NF, subject) 
     next 
    } 
    {entry = entry "\n" $0} 
    END {print subject; print entry} 
' 
+0

謝謝!這是一個非常好的解決方案。當我將第一部分傳遞給它時,它會起作用。但是,該腳本會在每個主題行下面生成額外的空白行。我可以添加另一個條件,一個錯誤信息可以在行Issue:###中描述。我發佈後我才發現這個案例。 – DeenSeth 2012-02-25 10:20:54

0

一種方法使用Sed則假設您輸入的例子是infile內容(可能需要GNU sed因爲\s與如果sed抱怨文字空間更改。):

sed -n '/^\s*bug:/I ! { H ; b }; s/^[^:]*:// ; G ; s/\([^\n]*\)\n\(.*\*\)/\2\1/ ; s/^\n// ; p' infile 

輸出:

* This is subject without issue number 
+ 
There will be multiple lines of text and multiple paragraphs. 

2nd paragraph of the commit message. 


* issue 1234 This is commit with issue number 
+ 
There can be multiple lines of comment message. 

2nd paragraph of the commit message. A line with Bug: issue ### 
will be the last line. I need to combine the issue ### with 
the subject line. 

說明:

-n       # Disable printing. 
/^\s*bug:/I ! {    # If line doesn't begin with 'bug' ignoring case. 
    H       # Append line to 'hold space'. 
    b       # Read next line. 
} 
s/^[^:]*://     # Line beginning with 'bug': Remove part of line until a colon. 
G       # Get data of 'hold space'. 
s/\([^\n]*\)\n\(.*\*\)/\2\1/ # Put bug line just before the commit message. 
s/^\n//      # Remove leading newline. 
p       # Print. 
+0

非常感謝你。這是很了不起的感謝解釋。這項工作很好,但它需要一箇中間文件。我正在尋找解決方案,我可以通過管道處理來自git日誌的流。 – DeenSeth 2012-02-25 10:19:07

0

如果整個文件加載到內存中,這是很容易解決:

s/^\*((?:(?!^\*).)*)\n^Bug: (issue \d+)\n/* $2$1/msg; 

     ^^^^^^^^^^^^^ 
     Matches anything but "*" at the start of a line. 

而這正是這麼做的:

perl -0777pe1's/^\*((?:(?!^\*).)*)\n^Bug: (issue \d+)\n/* $2$1/msg' 
+0

我對Perl不熟悉。我將如何在腳本中使用這一行?我希望能做一些像git log --reverse --no-merges $ 1 .. $ 2 --format ='*%s%n +%n%b'| sed -e'/^Change-Id:.*$/d'| sed -e'/^Signed-off-by:.*$/d'| <額外的邏輯添加問題到主題>> outputfile – DeenSeth 2012-02-25 09:53:28

+0

只需用我提供的代碼('perl -0777pe1's/^ \ *((?:(?!^ \ *))替換' 。)*)\ n ^錯誤:(issue \ d +)\ n/* $ 2 $ 1/msg'')。 '-p'表示「爲每一行輸入執行代碼,然後打印出(可能改變的)行。」 – ikegami 2012-02-25 20:34:49