I would prefer running this through the parser that RevCommit uses when parsing footer lines.  Unfortunately that code is part of RevCommit.  So we would need to pull it out into a utility class, or find a way to construct a RevCommit from the buffered message so we can reuse that parser.  But even if we don't do that, you can search backwards from the end of the string to find the paragraph break that denotes the "footer" and then insert there.  But at that point, we're mostly replicating that parsing code.

In particular one of the rules we do in the shell hook is, we try to insert the Change-Id line after the Bug line, if one is present, but before any other lines.  That's hard to do with a simple regex replaceAll like you are doing here to insert the item into the string.