Skip to content

Commit 910f849

Browse files
committed
New addin: switch repo HTTPS <-> SSH
1 parent 06f938b commit 910f849

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ export(sdd_info)
77
export(sdd_repo_issue)
88
export(sign_in)
99
export(sign_out)
10+
export(switch_repo)
1011
importFrom(gert,git_remote_info)
12+
importFrom(gert,git_remote_set_url)
1113
importFrom(rstudioapi,showDialog)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- New version for academic year 2024-2024. New passwords.
44

5+
- New function `switch_repo()` and new addin to easily switch a GitHub repository between HTTP and SHH remote URL.
6+
57
# BioDataScience 2023.1.0
68

79
- An RStudio addin to check disk space used in `/home/jovyan` is added (useful for SaturnCloud). The corresponding function is `disk_info()`, which allows for testing other disks than the one corresponding to `/home/jovyan` (or all disks with `path = ""`).

R/addins.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ sdd_user_addin <- function()
1414

1515
sign_out_addin <- function()
1616
sign_out()
17+
18+
switch_repo_addin <- function()
19+
invisible(switch_repo())

R/switch_repo.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#' Switch the remote URL of a GitHUb repository between HTTPS and SSH
2+
#'
3+
#' Use this function to easily switch the remote URL of a GitHub repository
4+
#' between HTTPS and SSH. GitHub allows for the two modes, but if the machine is
5+
#' configured in one mode and a clone is created in the other mode, one cannot
6+
#' push or pull. The correction is easy to do at the command line, but this
7+
#' function makes it even easier.
8+
#'
9+
#' @param verbose If `TRUE` (by default), the function produces a message that explain what
10+
#' was done.
11+
#'
12+
#' @return The new remote URL for the GitHub repository is returned.
13+
#' @export
14+
#' @importFrom gert git_remote_info git_remote_set_url
15+
#'
16+
#' @examples
17+
#' # Run only when you know what you are doing: the remote URL is changed!
18+
#' #switch_repo()
19+
switch_repo <- function(verbose = TRUE) {
20+
repo_url <- try(gert::git_remote_info()$url, silent = TRUE)
21+
if (inherits(repo_url, "try-error"))
22+
stop("Cannot get info about current GitHub repository (is one edited?)")
23+
24+
# The two forms are:
25+
# git@github.com:<user_or_org>/<repo>.git
26+
# https://github.com/<user_or_org>/<repo>.git
27+
if (!grepl("^git@github\\.com:.+/.+\\.git$", repo_url) &&
28+
!grepl("^https://github\\.com/.+/.+\\.git$", repo_url))
29+
stop("Unrecognized remote: ", repo_url)
30+
31+
if (grepl("^git@github\\.com:.+/.+\\.git$", repo_url)) {# SSH -> HTTPS
32+
repo_url2 <- sub("^git@github\\.com:(.+/.+\\.git)$",
33+
"https://github.com/\\1", repo_url)
34+
} else {#HTTPS -> SSH
35+
repo_url2 <- sub("^https://github\\.com/(.+/.+\\.git)$",
36+
"git@github.com:\\1", repo_url)
37+
}
38+
39+
res <- try(gert::git_remote_set_url(repo_url2), silent = TRUE)
40+
if (inherits(res, "try-error"))
41+
stop("Error while changhing the remote URL: ", res)
42+
43+
if (isTRUE(verbose))
44+
message("Remote URL changed from: ", repo_url, " to ", repo_url2)
45+
46+
repo_url2
47+
}

inst/rstudio/addins.dcf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ Name: Sign out
2222
Description: Sign out current user
2323
Binding: sign_out_addin
2424
Interactive: true
25+
26+
Name: Switch repo (https <-> ssh)
27+
Description: Switch between HTTPS and SSH for the current GitHub repository
28+
Binding: switch_repo_addin
29+
Interactive: false

man/SciViews-package.Rd renamed to man/BioDataScience-package.Rd

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/switch_repo.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)