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)
}