Home » Git » powershell select-string preserve formatting of git log

powershell select-string preserve formatting of git log

Posted by: admin November 22, 2021 Leave a comment

Questions:

Is it possible to preserve formatting (specifically colour) of a string when piped through select-string?

I’m using select-string to find entries with specific tags in a git log that’s been pretty-formatted with some nice colours. Unfortunately the colour gets lost in the process, and I get something that looks like non-printing control characters scattered about. This happens even on the “line” property of the match-info object.

I’ve piped the output of the git log into Get-Member and it appears to be a vanilla System.String.

Extra-details:
(probably not important, but you can never tell)

powershell v4

git 1.9.5

The formatted git log command is:

git log --color --graph --pretty=format:'%C(cyan)%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

I am vaguely aware that I may be able to do something similar with git log switches, and I’m looking into this, but for this question, assume I still need to pipe the output through powershell commands keeping the colours.

Edit:

Mangled output looks something like this (slightly sanitized). Unfortunately the unicode chars didn’t survive emailing off the system in question.

* [0123abc[m -[33m (tag: build-1.0.3730)[m Some Comment [32m(5 hours ago) [1;34m<Author Name>[m
* [0123xyz[m -[33m[m Some other comment [32m(6 hours ago) [1;34m<Author Name>[m

According to www.babelstone.co.uk/Unicode/whatisit.html there is a control (escape) character in the output with codepoint U+001B, immediately preceding each left square bracket in the previous example, however these were removed by email clients.

Answers: