Qt Creator git plugin doesn't properly utilize git hooks

  • TL;DR git's prepare-commit-msg hook doesn't work as expected in Qt Creator for two reasons. First, the GUI always assumes we want to start with a blank commit message. Second, Qt Creator uses a temporary message file and calls git commit with the -F flag, meaning no hooks are run until the commit message is already complete. Is this desired behavior?

    I have two git hooks in my repository, prepare-commit-msg and commit-msg. prepare-commit-msg acts as a way to template a commit by inserting the branch name and some other items into the commit message before the user even sees it. commit-msg is a tool I wrote to check a commit and make sure it conforms to style standards my team has put in place.

    When invoking git commit from the terminal, my hooks work as expected. First, the prepare-commit-msg hook is run, templating my commit message and writing it to the default commit message file, path/to/repo/.git/EDIT-COMMITMSG. Then, git opens the now templated path/to/repo/.git/EDIT-COMMITMSG in the default text editor. When the user is done writing their message, they save and close the file. The commit-msg hook now runs, checking the message in path/to/repo/.git/EDIT-COMMITMSG against it's rules. If successful, all is good and git logs the commit. Otherwise, it aborts.

    However, when using Qt Creator, I found that my hooks do not behave the same way. What I found is that Qt Creator doesn't use the typical git commit message (path/to/repo/.git/EDIT-COMMITMSG). Instead, what Qt Creator appears to do is pop up its git commit GUI with an empty string buffer (the Description box). When the user clicks the "Commit" button in the GUI, the data in the string buffer (the commit message) is saved to a temporary file location. Then, Qt Creator calls git commit -F path/to/temp/commit/msg. git, in the background, copies the contents of path/to/temp/commit/msg to path/to/repo/.git/EDIT-COMMITMSG because the -F flag says to take the commit message from the designated file. It is at this point that my hooks run. prepare-commit-msg prepends my template to the already populated path/to/repo/.git/EDIT-COMMITMSG. We skip opening the file with our text editor, and next the commit-msg hook is run. My default template does not pass the check, and the commit is aborted.

    Is this the desired behavior? The takeaway here is that prepare-commit-msg is pretty much useless in Qt Creator if you want to edit your commit message before the user touches it. I'm getting around it by using git's template feature, but my template.txt can't populate its contents on-the-fly like a shell or Python script can. I could handle styling the commit message after the user has submitted the message, but I don't want to silently edit their message and confuse them down the road.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I’d say it’s likely not. I’d recommend bringing this excellent question to the Qt Creator mailing list. You’ll find there Qt Creator’s developers/maintainers. This forum is more user oriented.

Log in to reply