/
get_info_files.R
226 lines (206 loc) · 6.77 KB
/
get_info_files.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#' Get time
#'
#' @param object hunks list
#'
#' @return time
#' @noRd
get_time <- function(object) {
pluck(object, "final_signature", "when", "time")
}
#' Get the first and last modification time for a specific file, based on git2r::blame().
#'
#' @param path path to the file
#' @param repo repo of the git project
#'
#' @return A list with information of the selected file:
#'
#' - file: file name
#' - in_repository: Logical. Whether the file has already been commit once in the
#' git repository
#' - first_modif: time of first modification. Commit time if in the git repository,
#' system date of creation otherwise.
#' - last_modif: time of last modification. Commit time if in the git repository,
#' system date of last modification otherwise.
#'
#' @export
#'
#' @importFrom git2r blame
#' @importFrom purrr pluck map flatten_dbl set_names
#' @importFrom dplyr mutate pull
#'
#' @examples
#' repo <- fake_repo()
#' get_info(list.files(repo)[1], repo = repo)
get_info <- function(path, repo = ".") {
# test if in git repo
in_repository <- path %in%
(git2r::ls_tree(repo = repo, recursive = TRUE) %>%
mutate(filepath = paste0(path, name)) %>%
pull(filepath))
if (in_repository) {
# In git
blame_object <- blame(repo = repo, path = path)
file <- path #basename(blame_object$hunks[[1]]$orig_path) # %>%
# gsub(pattern = "^R/", replacement = "") %>%
# gsub(pattern = "\\.R$", replacement = "")
first_last <- blame_object %>%
pluck("hunks") %>%
map(get_time) %>%
flatten_dbl() %>%
range() %>%
as.POSIXct.numeric(origin = "1970-01-01") %>%
set_names(nm = c("first", "last"))
} else {
# Not in git repo
file <- path
first_last <- c(as.POSIXct.numeric(NA_real_, origin = "1970-01-01"),
file.info(file.path(repo, path))$mtime) %>%
set_names(nm = c("first", "last"))
}
list(
file = file,
in_repository = in_repository,
first_modif = first_last[1],
last_modif = first_last[2]
)
}
#' Get the first and last modification time of files of a directory
#'
#' @param repo git repository
#' @param path Default to R folder. Use "" for the complete directory
#' @param recursive Logical. Should the listing recurse into directories?
#' @param untracked Logical. Should the not tracked files be included?
#'
#' @return A list of files with information of each file:
#'
#' - file: file name
#' - in_repository: Logical. Whether the file has already been commit once in the
#' git repository
#' - first_modif: time of first modification. Commit time if in the git repository,
#' system date of creation otherwise.
#' - last_modif: time of last modification. Commit time if in the git repository,
#' system date of last modification otherwise.
#'
#' @export
#'
#' @importFrom purrr map
#'
#' @examples
#' repo <- fake_repo()
#' # Complete repository
#' get_last_modif(repo = repo, path = "")
#' repo <- fake_repo(as.package = TRUE)
#' # Complete repository
#' get_last_modif(repo = repo, path = "")
get_last_modif <- function(repo = ".", path = "R",
recursive = TRUE, untracked = TRUE) {
folder <- normalizePath(file.path(repo, path), mustWork = FALSE)
if (dir.exists(folder)) {
if (path != "") {
# One directory
files <- git2r::ls_tree(repo = repo, recursive = recursive) %>%
filter(path == paste0(!!path, "/")) %>%
mutate(filepath = paste0(path, name)) %>%
pull(filepath)
if (isTRUE(untracked)) {
not_in_git <- git2r::status(repo, all_untracked = TRUE) %>% unlist()
files <- c(files, not_in_git[grepl(paste0(path, "/"), not_in_git)])
}
} else {
# All files
files <- git2r::ls_tree(repo = repo, recursive = recursive) %>%
mutate(filepath = paste0(path, name)) %>%
pull(filepath)
if (isTRUE(untracked)) {
not_in_git <- git2r::status(repo, all_untracked = TRUE) %>% unlist()
files <- c(files, not_in_git[grepl(paste0(path, "/"), not_in_git)])
}
}
} else {
stop(path, "/ folder was not found")
}
if (length(files) == 0) {
stop("There are no files to show. ",
"Check the path, recursive and untracked parameters.")
}
map(files, ~ get_info(.x, repo = repo))
}
#' Presenting results of files and last modification time in a printed table using 'kable()'
#'
#' @inheritParams get_last_modif
#'
#' @importFrom dplyr transmute
#'
#' @return A 'kable()' output to be included in a markdown file
#' @export
#'
#' @examples
#' repo <- fake_repo(as.package = TRUE)
#' cat(present_files(repo))
present_files <- function(repo = ".", path = "R",
recursive = TRUE, untracked = TRUE) {
get_last_modif(repo, path, recursive, untracked) %>%
purrr::map_dfr(as_tibble) %>%
transmute(
File = file,
`Tracked in git` = ifelse(in_repository, "Yes", "No"),
`Date of creation` = first_modif,
`Last modification` = last_modif
) %>%
knitr::kable(., format = "markdown") %>%
paste(., collapse = " \n")
}
#' Creates and updates a vignette in the 'vignette/' directory of a package with last modification time of tracked files
#'
#' @return No return value, called for side effect.
#' Creates and updates the content of the "modification_files.Rmd" vignette
#' @export
#'
#' @rdname create_vignette_last_modif
#' @examples
#'
#' # Creates vignette
#' repo <- fake_repo(as.package = TRUE)
#' if (rmarkdown::pandoc_available("2.0.0")) {
#' create_vignette_last_modif(repo, path = "R")
#' }
create_vignette_last_modif <- function(repo = ".", path = "R",
recursive = TRUE, untracked = TRUE) {
vig <- file.path(repo, "vignettes")
if (!dir.exists(vig)) {
stop("vignettes folder doesn't exist, please create vignettes folder")
} else {
update_vignette_last_modif(repo, path, recursive, untracked)
}
}
#' @inheritParams get_last_modif
#'
#' @export
#' @rdname create_vignette_last_modif
#'
#' @examples
#' # update vignette
#' repo <- fake_repo(as.package = TRUE)
#' if (rmarkdown::pandoc_available("2.0.0")) {
#' update_vignette_last_modif(repo, path = "R")
#' rmarkdown::render(file.path(repo, "vignettes", "modification_files.Rmd"))
#' }
update_vignette_last_modif <- function(repo = ".", path = "R",
recursive = TRUE, untracked = TRUE) {
vig <- file.path(repo, "vignettes")
file <- file.path(vig, "modification_files.Rmd")
if (file.exists(file)) {
unlink(file)
}
path_to_copy <- system.file("template/modification_files.Rmd", package = "gitdown")
file.copy(path_to_copy, to = vig)
if (file.exists(file)) {
md <- c(
paste0("Created on: ", Sys.time()),
"\n\n",
present_files(repo, path, recursive, untracked), "\n\n")
write(md, file = file, append = TRUE)
} else {
stop("Copying the file didn't work!")
}
}