-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathinstall-git.R
346 lines (299 loc) · 10.8 KB
/
install-git.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#' Install a package from a git repository
#'
#' It is vectorised so you can install multiple packages with
#' a single command. You do not need to have the `git2r` package,
#' or an external git client installed.
#'
#' If you need to set git credentials for use in the `Remotes` field you can do
#' so by placing the credentials in the `remotes.git_credentials` global
#' option.
#'
#' @param url Location of package. The url should point to a public or
#' private repository.
#' @param ref Name of branch, tag or SHA reference to use, if not HEAD.
#' @param branch Deprecated, synonym for ref.
#' @param subdir A sub-directory within a git repository that may
#' contain the package we are interested in installing.
#' @param credentials A git2r credentials object passed through to clone.
#' Supplying this argument implies using `git2r` with `git`.
#' @param git Whether to use the `git2r` package, or an external
#' git client via system. Default is `git2r` if it is installed,
#' otherwise an external git installation.
#' @param ... Other arguments passed on to [utils::install.packages()].
#' @inheritParams install_github
#' @family package installation
#' @export
#' @examples
#' \dontrun{
#' install_git("https://github.com/hadley/stringr.git")
#' install_git("https://github.com/hadley/stringr.git", ref = "stringr-0.2")
#' }
install_git <- function(url, subdir = NULL, ref = NULL, branch = NULL,
credentials = git_credentials(),
git = c("auto", "git2r", "external"),
dependencies = NA,
upgrade = c("default", "ask", "always", "never"),
force = FALSE,
quiet = FALSE,
build = TRUE, build_opts = c("--no-resave-data", "--no-manual", "--no-build-vignettes"),
build_manual = FALSE, build_vignettes = FALSE,
repos = getOption("repos"),
type = getOption("pkgType"),
...) {
if (!missing(branch)) {
warning("`branch` is deprecated, please use `ref`")
ref <- branch
}
remotes <- lapply(url, git_remote,
subdir = subdir, ref = ref,
credentials = credentials, git = match.arg(git)
)
install_remotes(remotes,
credentials = credentials,
dependencies = dependencies,
upgrade = upgrade,
force = force,
quiet = quiet,
build = build,
build_opts = build_opts,
build_manual = build_manual,
build_vignettes = build_vignettes,
repos = repos,
type = type,
git = match.arg(git),
...
)
}
git_remote <- function(url, subdir = NULL, ref = NULL, credentials = git_credentials(),
git = c("auto", "git2r", "external"), ...) {
git <- match.arg(git)
if (git == "auto") {
git <- if (!is_standalone() && pkg_installed("git2r")) "git2r" else "external"
}
if (!is.null(credentials) && git != "git2r") {
stop("`credentials` can only be used with `git = \"git2r\"`", call. = FALSE)
}
url_parts = re_match( url,
"(?<protocol>[^/]*://)?(?<authhost>[^/]+)(?<path>[^@]*)(@(?<ref>.*))?")
ref <- ref %||% (if (url_parts$ref == "") NULL else url_parts$ref)
url = paste0(url_parts$protocol, url_parts$authhost, url_parts$path)
list(git2r = git_remote_git2r, external = git_remote_xgit)[[git]](url, subdir, ref, credentials)
}
git_remote_git2r <- function(url, subdir = NULL, ref = NULL, credentials = git_credentials()) {
remote("git2r",
url = url,
subdir = subdir,
ref = ref,
credentials = credentials
)
}
git_remote_xgit <- function(url, subdir = NULL, ref = NULL, credentials = git_credentials()) {
remote("xgit",
url = url,
subdir = subdir,
ref = ref
)
}
#' @export
remote_download.git2r_remote <- function(x, quiet = FALSE) {
if (!quiet) {
message("Downloading git repo ", x$url)
}
bundle <- tempfile()
git2r::clone(x$url, bundle, credentials = x$credentials, progress = FALSE)
if (!is.null(x$ref)) {
r <- git2r::repository(bundle)
git2r::checkout(r, x$ref)
}
bundle
}
#' @export
remote_metadata.git2r_remote <- function(x, bundle = NULL, source = NULL, sha = NULL) {
if (!is.null(bundle)) {
r <- git2r::repository(bundle)
sha <- git2r::commits(r)[[1]]$sha
} else {
sha <- NULL
}
list(
RemoteType = "git2r",
RemoteUrl = x$url,
RemoteSubdir = x$subdir,
RemoteRef = x$ref,
RemoteSha = sha
)
}
#' @export
remote_package_name.git2r_remote <- function(remote, ...) {
tmp <- tempfile()
on.exit(unlink(tmp))
description_path <- paste0(collapse = "/", c(remote$subdir, "DESCRIPTION"))
if (grepl("^https?://", remote$url)) {
# assumes GitHub-style "<repo>/raw/<ref>/<path>" url
url <- build_url(sub("\\.git$", "", remote$url), "raw", remote_sha(remote, ...), description_path)
download_args <- list(path = tmp, url = url)
if (!is.null(remote$credentials)) {
if (inherits(remote$credentials, "cred_user_pass")) {
download_args$basic_auth <- list(
user = remote$credentials$username,
password = remote$credentials$password
)
} else if (inherits(remote$credentials, "cred_env")) {
if (Sys.getenv(remote$credentials$username) == "") {
stop(paste0("Environment variable `", remote$credentials$username, "` is unset."), .call = FALSE)
}
if (Sys.getenv(remote$credentials$password) == "") {
stop(paste0("Environment variable `", remote$credentials$password, "` is unset."), .call = FALSE)
}
download_args$basic_auth <- list(
user = Sys.getenv(remote$credentials$username),
password = Sys.getenv(remote$credentials$username)
)
} else if (inherits(remote$credentials, "cred_token")) {
if (Sys.getenv(remote$credentials$token) == "") {
stop(paste0("Environment variable `", remote$credentials$token, "` is unset."), .call = FALSE)
}
download_args$auth_token <- Sys.getenv(remote$credentials$token)
} else if (inherits(remote$credentials, "cred_ssh_key")) {
stop(paste(
"Unable to fetch the package DESCRIPTION file using SSH key authentication.",
"Try using `git2r::cred_user_pass`, `git2r::cred_env`, or `git2r::cred_token` instead of `git2r::cred_ssh_key` for authentication."
), .call = FALSE)
} else {
stop(paste(
"`remote$credentials` is not NULL and it does not inherit from a recognized class.",
"Recognized classes for `remote$credentials` are `cred_user_pass`, `cred_env`, `cred_token`, and `cred_ssh_key`."
), .call = FALSE)
}
}
tryCatch({
do.call(download, args = download_args)
read_dcf(tmp)$Package
}, error = function(e) {
NA_character_
})
} else {
# Try using git archive --remote to retrieve the DESCRIPTION, if the protocol
# or server doesn't support that return NA
res <- try(
silent = TRUE,
system_check(git_path(),
args = c(
"archive", "-o", tmp, "--remote", remote$url,
if (is.null(remote$ref)) "HEAD" else remote$ref,
description_path
),
quiet = TRUE
)
)
if (inherits(res, "try-error")) {
res <- try(
silent = TRUE,
{
bundle <- remote_download(remote, quiet = TRUE)
bundle_description_path <- file.path(bundle, description_path)
if (file.exists(bundle_description_path)) {
description_path_dir <- file.path(tempdir(), dirname(description_path))
dir.create(description_path_dir, recursive = TRUE,
showWarnings = FALSE)
file.copy(bundle_description_path,
file.path(tempdir(), description_path),
overwrite = TRUE)
}
})
if (inherits(res, "try-error")) {
return(NA_character_)
}
} else {
# git archive returns a tar file, so extract it to tempdir and read the DCF
utils::untar(tmp, files = description_path, exdir = tempdir())
}
read_dcf(file.path(tempdir(), description_path))$Package
}
}
#' @export
remote_sha.git2r_remote <- function(remote, ...) {
tryCatch(
{
# set suppressWarnings in git2r 0.23.0+
res <- suppressWarnings(git2r::remote_ls(remote$url, credentials = remote$credentials))
ref <- remote$ref %||% "HEAD"
if (ref != "HEAD") ref <- paste0("/", ref)
found <- grep(pattern = paste0(ref, "$"), x = names(res))
# If none found, it is either a SHA, so return the pinned sha or NA
if (length(found) == 0) {
return(remote$ref %||% NA_character_)
}
unname(res[found[1]])
},
error = function(e) {
warning(e)
NA_character_
}
)
}
#' @export
format.xgit_remote <- function(x, ...) {
"Git"
}
#' @export
format.git2r_remote <- function(x, ...) {
"Git"
}
#' @export
remote_download.xgit_remote <- function(x, quiet = FALSE) {
if (!quiet) {
message("Downloading git repo ", x$url)
}
bundle <- tempfile()
args <- c("clone", "--depth", "1", "--no-hardlinks")
args <- c(args, x$args, x$url, bundle)
git(paste0(args, collapse = " "), quiet = quiet)
if (!is.null(x$ref)) {
git(paste0(c("fetch", "origin", x$ref), collapse = " "), quiet = quiet, path = bundle)
git(paste0(c("checkout", "FETCH_HEAD"), collapse = " "), quiet = quiet, path = bundle)
}
bundle
}
#' @export
remote_metadata.xgit_remote <- function(x, bundle = NULL, source = NULL, sha = NULL) {
if (is_na(sha)) {
sha <- NULL
}
list(
RemoteType = "xgit",
RemoteUrl = x$url,
RemoteSubdir = x$subdir,
RemoteRef = x$ref,
RemoteSha = sha,
RemoteArgs = if (length(x$args) > 0) paste0(deparse(x$args), collapse = " ")
)
}
#' @importFrom utils read.delim
#' @export
remote_package_name.xgit_remote <- remote_package_name.git2r_remote
#' @export
remote_sha.xgit_remote <- function(remote, ...) {
url <- remote$url
ref <- remote$ref
refs <- git(paste("ls-remote", url, ref))
# If none found, it is either a SHA, so return the pinned SHA or NA
if (length(refs) == 0) {
return(remote$ref %||% NA_character_)
}
refs_df <- read.delim(
text = refs, stringsAsFactors = FALSE, sep = "\t",
header = FALSE
)
names(refs_df) <- c("sha", "ref")
refs_df$sha[[1]]
}
#' Specify git credentials to use
#'
#' The global option `remotes.git_credentials` is used to set the git
#' credentials.
#' @export
#' @keywords internal
git_credentials <- function() {
getOption("remotes.git_credentials", NULL)
}