Skip to content

Commit a02cc81

Browse files
authored
Merge branch 'main' into get
2 parents 5fd2978 + deb6be6 commit a02cc81

6 files changed

Lines changed: 45 additions & 6 deletions

File tree

NEWS.md

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

33
- Add `git_config_get()`, `git_config_local_get()`, and `git_config_global_get()` to retrieve a single named config option, returning `NULL` if unset (#267).
4+
- `git_clone()` without a `path` argument now clones into a directory named after the
5+
"humanish" part of the URL, so "git@github.com:francisbarton/myrepo.git" gets cloned into `myrepo` (@francisbardon, #192).
46
- `git_remote_set_pushurl()` gains an `add` argument to append push URLs instead of replacing them. (@robitalec, #128)
57
- Fix `git_info()` for the case when no upstream is configured (@mpage, #263)
68
- `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

R/repository.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#' * `git_info()` shows basic information about a repository, such as the SHA
66
#' and branch of the current HEAD.
77
#'
8+
#' @section Path:
9+
#'
810
#' For `git_init()` the `path` parameter sets the directory of the git repository
911
#' to create. If this directory already exists, it must be empty. If it does
1012
#' not exist, it is created, along with any intermediate directories that don't
@@ -21,7 +23,7 @@
2123
#' @family git
2224
#' @useDynLib gert R_git_repository_init
2325
#' @inheritParams git_open
24-
#' @param path the location of the git repository, see details.
26+
#' @param path the location of the git repository, see the "path" section.
2527
#' @param bare if true, a Git repository without a working directory is created
2628
#' @git repository
2729
#' @return
@@ -68,6 +70,9 @@ git_find <- function(path = '.') {
6870
#' @export
6971
#' @rdname git_repo
7072
#' @useDynLib gert R_git_repository_info
73+
#' @section Detached head:
74+
#' If `git_info()$shorthand` is equal to `HEAD`,
75+
#' it means the repository is in a [detached head state](https://jvns.ca/blog/2023/11/01/confusing-git-terminology/#detached-head-state).
7176
git_info <- function(repo = '.') {
7277
repo <- git_open(repo)
7378
.Call(R_git_repository_info, repo)

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.

man/git_repo.Rd

Lines changed: 11 additions & 2 deletions
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)