Skip to content

Commit f993bf3

Browse files
committed
restructure single vs multi-core fns for #1
1 parent 4c30477 commit f993bf3

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: githist
22
Title: Code analyses traced along the 'git' history of a package
3-
Version: 0.1.0.032
3+
Version: 0.1.0.033
44
Authors@R:
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-2172-5265"))

R/githist-fn.R

+40-32
Original file line numberDiff line numberDiff line change
@@ -37,59 +37,67 @@ githist <- function (path, n = NULL, step_size = 1L, num_cores = -1L) {
3737

3838
num_cores <- set_num_cores (num_cores)
3939

40-
path_cp <- fs::path (fs::path_temp (), basename (path))
41-
clean_after <- FALSE
42-
if (fs::path (fs::path_dir (path)) != fs::path_temp () &&
43-
!fs::dir_exists (path_cp)) {
44-
path_cp <- fs::dir_copy (path, fs::path_temp ())
45-
clean_after <- TRUE
46-
}
47-
48-
h <- gert::git_log (repo = path_cp, max = 1e6)
40+
h <- gert::git_log (repo = path, max = 1e6)
4941
if (step_size > 1L) {
5042
h <- h [seq (1, nrow (h), by = step_size), ]
5143
}
5244
if (!is.null (n)) {
5345
h <- h [seq_len (n), ]
5446
}
5547

56-
res <- extract_pkgstats_data (h, path_cp, num_cores)
48+
if (num_cores == 1L) {
49+
50+
res <- extract_pkgstats_data_single (h, path)
51+
52+
} else {
53+
54+
res <- extract_pkgstats_data_multi (h, path, num_cores)
5755

58-
if (clean_after) {
59-
fs::dir_delete (path_cp)
6056
}
6157

6258
collate_pkgstats (res)
6359
}
6460

65-
extract_pkgstats_data <- function (log, path, num_cores) {
61+
extract_pkgstats_data_single <- function (log, path) {
6662

67-
if (num_cores == 1L) {
68-
69-
res <- pbapply::pblapply (seq_len (nrow (log)), function (i) {
70-
g <- gert::git_reset_hard (ref = log$commit [i], repo = path)
71-
run_one_pkgstats (path = path, pkg_date = log$time [i])
72-
})
63+
path_cp <- fs::path (fs::path_temp (), basename (path))
64+
clean_after <- FALSE
65+
if (fs::path (fs::path_dir (path)) != fs::path_temp () &&
66+
!fs::dir_exists (path_cp)) {
67+
path_cp <- fs::dir_copy (path, fs::path_temp ())
68+
clean_after <- TRUE
69+
}
7370

74-
} else {
71+
res <- pbapply::pblapply (seq_len (nrow (log)), function (i) {
72+
g <- gert::git_reset_hard (ref = log$commit [i], repo = path_cp)
73+
run_one_pkgstats (path = path_cp, pkg_date = log$time [i])
74+
})
7575

76-
cl <- parallel::makeCluster (num_cores)
77-
parallel::clusterExport (cl, c ("log", "path", "run_one_pkgstats"))
78-
res <- pbapply::pblapply (seq_len (nrow (log)), function (i) {
79-
path_cp <- fs::dir_copy (path, fs::path_temp ())
80-
g <- gert::git_reset_hard (ref = log$commit [i], repo = path_cp)
81-
s <- run_one_pkgstats (path = path_cp, pkg_date = log$time [i])
82-
fs::dir_delete (path_cp)
83-
return (s)
84-
}, cl = cl)
85-
parallel::stopCluster (cl)
86-
87-
return (res)
76+
if (clean_after) {
77+
fs::dir_delete (path_cp)
8878
}
8979

9080
return (res)
9181
}
9282

83+
extract_pkgstats_data_multi <- function (log, path, num_cores) {
84+
85+
cl <- parallel::makeCluster (num_cores)
86+
parallel::clusterExport (cl, c ("log", "path", "run_one_pkgstats"))
87+
res <- pbapply::pblapply (seq_len (nrow (log)), function (i) {
88+
path_cp <- fs::dir_copy (path, fs::path_temp ())
89+
g <- gert::git_reset_hard (ref = log$commit [i], repo = path_cp)
90+
s <- run_one_pkgstats (path = path_cp, pkg_date = log$time [i])
91+
fs::dir_delete (path_cp)
92+
return (s)
93+
}, cl = cl)
94+
parallel::stopCluster (cl)
95+
96+
return (res)
97+
98+
return (res)
99+
}
100+
93101
collate_pkgstats <- function (x) {
94102
nms <- names (x [[1]])
95103
nms2df <- nms [seq_len (which (nms == "loc") - 1L)]

codemeta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"codeRepository": "https://github.com/ropensci-review-tools/githist",
99
"issueTracker": "https://github.com/ropensci-review-tools/githist/issues",
1010
"license": "https://spdx.org/licenses/GPL-3.0",
11-
"version": "0.1.0.032",
11+
"version": "0.1.0.033",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",

0 commit comments

Comments
 (0)