forked from r-lib/gert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.R
More file actions
300 lines (286 loc) · 8.91 KB
/
Copy pathfetch.R
File metadata and controls
300 lines (286 loc) · 8.91 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
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
#' Push and pull
#'
#' Functions to connect with a git server (remote) to fetch or push changes.
#' The 'credentials' package is used to handle authentication, the
#' [credentials vignette](https://docs.ropensci.org/credentials/articles/intro.html)
#' explains the various authentication methods for SSH and HTTPS remotes.
#'
#' Use [git_fetch()] and [git_push()] to sync a local branch with a remote
#' branch. Here [git_pull()] is a wrapper for [git_fetch()] which then tries to
#' [fast-forward][git_branch_fast_forward()] the local branch after fetching.
#'
#' @export
#' @family git
#' @name git_fetch
#' @rdname git_fetch
#' @inheritParams git_open
#' @useDynLib gert R_git_remote_fetch
#' @git remote
#' @param remote Optional. Name of a remote listed in [git_remote_list()]. If
#' unspecified and the current branch is already tracking branch a remote
#' branch, that remote is honored. Otherwise, defaults to `origin`.
#' @param refspec string with mapping between remote and local refs. Default
#' uses the default refspec from the remote, which usually fetches all branches.
#' @param mirror use the `--mirror` flag
#' @param bare use the `--bare` flag
#' @param force use the `--force` flag
#' @param prune delete tracking branches that no longer exist on the remote, or
#' are not in the refspec (such as pull requests).
git_fetch <- function(
remote = NULL,
refspec = NULL,
password = askpass,
ssh_key = NULL,
prune = FALSE,
verbose = interactive(),
repo = '.'
) {
repo <- git_open(repo)
info <- git_info(repo)
if (!length(remote)) {
remote <- info$remote
}
remote <- as.character(remote)
if (!length(remote) || is.na(remote)) {
if (is.na(match("origin", git_remote_list(repo = repo)$name))) {
stop("No remote is set for this branch")
} else {
inform("No remote set for this branch, using default remote 'origin'")
remote <- "origin"
}
}
refspec <- as.character(refspec)
prune <- as.logical(prune)
verbose <- as.logical(verbose)
host <- remote_to_host(repo, remote)
key_cb <- make_key_cb(ssh_key, host = host, password = password)
cred_cb <- make_cred_cb(password = password, verbose = verbose)
.Call(
R_git_remote_fetch,
repo,
remote,
refspec,
key_cb,
cred_cb,
prune,
verbose
)
git_repo_path(repo)
}
#' @export
#' @rdname git_fetch
#' @useDynLib gert R_git_remote_ls
git_remote_ls <- function(
remote = NULL,
password = askpass,
ssh_key = NULL,
verbose = interactive(),
repo = '.'
) {
repo <- git_open(repo)
info <- git_info(repo)
if (!length(remote)) {
remote <- info$remote
}
remote <- as.character(remote)
if (!length(remote) || is.na(remote)) {
if (is.na(match("origin", git_remote_list(repo = repo)$name))) {
stop("No remote is set for this branch")
} else {
inform("No remote set for this branch, using default remote 'origin'")
remote <- "origin"
}
}
verbose <- as.logical(verbose)
host <- remote_to_host(repo, remote)
key_cb <- make_key_cb(ssh_key, host = host, password = password)
cred_cb <- make_cred_cb(password = password, verbose = verbose)
.Call(R_git_remote_ls, repo, remote, key_cb, cred_cb, verbose)
}
#' @export
#' @rdname git_fetch
#' @param set_upstream change the branch default upstream to `remote`.
#' If `NULL`, this will set the branch upstream only if the push was
#' successful and if the branch does not have an upstream set yet.
#' @useDynLib gert R_git_remote_push
git_push <- function(
remote = NULL,
refspec = NULL,
set_upstream = NULL,
password = askpass,
ssh_key = NULL,
mirror = FALSE,
force = FALSE,
verbose = interactive(),
repo = '.'
) {
repo <- git_open(repo)
info <- git_info(repo)
verbose <- as.logical(verbose)
if (!length(remote)) {
remote <- info$remote
}
remote <- as.character(remote)
if (!length(remote) || is.na(remote)) {
if (is.na(match("origin", git_remote_list(repo = repo)$name))) {
stop("No remote is set for this branch")
} else {
if (verbose) {
inform("No remote set for this branch, using default remote 'origin'")
}
remote <- "origin"
}
}
if (isTRUE(mirror)) {
refs <- info$reflist
# Ignore github's special refs
refs <- refs[!grepl("^refs/pull", refs)]
refspec <- paste0(refs, ":", refs)
}
if (!length(refspec)) {
refspec <- info$head
}
refspec <- as.character(refspec)
if (isTRUE(force)) {
refspec <- sub("^\\+?", "+", refspec)
}
host <- remote_to_host(repo, remote)
key_cb <- make_key_cb(ssh_key, host = host, password = password)
cred_cb <- make_cred_cb(password = password, verbose = verbose)
.Call(R_git_remote_push, repo, remote, refspec, key_cb, cred_cb, verbose)
if (is.null(set_upstream)) {
set_upstream <- isTRUE(is.na(info$upstream)) && !isTRUE(info$bare)
}
if (isTRUE(set_upstream)) {
git_branch_set_upstream(paste0(remote, "/", info$shorthand), repo = repo)
}
git_repo_path(repo)
}
#' @export
#' @rdname git_fetch
#' @useDynLib gert R_git_repository_clone
#' @git clone
#' @param url remote url. Typically starts with `https://github.com/` for public
#' repositories, and `https://yourname@github.com/` or `git@github.com/` for
#' private repos. You will be prompted for a password or pat when needed.
#' @param path Directory of the Git repository to create.
#' By default, the "humanish" part of the URL.
#' For instance, "git@github.com:someone/myrepo.git" will be cloned to
#' `myrepo/`.
#' @param ssh_key path or object containing your ssh private key. By default we
#' look for keys in `ssh-agent` and [credentials::ssh_key_info()].
#' @param branch name of branch to check out locally
#' @param password a string or a callback function to get passwords for authentication
#' or password protected ssh keys. Defaults to [askpass][askpass::askpass()] which
#' checks `getOption('askpass')`.
#' @param verbose display some progress info while downloading
#' @examples {# Clone a small repository
#' git_dir <- file.path(tempdir(), 'antiword')
#' git_clone('https://github.com/ropensci/antiword', git_dir)
#'
#' # Change into the repo directory
#' olddir <- getwd()
#' setwd(git_dir)
#'
#' # Show some stuff
#' git_log()
#' git_branch_list()
#' git_remote_list()
#'
#' # Add a file
#' write.csv(iris, 'iris.csv')
#' git_add('iris.csv')
#'
#' # Commit the change
#' jerry <- git_signature("Jerry", "jerry@hotmail.com")
#' git_commit('added the iris file', author = jerry)
#'
#' # Now in the log:
#' git_log()
#'
#' # Cleanup
#' setwd(olddir)
#' unlink(git_dir, recursive = TRUE)
#' }
git_clone <- function(
url,
path = NULL,
branch = NULL,
password = askpass,
ssh_key = NULL,
bare = FALSE,
mirror = FALSE,
verbose = interactive()
) {
stopifnot(is.character(url))
# "humanish" part of the URL
# https://github.com/git/git/blob/6e8d538aab8fe4dd07ba9fb87b5c7edcfa5706ad/dir.h#L494
if (!length(path)) {
path <- file.path(
getwd(),
sub("\\.git$", "", sub("/.git$", "", basename(url)))
)
}
stopifnot(is.character(path))
stopifnot(is.null(branch) || is.character(branch))
verbose <- as.logical(verbose)
path <- normalizePath(path.expand(path), mustWork = FALSE)
host <- url_to_host(url)
key_cb <- make_key_cb(ssh_key, host = host, password = password)
cred_cb <- make_cred_cb(password = password, verbose = verbose)
repo <- .Call(
R_git_repository_clone,
url,
path,
branch,
key_cb,
cred_cb,
bare,
mirror,
verbose
)
git_repo_path(repo)
}
#' @export
#' @rdname git_fetch
#' @param rebase if TRUE we try to rebase instead of merge local changes. This
#' is not possible in case of conflicts (you will get an error).
#' @param ... arguments passed to `git_fetch()`
git_pull <- function(remote = NULL, rebase = FALSE, ..., repo = '.') {
repo <- git_open(repo)
info <- git_info(repo)
branch <- info$shorthand
if (branch == "HEAD") {
stop("Repository is currently in a detached head state")
}
upstream <- if (length(remote) && nchar(remote)) {
paste0(remote, '/', branch)
} else {
info$upstream
}
if (!length(upstream) || is.na(upstream) || !nchar(upstream)) {
stop("No upstream configured for current branch, please specify a remote")
}
if (grepl(".*/pr/\\d+$", upstream)) {
pr <- utils::tail(strsplit(upstream, '/pr/', fixed = TRUE)[[1]], 1)
try(git_fetch_pull_requests(pr = pr, remote = remote, repo = repo))
}
if (git_branch_exists(upstream, local = TRUE, repo = repo)) {
inform("Local upstream, skipping fetch")
} else {
git_fetch(remote, ..., repo = repo)
if (!git_branch_exists(upstream, local = FALSE, repo = repo)) {
stop("Failed to fetch upstream branch: ", upstream)
}
}
if (isTRUE(rebase)) {
rebase_df <- git_rebase_list(upstream = upstream, repo = repo)
if (any(rebase_df$conflicts)) {
stop("Found conflicts, rebase not possible. Retry with rebase = FALSE")
}
git_rebase_commit(upstream = upstream, repo = repo)
} else {
git_merge(upstream, repo = repo)
}
git_repo_path(repo)
}