There is a really nice feature of the Jira / Git integration where if you give a Git branch the same name as a Jira ticket label (e.g. SP-678), then the commit history of that branch is automatically reported in the Jira ticket. Well, it seems that the linking is automatic, but the commit reporting only works if you also remember to prefix your commit message with the branch name. This seems like repetition to me and I invariably forget.
So my question is How can I automatically add the branch name prefix to my commit messages?
Thanks very much Hugh. In your example last_commit is just the name of the file holding the message, right? (It was a revelation to see all this .git stuff in the background.)
I've modified it slightly to paste in the current_branch and rewrite the file. I hope readLines and writeLines will work in general.
#!C:/Program Files/R/R-3.5.2/bin/Rscript.exe
# current branch name
branches <- system("git branch", intern = TRUE)
current_branch <- strsplit(branches[startsWith(branches, "*")], split = " ")[[1]][2]
# commit message file name
commit_msg_file <- commandArgs(trailingOnly = TRUE)[1]
# check, ?add prefix
commit_msg <- readLines(commit_msg_file)
if (current_branch != "master" && !startsWith(commit_msg, current_branch)) {
writeLines(paste(current_branch, commit_msg), commit_msg_file)
}
# check again
commit_msg <- readLines(commit_msg_file)
if (current_branch != "master" && !startsWith(commit_msg, current_branch)) {
stop("Commit failed to have the required prefix: ", current_branch)
}