From ab9be0ca6e46bc30b2d74a0cfb52976527110fc1 Mon Sep 17 00:00:00 2001 From: Nikolas Burkoff Date: Wed, 10 Nov 2021 11:19:33 +0000 Subject: [PATCH 1/4] fixgetting remote_name --- R/git_tools.R | 19 ++++++++++++++--- tests/testthat/test-git_tools_mocking.R | 28 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/R/git_tools.R b/R/git_tools.R index 9422cf7..4c5a573 100644 --- a/R/git_tools.R +++ b/R/git_tools.R @@ -184,15 +184,28 @@ install_external_deps <- function(repo_dir, internal_pkg_deps, ...) { # function to get the remote name (e.g. origin) which matches # the url given in the staged.deps yaml file get_remote_name <- function(git_repo, repo_url) { + + # remove the https:// and .git from repo_url + repo_url <- strsplit(repo_url, "//", fixed = TRUE)[[1]][2] + repo_url <- gsub(".git", "", repo_url) + + remotes <- git2r::remotes(git_repo) + for (remote in remotes) { + target_url <- git2r::remote_url(git_repo, remote = remote) - #sometimes git2r remote_url includes .git in remote_url sometimes does not - if (repo_url == target_url || repo_url == paste0(target_url, ".git")) { + target_url <- gsub(":", "/", target_url, fixed = TRUE) + + #print(repo_url) + #print(target_url) + #print(grepl(repo_url, target_url)) + if (grepl(repo_url, target_url)) { return(remote) } } - stop("Cannot determine remote") + # by default return origin + return("origin") } diff --git a/tests/testthat/test-git_tools_mocking.R b/tests/testthat/test-git_tools_mocking.R index d08dc9f..daa6e53 100644 --- a/tests/testthat/test-git_tools_mocking.R +++ b/tests/testthat/test-git_tools_mocking.R @@ -18,7 +18,6 @@ test_that("checkout_repo with mocking works", { # delete other local branches local_branches <- git2r::branches(repo_dir, flags = "local") lapply(local_branches[names(local_branches) != "main"], git2r::branch_delete) - git2r::remote_set_url(repo_dir, name = "origin", url = url) git2r::repository(local_path) }) @@ -27,6 +26,8 @@ test_that("checkout_repo with mocking works", { invisible(NULL) }) + mockery::stub(checkout_repo, 'get_remote_name', function(...) "origin") + with_tmp_cachedir({ repo_dir <- file.path(tempfile(), "stageddeps.food") @@ -76,3 +77,28 @@ test_that("check_only_remote_branches works", { regexp = "remote_name", fixed = TRUE ) }) + + +test_that("get_remote_name provides remote name of repo",{ + + mockery::stub(get_remote_name, 'git2r::remote_url', function(git_repo, remote){ + switch(remote, + A = "git@ssh.github.com:x/y.git", + B = "git@github.com:w/v.git", + C = "https://github.com/k/l/m", + D = "https://github.com/a/b.git", + origin = "https://github.com/xxx/c.git" + ) + }) + + + mockery::stub(get_remote_name, 'git2r::remotes', function(git_repo) c("origin", "A", "B", "C", "D")) + expect_equal(get_remote_name(".", "https://github.com/x/y.git"), "A") + expect_equal(get_remote_name(".", "https://github.com/w/v.git"), "B") + expect_equal(get_remote_name(".", "https://github.com/k/l/m.git"), "C") + expect_equal(get_remote_name(".", "https://github.com/a/b.git"), "D") + expect_equal(get_remote_name(".", "https://other.git"), "origin") + + +}) + From f8a41c01ede07848a981b190d72c7472e49f67c2 Mon Sep 17 00:00:00 2001 From: Nikolas Burkoff Date: Wed, 10 Nov 2021 11:20:10 +0000 Subject: [PATCH 2/4] remove print --- R/git_tools.R | 3 --- 1 file changed, 3 deletions(-) diff --git a/R/git_tools.R b/R/git_tools.R index 4c5a573..e9157f7 100644 --- a/R/git_tools.R +++ b/R/git_tools.R @@ -197,9 +197,6 @@ get_remote_name <- function(git_repo, repo_url) { target_url <- git2r::remote_url(git_repo, remote = remote) target_url <- gsub(":", "/", target_url, fixed = TRUE) - #print(repo_url) - #print(target_url) - #print(grepl(repo_url, target_url)) if (grepl(repo_url, target_url)) { return(remote) } From 17a74b6431da95858c08660e3fda3e2691a8c107 Mon Sep 17 00:00:00 2001 From: Nikolas Burkoff Date: Wed, 10 Nov 2021 12:28:56 +0000 Subject: [PATCH 3/4] fix after review --- R/git_tools.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/git_tools.R b/R/git_tools.R index e9157f7..ac564f1 100644 --- a/R/git_tools.R +++ b/R/git_tools.R @@ -186,8 +186,8 @@ install_external_deps <- function(repo_dir, internal_pkg_deps, ...) { get_remote_name <- function(git_repo, repo_url) { # remove the https:// and .git from repo_url - repo_url <- strsplit(repo_url, "//", fixed = TRUE)[[1]][2] - repo_url <- gsub(".git", "", repo_url) + repo_url <- gsub("^.+://", "", repo_url, perl = TRUE) + repo_url <- gsub(".git$", "", repo_url) remotes <- git2r::remotes(git_repo) @@ -195,9 +195,10 @@ get_remote_name <- function(git_repo, repo_url) { for (remote in remotes) { target_url <- git2r::remote_url(git_repo, remote = remote) + target_url <- gsub("^(https://|git@ssh.|git@)|\\.git$", "", target_url) target_url <- gsub(":", "/", target_url, fixed = TRUE) - if (grepl(repo_url, target_url)) { + if (repo_url == target_url) { return(remote) } } From 5d91fa0d4a3df4347df7287c3c66006f8e5d48cd Mon Sep 17 00:00:00 2001 From: Nikolas Burkoff Date: Wed, 10 Nov 2021 12:37:42 +0000 Subject: [PATCH 4/4] Update R/git_tools.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dawid Kałędkowski <6959016+gogonzo@users.noreply.github.com> --- R/git_tools.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/git_tools.R b/R/git_tools.R index ac564f1..b7ef0af 100644 --- a/R/git_tools.R +++ b/R/git_tools.R @@ -186,8 +186,7 @@ install_external_deps <- function(repo_dir, internal_pkg_deps, ...) { get_remote_name <- function(git_repo, repo_url) { # remove the https:// and .git from repo_url - repo_url <- gsub("^.+://", "", repo_url, perl = TRUE) - repo_url <- gsub(".git$", "", repo_url) + repo_url <- gsub("^.+://|.git$", "", repo_url, perl = TRUE) remotes <- git2r::remotes(git_repo)