├── .gitmessage ├── hooks ├── prepare-commit-msg_v2 └── prepare-commit-msg └── README.md /.gitmessage: -------------------------------------------------------------------------------- 1 | #issueType: [#issueNumber] 2 | 3 | #jiraIssueURL 4 | -------------------------------------------------------------------------------- /hooks/prepare-commit-msg_v2: -------------------------------------------------------------------------------- 1 | COMMIT_MSG_FILE=$1 2 | 3 | # find issue number 4 | issueNumber=$(git symbolic-ref HEAD | grep -oE 'JIRA-\d+' | head -1) 5 | 6 | # find types of MR 7 | issueType=$(git symbolic-ref HEAD | grep -oE 'feat|fix|docs' | head -1) 8 | 9 | # set URL of our jira project 10 | jiraIssueURL="https://jira.example.com/browse/$issueNumber" 11 | 12 | # check the variables and set default values 13 | 14 | # if no issue number is found, set jiraIssueURL to empty 15 | if [ -z "$issueNumber" ]; then 16 | jiraIssueURL='' 17 | fi 18 | 19 | # by default issue type should be 'feat' as the most common type 20 | issueType=${issueType:-feat} 21 | 22 | # set the issue number to 'JIRA-0000' to follow the commit message format if not found 23 | issueNumber=${issueNumber:-JIRA-0000} 24 | 25 | # form the commit message 26 | 27 | if [ -n "$issueNumber" ]; then 28 | cat << EOF > "$COMMIT_MSG_FILE" 29 | $issueType: [$issueNumber] $(cat $COMMIT_MSG_FILE) 30 | 31 | $jiraIssueURL 32 | EOF 33 | fi 34 | -------------------------------------------------------------------------------- /hooks/prepare-commit-msg: -------------------------------------------------------------------------------- 1 | # gets digits in current branch name. 2 | # `head -1` gets just the first group. 3 | # Note that this assumes you are adding your git issue numbers to your branches. 4 | 5 | issueNumber=$(git symbolic-ref HEAD | grep -oE 'JIRA-\d+' | head -1) 6 | 7 | # found some types of MR 8 | issueType=$(git symbolic-ref HEAD | grep -oE 'feat|fix|docs' | head -1) 9 | 10 | # URL of our jira project 11 | jiraIssueURL="https:\/\/jira.example.com\/browse\/$issueNumber" 12 | 13 | if [ -n "$issueNumber" ]; then 14 | # replace #gitlabIssueURL with the value above 15 | sed -i '' -e "s/\#jiraIssueURL/$jiraIssueURL/" "$1" 16 | sed -i '' -e "s/\#issueNumber/$issueNumber/" "$1" 17 | 18 | else 19 | # delete the line containing #gitlabURL and set default issueNumber 20 | sed -i '' -e "/\#jiraIssueURL/d" "$1" 21 | sed -i '' -e "s/\#issueNumber/JIRA-0000/" "$1" 22 | fi 23 | 24 | if [ -n "$issueType" ]; then 25 | sed -i '' -e "s/\#issueType/$issueType/" "$1" 26 | else 27 | sed -i '' -e "s/\#issueType/feat/" "$1" 28 | fi 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What is git-template 2 | 3 | This hook creates predefined commit message when you type `git commit`. You can modify it if needed. 4 | It renders the branch name and gets the necessary parameters (such as the Jira issue and commit type) 5 | If the parameters are not available, default parameters will be used. 6 | 7 | For example, for branch name `fix/JIRA-2296_cdn`, commit message will looks like: 8 | ``` 9 | fix: [JIRA-2296] Test commit message 10 | 11 | https://jira.example.com/browse/JIRA-2296 12 | ``` 13 | 14 | For branch name, `best_change`, commit message will have default values: 15 | ``` 16 | feat: [JIRA-0000] Add the best change 17 | ``` 18 | 19 | ## How-to install 20 | 21 | Copy this repo to the preffered location (for example ~/.git-template): 22 | ``` 23 | git clone git@github.com:Nmishin/git-template.git ~/.git-template 24 | ``` 25 | 26 | After that, you need to add a couple of lines to the global git config (`~/.gitconfig` or `~/.config/git/config`): 27 | ``` 28 | [commit] 29 | template = ~/.git-template/.gitmessage 30 | [init] 31 | templateDir = ~/.git-template 32 | template = ~/.git-template/.gitmessage 33 | ``` 34 | 35 | For now, every newly cloned repo will be preconfigured with these templates. 36 | 37 | To add these templates to the existing repos, run the script: 38 | ``` 39 | #!/bin/sh 40 | 41 | set -ue 42 | 43 | # Set the path to the updated prepare-commit-msg script 44 | PREPARE_COMMIT_MSG_SCRIPT=$(git config --global init.templateDir)/hooks/prepare-commit-msg 45 | 46 | # We need to replace tilde (~) to the real HOME directory 47 | PREPARE_COMMIT_MSG_SCRIPT="${PREPARE_COMMIT_MSG_SCRIPT/#\~/$HOME}" 48 | 49 | # Find all Git repositories under the current directory 50 | repos=$(find . -name ".git" -type d -prune -exec dirname {} \;) 51 | 52 | # Loop through each repository and copy the updated script to the hooks directory 53 | for repo in $repos; do 54 | hooks_dir="$repo/.git/hooks" 55 | if [ -d "$hooks_dir" ]; then 56 | echo "Updating prepare-commit-msg hook in $repo" 57 | cp "$PREPARE_COMMIT_MSG_SCRIPT" "$hooks_dir/prepare-commit-msg" 58 | fi 59 | done 60 | ``` 61 | 62 | ## Notes 63 | Please note: this `templatedir` will override the default template directory, and if you use some templates from the default directory, you need to copy them from `/Library/Developer/CommandLineTools/usr/share/git-core/templates` (for macOS) or `/usr/share/git-core/templates` for Linux. 64 | 65 | 66 | ## Limitations 67 | This method doesn't work if you add commit messages in a non-interactive manner, such as `git comit -m "commit message"`. 68 | 69 | Only the interactive Git commit command `git commit` is supported. 70 | 71 | 72 | ## Links: 73 | - [Git Smart: Streamlining Your Workflow with the prepare-commit-msg Hook](https://dev.to/chaz8080/git-smart-streamlining-your-workflow-with-the-prepare-commit-msg-hook-432p) 74 | - [Automatically Add Issue URL to Commit Message](https://jasonmfry.wordpress.com/2019/11/13/automatically-add-issue-url-to-commit-message/) 75 | --------------------------------------------------------------------------------