简体   繁体   English

`git log --pretty=format` 可以通过正则表达式转换 output 吗?

[英]Can `git log --pretty=format` transform output via regex?

Our merge commit messages look like this (GitHub creates this format automatically):我们的合并提交消息如下所示(GitHub 自动创建此格式):

Merge pull request #123 from repo/branch-a

Some change (title of the PR)

I can use --pretty=format:"%s: %b" ( %s is "subject", %b is "body") to get an output like this:我可以使用--pretty=format:"%s: %b"%s是“主题”, %b是“主体”)来获得这样的 output :

* Merge pull request #123 from repo/branch-a: Some change
* Merge pull request #456 from repo/branch-b: Another change

I'd like to transform the output to this:我想将 output 转换为:

* PR #123: Some change
* PR #456: Another change

Can this be done via --pretty alone or do I need to pipe it to another program that will make the transformation?这可以通过--pretty单独完成,还是我需要将其 pipe 到另一个将进行转换的程序? What would you do to get the same coloring and pagination (via $PAGER ; less in my case) that plain git log does?你会怎么做才能获得与普通git log相同的着色和分页(通过$PAGER ;在我的情况下less )?


UPDATE : the full command I run is this:更新:我运行的完整命令是这样的:

git log --color --graph --pretty=format:\"%Cred%h%Creset -%C(yellow)%d%Creset %b: %s %Cgreen(%cr) %C(bold blue)<%an>%Creset\" --abbrev-commit --merges --first-parent

The short answer is no: --pretty has a lot of format directives, but none of them allow regex substitution.简短的回答是否定的: --pretty有很多格式指令,但没有一个允许正则表达式替换。

What would you do to get the same coloring and pagination (via $PAGER ; less in my case) that plain git log does?你会怎么做才能获得与普通 git 日志相同的着色和分页(通过$PAGER ;在我的情况下less )?

Plain git log , with no options at all, does some color switching you can't quite get with any pretty format (I consider this a very minor bug).普通git log ,根本没有任何选项,会进行一些颜色切换,这是任何漂亮格式都无法做到的(我认为这是一个非常小的错误)。 The main thing to get color controls out of git log when piping git log output (so as to do regex work or whatever) is to use the --color=always switch.当管道git log output (以便进行正则表达式工作或其他)时,从git log中获取颜色控制的主要方法是使用--color=always开关。 To get the pager to run, use git var GIT_PAGER to find which pager to run, then run it.要让寻呼机运行,请使用git var GIT_PAGER查找要运行的寻呼机,然后运行它。

I think this should work:我认为这应该有效:

YOURCOMMAND | sed 's/Merge pull request \#\([1-9]\).*\:\(.*\)/PR #\1: \2/g'

Just no colors here只是没有 colors 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM