Skip to content

Commit 06ee33e

Browse files
committed
Minor tweak to trim_sha() and export is_sha()
- now returns 'sha' if not a sha or empty string - new function `is_sha()` - added new unit tests - fixes #9
1 parent e2789f3 commit 06ee33e

6 files changed

Lines changed: 66 additions & 9 deletions

File tree

‎NAMESPACE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export(gsts)
6060
export(gtn)
6161
export(gwip)
6262
export(is_git)
63+
export(is_sha)
6364
export(lint_commit_msg)
6465
export(scrape_commits)
6566
export(trim_sha)

‎NEWS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# gitr (development version)
22

3+
* `is_sha()` now exported to test regex of expected `SHA1` pattern (@stufield, #9)
4+
35
# gitr 0.0.1 :tada:
46

5-
* Now released on CRAN! :partying_face:
7+
* Now released (#7) on CRAN! :partying_face:
68

79
* The package repository was restructured
810
- now functions should be easier to find

‎R/trim-sha.R‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ NULL
88
#' @describeIn sha
99
#' trims the `SHA-1` hash from the default full length to the
1010
#' human-readable short version.
11-
#' @return Character. The trimmed `sha`.
11+
#' @return [trim_sha()]: Character. The trimmed `sha`.
12+
#' If `sha` is not a `SHA1` hash, the identical string unchanged.
1213
#' @export
1314
trim_sha <- function(sha) {
14-
stopifnot(
15-
"`sha` must be a character." = is.character(sha),
16-
"`sha` must be longer than 7 chars." = nchar(sha) > 7
17-
)
18-
substr(sha, start = 1L, stop = 7L)
15+
idx <- which(is_sha(sha))
16+
sha[idx] <- substr(sha[idx], start = 1L, stop = 7L)
17+
sha
18+
}
19+
20+
#' @describeIn sha
21+
#' determines whether strings to be tested are a `SHA1` hash
22+
#' via regular expression (`"^[a-f0-9]{5,40}$"`) match.
23+
#' @return [is_sha()]: Logical. If `sha` matches the `SHA1` expected pattern.
24+
#' @seealso [grepl()]
25+
#' @export
26+
is_sha <- function(sha) {
27+
grepl("^[a-f0-9]{5,40}$", sha)
1928
}

‎README.Rmd‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,15 @@ git_unstage("DESCRIPTION")
152152
```
153153

154154

155-
#### SHA
155+
#### SHA1
156156
```{r trim}
157+
is_sha("d670c93733f3e1d7c95df7f61ebf6ca0476f14e3")
158+
159+
is_sha("foo")
160+
157161
trim_sha("d670c93733f3e1d7c95df7f61ebf6ca0476f14e3")
162+
163+
trim_sha("foo")
158164
```
159165

160166

‎man/sha.Rd‎

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-trim-sha.R‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
test_that("`trim_sha()` works as expected", {
3+
expect_equal(trim_sha("854ec87"), "854ec87")
4+
expect_equal(trim_sha("854ec871fbb8f2593275b077d596987cede73261"), "854ec87")
5+
expect_true(is_sha("854ec871fbb8f2593275b"), "854ec87")
6+
expect_equal(trim_sha("hello"), "hello")
7+
expect_equal(trim_sha(c("foo", "bar")), c("foo", "bar"))
8+
expect_equal(trim_sha(c("foo",
9+
"854ec871fbb8f2593275b077d596987cede73261",
10+
"bar")),
11+
c("foo", "854ec87", "bar")
12+
)
13+
expect_equal(trim_sha(""), "")
14+
expect_null(trim_sha(NULL))
15+
expect_equal(trim_sha(NA_character_), NA_character_)
16+
expect_equal(trim_sha(5), "5")
17+
expect_equal(trim_sha(LETTERS), LETTERS)
18+
})
19+
20+
test_that("`is_sha()` works as expected", {
21+
expect_true(is_sha("854ec87"))
22+
expect_true(is_sha("854ec871fbb8f2593275b"))
23+
expect_false(is_sha("854ec871fbb8f2593275b077d596987cede73261a")) # too long
24+
expect_false(is_sha("foo"))
25+
expect_false(is_sha("854e")) # too short
26+
expect_equal(is_sha(c("foo", "854ec87")), c(FALSE, TRUE)) # vector
27+
})

0 commit comments

Comments
 (0)