I have used subprocess.check_output
to get the result of Git command but I can't think of a way how to update the commit message that we do with the command git commit -amend
?
You can use Python as the editor, so amending uses GIT_EDITOR
which can be your own Python command to manipulate text, this can even be a call to Python containing a code-snippet, e.g.
import os
import subprocess
# Add "Some Appended Text!" after the commit message.
subprocess.check_call(
("git", "commit", "--amend"),
env={
**os.environ,
"GIT_EDITOR": "".join([
"""'%s' -c "import sys\n""" % sys.executable,
"""file = sys.argv[-1]\n""",
"""with open(file, 'r', encoding='utf-8') as fh: data = fh.read()\n""",
"""data = '\\n'.join([l for l in data.split('\\n') if not l.startswith('#')])\n""",
"""data = data.rstrip() + '\\n\\nRef: !%d\\n'\n""" % args.pr,
"""with open(file, 'w', encoding='utf-8') as fh: fh.write(data)\n" - """,
]),
}
)