-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-lint-commit.R
57 lines (57 loc) · 1.81 KB
/
git-lint-commit.R
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
#' Common Lints for Commit Messages
#'
#' Lint a commit message for typical commit style and
#' best practices for git.
#'
#' @name lint
#' @param x A single commit message from [get_commit_msgs()].
#' @return Integer. Invisibly returns the number of detected lints
#' in the message.
#' @export
lint_commit_msg <- function(x) {
if ( length(x) == 0L ) {
return(0)
}
sha <- attr(x, "sha") %||% "\u2716"
cnt <- 0L
if ( grepl("^[a-z]", x[1L]) ) {
warning("Title should begin with a capital (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
if ( grepl("\\.$", x[1L]) ) {
warning("Title should not end with a period (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
if ( nchar(x[1L]) > 60L ) {
warning("Title is too long (", nchar(x[1L]), ") for (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
if ( isTRUE(x[2L] != "") ) {
warning("Title should be followed by empty line (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
star <- vapply(x, grepl, pattern = "^\\*", NA)
if ( any(star) ) {
warning("Please use `-` for bullets (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
bullet_space <- vapply(x, grepl, pattern = "^-[a-zA-Z]", NA)
if ( any(bullet_space) ) {
warning("Please place space following `-` bullet (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
wip <- vapply(x, grepl, pattern = "WIP", ignore.case = TRUE, NA)
if ( any(wip) ) {
warning("Work in progress commit detected! (", sha, ")", call. = FALSE)
cnt <- cnt + 1L
}
y <- paste(x, collapse = "\n")
jira <- grepl("[A-Z]{2,10}-[0-9]{1,4}", y)
gh <- grepl("fixes .*#[0-9]{1,4}|closes .*#[0-9]{1,4}", y, ignore.case = TRUE)
if ( !(jira || gh) ) {
warning("Could not find an issue number in commit message (", sha, ")",
call. = FALSE)
cnt <- cnt + 1L
}
invisible(sum(cnt))
}