Объедините 2 строки, которые, когда последующая строка соответствует шаблону - PullRequest
0 голосов
/ 24 февраля 2012

Я пытаюсь отформатировать вывод журнала git в журнал изменений в формате asciidoc.Я сделал это, используя git log --format.Затем мне нужно добавить номер ошибки, который содержится в сообщении фиксации, в тему.

Вводимые ниже данные генерируются с использованием

      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, который можно выполнить выше?Если нет, каковы другие варианты?Я хотел бы создать сценарий оболочки, который генерирует желаемый результат.

Ответы [ 3 ]

0 голосов
/ 25 февраля 2012

Если весь файл загружен в память, это легко исправить:

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 голосов
/ 25 февраля 2012
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 голосов
/ 24 февраля 2012

Один из способов использования 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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...