-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.R
More file actions
67 lines (64 loc) · 1.71 KB
/
Copy pathutils.R
File metadata and controls
67 lines (64 loc) · 1.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
66
67
ask_yn <- function(...) {
message(..., " [y/n]")
a <- "x"
while (!a %in% c("y", "n")) {
a <- tolower(readline(": "))
}
return(a == "y")
}
git_exe_cmd <- function(args, system2.arg.list = NULL) {
stdout_file_path <- tempfile()
stderr_file_path <- tempfile()
override_arg_list <- list(
command = "git",
args = args,
stdout = stdout_file_path,
stderr = stderr_file_path
)
arg_list <- as.list(system2.arg.list)
arg_list[names(override_arg_list)] <- override_arg_list
status <- do.call(system2, arg_list, quote = TRUE)
stdout <- tryCatch(readLines(stdout_file_path), error = function(e) e)
stderr <- tryCatch(readLines(stderr_file_path), error = function(e) e)
if (!identical(status, 0L)) {
stop(
"Got nonzero status ",
status,
"; stdout: ",
paste0(stdout, collapse = " "),
"; stderr: ",
paste0(stderr, collapse = " ")
)
}
return(list(status = status, stdout = stdout, stderr = stderr))
}
git_commit_if_changes_made <- function(
expr,
message
) {
stopifnot(
is.character(message),
!is.na(message)
)
message <- paste0(message, collapse = "\n")
s1 <- git_exe_cmd("status")
stopifnot(
any(grepl("nothing to commit, working tree clean", s1[["stdout"]]))
)
out <- expr # lazy eval triggered
s2 <- git_exe_cmd("status")
if (!identical(s1, s2)) {
git_exe_cmd(c("add", "-A"))
git_exe_cmd(c("commit", "-m", paste0("\"", message, "\"")))
}
return(out)
}
gitignore_append <- function(lines) {
if (file.exists(".gitignore")) {
gitignore_lines <- readLines(".gitignore")
} else {
gitignore_lines <- character(0L)
}
gitignore_lines <- union(gitignore_lines, lines)
writeLines(gitignore_lines, ".gitignore")
}