Skip to content

Commit 97a5910

Browse files
authored
feat: git_clone() creates path more similarly to git clone (#272)
1 parent 1ae6833 commit 97a5910

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# gert (development version)
22

3+
- `git_clone()` without a `path` argument now clones into a directory named after the
4+
"humanish" part of the URL, so "git@github.com:francisbarton/myrepo.git" gets cloned into `myrepo` (@francisbardon, #192).
35
- `git_remote_set_pushurl()` gains an `add` argument to append push URLs instead of replacing them. (@robitalec, #128)
46
- Fix `git_info()` for the case when no upstream is configured (@mpage, #263)
57
- `git_branch_create()`: `force` now also applies to the checkout step, allowing branch creation even when local changes would be overwritten (@MichaelChirico, #177).

R/fetch.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ git_push <- function(
174174
#' repositories, and `https://yourname@github.com/` or `git@github.com/` for
175175
#' private repos. You will be prompted for a password or pat when needed.
176176
#' @param path Directory of the Git repository to create.
177+
#' By default, the "humanish" part of the URL.
178+
#' For instance, "git@github.com:someone/myrepo.git" will be cloned to
179+
#' `myrepo/`.
177180
#' @param ssh_key path or object containing your ssh private key. By default we
178181
#' look for keys in `ssh-agent` and [credentials::ssh_key_info()].
179182
#' @param branch name of branch to check out locally
@@ -220,8 +223,13 @@ git_clone <- function(
220223
verbose = interactive()
221224
) {
222225
stopifnot(is.character(url))
226+
# "humanish" part of the URL
227+
# https://github.com/git/git/blob/6e8d538aab8fe4dd07ba9fb87b5c7edcfa5706ad/dir.h#L494
223228
if (!length(path)) {
224-
path <- file.path(getwd(), basename(url))
229+
path <- file.path(
230+
getwd(),
231+
sub("\\.git$", "", sub("/.git$", "", basename(url)))
232+
)
225233
}
226234
stopifnot(is.character(path))
227235
stopifnot(is.null(branch) || is.character(branch))
@@ -249,7 +257,7 @@ git_clone <- function(
249257
#' @param rebase if TRUE we try to rebase instead of merge local changes. This
250258
#' is not possible in case of conflicts (you will get an error).
251259
#' @param ... arguments passed to `git_fetch()`
252-
git_pull <- function(remote = NULL, rebase = FALSE, ..., repo = '.'){
260+
git_pull <- function(remote = NULL, rebase = FALSE, ..., repo = '.') {
253261
repo <- git_open(repo)
254262
info <- git_info(repo)
255263
branch <- info$shorthand

man/git_fetch.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-clone.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ test_that("cloning repositories works", {
3333
expect_equal(zip::zip_list('gert.zip')$filename, git_ls(repo = repo)$path)
3434
unlink('gert.zip')
3535
})
36+
37+
test_that("cloning repositories works, no path", {
38+
skip_if_offline('github.com')
39+
path <- file.path(tempdir(), 'gert-test')
40+
dir.create(path)
41+
on.exit(unlink(path, recursive = TRUE))
42+
oldwd <- getwd()
43+
on.exit(setwd(oldwd), add = TRUE)
44+
setwd(path)
45+
repo <- git_clone('https://github.com/r-lib/gert.git')
46+
expect_true(file.exists(file.path(path, "gert", 'DESCRIPTION')))
47+
})

0 commit comments

Comments
 (0)