WestFarmer
WestFarmer

Reputation: 765

why git log output redirect to a while loop not working?

I am trying this command in my bash shell script:

git log --oneline --no-decorate  --pretty=format:"%s" $oldrev..$newrev

git log --oneline --no-decorate  --pretty=format:"%s" $oldrev..$newrev | while read -r line; do
    echo "$line"
done

the first git log can print output, but the second one followed by while won't print anything. why ?

I invoke my script like this:( second and third argument passed to $oldrev and $newrev)

./check master a735c2f eb23992

if I add --no-pager option, both will print nothing.

I am using bash 4.4.23(1)-release on fedora 28.

Upvotes: 2

Views: 775

Answers (1)

VonC
VonC

Reputation: 1326782

Instead of pretty=format, you should use pretty=tformat:

'tformat:'

The 'tformat:' format works exactly like 'format:', except that it provides "terminator" semantics instead of "separator" semantics.

In other words, each commit has the message terminator character (usually a newline) appended, rather than a separator placed between entries.

This means that the final entry of a single-line format will be properly terminated with a new line, just as the "oneline" format does. For example:

$ git log -2 --pretty=format:%h 4da45bef \
  | perl -pe '$_ .= " -- NO NEWLINE\n" unless /\n/'
4da45be
7134973 -- NO NEWLINE

$ git log -2 --pretty=tformat:%h 4da45bef \
  | perl -pe '$_ .= " -- NO NEWLINE\n" unless /\n/'
4da45be
7134973

In addition, any unrecognized string that has a % in it is interpreted as if it has tformat: in front of it.
For example, these two are equivalent:

$ git log -2 --pretty=tformat:%h 4da45bef
$ git log -2 --pretty=%h 4da45bef

Upvotes: 2

Related Questions