Skip to content

Remove preceding space when empty variables used in message templates

Problem to solve

When trying to use variables in message templates, there are times when those variables may be empty. In this case, it is desirable for minor adjustments to whitespace to be performed when applying the template, to take into account variables that were empty. This is already acknowledged and has some default behaviour to assist, as per the docs at https://docs.gitlab.com/18.2/user/project/merge_requests/commit_templates/#supported-variables-in-commit-templates:

"Any line containing only an empty variable is removed. If the removed line is both preceded and followed by an empty line, the preceding empty line is also removed."

This means the following message template:

MR%{local_reference}

%{issues}

%{title}

Will be resolved as this if the "issues" variable is not empty:

MR!1234

Closes #5678

Text from title variable

While it will be resolved as this if the "issues" variable is empty:

MR!1234

Text from title variable

This is desirable and useful, however this behaviour has only considered multi-line scenarios. It is also common and desirable to be able to use variables in a single-line format like this:

MR%{local_reference} %{issues} - %{title}

In this case, GitLab will currently resolve in this manner if the "issues" variable is not empty:

MR!1234 Closes #5678 - Text from title variable

While it will be resolved as this if the "issues" variable is empty:

MR!1234  - Text from title variable

Note the presence of two space characters in the leading MR!1234 - section.

There is currently no way to overcome this. If one of the two spaces is removed, it will result in a lack of separating whitespace when the "issues" variable is not empty, while leaving them in place will result in two spaces when it is empty.

Intended users

User experience goal

The goal of this feature is to make default templates able to produce the desired message in more cases. This will help reduce the need to manually edit as many messages to fix whitespace issues, which can currently lead to more "busy" work by users, and a messier history when corrections are not made.

Proposal

I would suggest that the current whitespace handling is incomplete functionality. I would propose that in the same manner as an empty variable preceded by and followed by a blank line results in the preceding blank line being removed, that an empty variable preceded and followed by a space should result in the preceding space being removed. That functionality would give this result with an empty "issues" variable: MR!1234 - Text from title variable Which would make whitespace adjustment more consistent between single-line and multi-line scenarios, making default templates more useful, as they are more likely to be accurate without manual adjustment on a case-by-case basis.

Edited by 🤖 GitLab Bot 🤖