简体   繁体   中英

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

Our merge commit messages look like this (GitHub creates this format automatically):

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:

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

* 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? What would you do to get the same coloring and pagination (via $PAGER ; less in my case) that plain git log does?


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.

What would you do to get the same coloring and pagination (via $PAGER ; less in my case) that plain git log does?

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). 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. To get the pager to run, use git var GIT_PAGER to find which pager to run, then run it.

I think this should work:

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

Just no colors here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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