Squash/Merge commit template: avoid breaking git trailers

In Squash Commit Template and Merge Commit Template we've added a feature where an empty variable will be remove along will all new line characters preceding it.

This feature can break conventions and Git Trailers. In a Git message there should be a blank line after title, and Git trailers need to be separated by a blank line from the rest of the commit.

For example, in this template:

%{title}

%{co_authored_by}
MR: %{url}
Merged-By: %{merged_by}

if %{co_authored_by} is empty, we will also remove the blank line, breaking the rest of Git trailers and convention of having empty line after title.

Related problem: #349621 (identified in #348263 (comment 801839586)).

Proposal

When the variable is empty and the only thing in a line:

  • If it has more than one newline character following it (or EOF), remove all newline characters preceding it.
  • Otherwise, remove only one newline character.

So, assuming %{co_authored_by} is empty, this:

%{title}

%{co_authored_by}
MR: %{url}
Merged-By: %{merged_by}

becomes:

%{title}

MR: %{url}
Merged-By: %{merged_by}

but this template:

%{title}


%{co_authored_by}

MR: %{url}
Merged-By: %{merged_by}

becomes:

%{title}

MR: %{url}
Merged-By: %{merged_by}
Edited by Piotr Stankowski