-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre-commit
More file actions
65 lines (55 loc) · 2.71 KB
/
Copy pathpre-commit
File metadata and controls
65 lines (55 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env Rscript
# License: CC0 (just be nice and point others to where you got this)
# Author: Robert M Flight <rflight79@gmail.com>, github.com/rmflight
#
# This is a pre-commit hook that checks that there are files to be committed, and if there are, increments the package version
# in the DESCRIPTION file.
#
# To install it, simply copy this into the ".git/hooks/pre-commit" file of your git repo, change /path/2/Rscript, and make it
# executable. Note that /path/2/Rscript is the same as your /path/2/R/bin/R, or may be in /usr/bin/Rscript depending on your
# installation. This has been tested on both Linux and Windows installations.
#
# In instances where you do NOT want the version incremented, add the environment variable doIncrement=FALSE to your git call.
# eg "doIncrement=FALSE git commit -m "commit message"".
# This is useful when you change the major version number for example.
doIncrement <- TRUE # default
# get the environment variable and modify if necessary
tmpEnv <- as.logical(Sys.getenv("doIncrement"))
if (!is.na(tmpEnv)){
doIncrement <- tmpEnv
}
# check that there are files that will be committed, don't want to increment version if there won't be a commit
fileDiff <- system("git diff HEAD --name-only", intern=TRUE)
DESC <- grep("DESCRIPTION",list.files(".", recursive = TRUE),value = TRUE)
if (length(DESC) && DESC %in% fileDiff) {
# Don't want to overwrite manual version bump
desc_diff <- system(paste("git diff HEAD", DESC), intern = TRUE)
doIncrement <- !any(grepl("\\+Version", desc_diff))
staged_files <- system("git diff HEAD --name-only --staged", intern = TRUE)
desc_staged <- DESC %in% staged_files
if (!desc_staged) {
if (doIncrement) cat("DESCRIPTION had additional changes that were committed.\n")
else cat("DESCRIPTION contains manual version bump but was not staged, so it was not committed.\n")
} else if(!doIncrement){
cat("Manual version set, so no bumping is done.\n")
}
}
if ((length(fileDiff) > 0) && length(DESC) && doIncrement){
currDCF <- read.dcf(DESC)
currVersion <- currDCF[1,"Version"]
splitVersion <- strsplit(currVersion, "-", fixed=TRUE)[[1]]
nVer <- length(splitVersion)
if(nVer < 2){
cat("No devel version (needs -). Version not updated.\n")
} else {
currEndVersion <- as.integer(trimws(splitVersion[nVer]))
newEndVersion <- as.character(currEndVersion + 1)
if(!is.na(currEndVersion)) splitVersion[nVer] <- newEndVersion
newVersion <- paste(splitVersion, collapse="-")
currDCF[1,"Version"] <- newVersion
currDCF[1, "Date"] <- strftime(as.POSIXlt(Sys.Date()), "%Y-%m-%d")
write.dcf(currDCF, DESC)
system(paste("git add", DESC))
cat("Incremented package version and added to commit!\n")
}
}