Richard Fearn
Richard Fearn

Reputation: 25491

Make Gerrit always reject a commit

While working on a new feature, I often commit changes that aren't part of the feature itself, but are useful at the time (for example, small changes to make my work easier to test).

The commit-msg hook automatically adds a Change-Id to every new commit, including my temporary ones. This leaves me in a state where I could accidentally push my branch to Gerrit, including the temporary commits.

Is there a way to 'taint' a commit so that attempting to push it (accidentally) to Gerrit will always fail?

Upvotes: 0

Views: 302

Answers (1)

Richard Fearn
Richard Fearn

Reputation: 25491

If git commit --amend is used to change the commit message, and the Change-Id line is removed, the commit-msg hook will add it back again.

Instead a Change-Id line is needed that:

  • The commit-msg hook will treat as valid, so that it doesn't attempt to replace it or add another.
  • Gerrit will reject.

The commit-msg hook looks for a line matching the regex ^Change-Id: so as long as those characters are present (up to and including the colon) a new Change-Id won't be added.

This commit can't be pushed to Gerrit.

Change-Id:

Gerrit firstly requires a Change-Id to match the regex ^I[0-9a-f]{8,}.*$, so anything that fails to match this will be rejected by Gerrit with the message missing or invalid Change-Id line format in commit message footer:

Change-Id: blah
Change-Id: I000
Change-Id: I1234567

For new changes, Gerrit subsequently requires a Change-Id to match ^I[0-9a-fA-F]{40}$ so in fact anything shorter than 40 characters will be rejected with the message invalid Change-Id:

Change-Id: I12345678
Change-Id: I012345678901234567890123456789012345678

Upvotes: 1

Related Questions