2012-02-24 3 views
0

git log出力をasciidoc形式の変更ログにフォーマットしようとしています。私はgit log --formatを使ってこれを行っています。次に、件名にコミットメッセージのバグ番号を追加する必要があります。私は、これはAwkのを使用して行うことができるかどうかを知りたい後続の行マッチパターンのときに2行を結合する

 * 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. 

:以下

入力は

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

入力例を使用して生成されます。あなたは上記を達成できるAwkコードを提供できますか?他のオプションは何ですか?私は、希望の出力を生成するシェルスクリプトを作成したいと思います。

答えて

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

0

一つの方法は、あなたの入力例は、infileのコンテンツであることが想定SEDの使用(おそらく\sGNU sedを必要と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 logからパイプを経由してストリームを処理できるというソリューションを探しています。 – DeenSeth

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' | <件名に課題を追加するための余分なロジック>>出力ファイル – DeenSeth

+0

私は提供したコード( 'perl -0777pe1's/^ \ *(?:(?!^ \ *) 。)*)\ n ^バグ:(問題\ d +)\ n/* $ 2 $ 1/msg ")。 '-p'は"入力行ごとにコードを実行し、(おそらく変更された)行を出力する "ことを意味します。 – ikegami

関連する問題