Git Hooks

Abstract

This is a brief explanation of how to use the git hooks included in this directory.

Git Hooks#

Makes use of the unmaintained GitPython package.

prepare-commit-msg#

A small Python script to produce commit messages.

Prepare Commit Message

This hook is invoked by git-commit[1] right after preparing the default log message, and before the editor is started.

It takes one to three parameters. The first is the name of the file that contains the commit log message. The second is the source of the commit message, and can be: message (if a -m or -F option was given); template (if a -t option was given or the configuration option commit.template is set); merge (if the commit is a merge or a .git/MERGE_MSG file exists); squash (if a .git/SQUASH_MSG file exists); or commit, followed by a commit object name (if a -c, -C or --amend option was given).

If the exit status is non-zero, git commit will abort.

The purpose of the hook is to edit the message file in place, and it is not suppressed by the --no-verify option. A non-zero exit means a failure of the hook and aborts the commit. It should not be used as a replacement for the pre-commit hook.

The sample prepare-commit-msg hook that comes with Git removes the help message found in the commented portion of the commit template.

prepare_commit_msg.get_git_branch() str

Return the currently checked out branch.

Variables:

ret_value (str) – Value to be returned, or the branch of the current commit decoded and stripped of whitespace.

Return ret_value:

The branch of the current commit.

prepare_commit_msg.get_git_username() int | float | str

Return the value of the git username from the configuration.

prepare_commit_msg.get_issue_message(branch: str) str | TypeError

Return the issue number based on the branch name.

Parameters:

branch (str) – Branch we can extract an issue from

prepare_commit_msg.get_issue_number(branch: str) str | TypeError

Return the issue number based on the branch name.

Parameters:

branch (str) – Branch we can extract an issue from

prepare_commit_msg.get_jira_ticket(branch: str) str | TypeError

Return the issue number based on the branch name.

Parameters:

branch (str) – Branch we can extract an issue from

prepare_commit_msg.parse_branch_name(branch: str) dict

Use regex to pull the issue number from the branch name.

Parameters:

branch (str) – A git branch containing an issue or jira_ticket example: 46-inf-197-add-docs-for-updated-files

prepare_commit_msg.prepare_message(branch_name: str, parsed_branch: dict, git_username: str)

Prepare a commit message.

prepare_commit_msg.write_message()

Write the prepared commit message.