Skip to content

Instantly share code, notes, and snippets.

@cderv
Created April 5, 2019 16:21
Show Gist options
  • Save cderv/0edf265f030d01b7072cddaf6418e019 to your computer and use it in GitHub Desktop.
Save cderv/0edf265f030d01b7072cddaf6418e019 to your computer and use it in GitHub Desktop.
update git remote to new url
# Copyright (C) 2018 C. Dervieux
# Licence MIT - https://spdx.org/licenses/MIT.html
#' Update git remote url from R
#'
#' If git remote server is migrated and url is changed, this function allows to easily update
#' the url of local git repo to the new git remote url.
#'
#' This function need `git2r`, `urltools` and `glue` to work.
#'
#' @param repo_path path to local git repository. default to current working directory.
#' @param new_base_url new url of git remote server
#'
#' @author C. Dervieux, @cderv
#'
#' @return invisibly, a dataframe of 1 row and 2 columns : old and new remote url
update_git_remote_url <- function(repo_path = ".", new_base_url) {
stopifnot(!missing(new_base_url))
repo <- git2r::repository(repo_path)
current_remote_url <- git2r::remote_url(repo, "origin")
new_remote_url <- current_remote_url
urltools::scheme(new_remote_url) <- urltools::scheme(new_base_url)
urltools::domain(new_remote_url) <- urltools::domain(new_base_url)
git2r::remote_set_url(repo, "origin", new_remote_url)
stopifnot(identical(git2r::remote_url(repo, "origin"), new_remote_url))
msg <- glue::glue("***",
"remote url updated",
"------------------",
"old: {current_remote_url}",
"new: {new_remote_url}",
"***",
.sep = "\n")
message(msg)
invisible(data.frame(old = current_remote_url, new = git2r::remote_url(repo, "origin")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment