1#' Open local repository
2#'
3#' Returns a pointer to a libgit2 repository object.This function is mainly
4#' for internal use; users should simply reference a repository in gert by
5#' by the path to the directory.
6#'
7#' @export
8#' @param repo The path to the git repository. If the directory is not a
9#' repository, parent directories are considered (see [git_find]). To disable
10#' this search, provide the filepath protected with [I()]. When using this
11#' parameter, always explicitly call by name (i.e. `repo = `) because future
12#' versions of gert may have additional parameters.
13#' @return an pointer to the libgit2 repository
14#' @useDynLib gert R_git_repository_open
15#' @examples
16#' r <- tempfile(pattern = "gert")
17#' git_init(r)
18#' r_ptr <- git_open(r)
19#' r_ptr
20#' git_open(r_ptr)
21#' git_info(r)
22#'
23#' # cleanup
24#' unlink(r, recursive = TRUE)
25git_open <- function(repo = '.'){
26  if(inherits(repo, 'git_repo_ptr')){
27    return(repo)
28  } else if(!is.character(repo)){
29    stop("repo argument must be a filepath or an existing repository object")
30  }
31  search <- !inherits(repo, 'AsIs')
32  path <- normalizePath(path.expand(repo), mustWork = FALSE)
33  out <- .Call(R_git_repository_open, path, search)
34  if(is_rstudio_ide()){
35    cl <- substitute(rstudioapi::executeCommand("vcsRefresh"))
36    do.call(on.exit, list(cl, add = TRUE), envir = parent.frame())
37  }
38  return(out)
39}
40
41is_rstudio_ide <- function(){
42  interactive() && identical(Sys.getenv('RSTUDIO'), '1') &&
43    !nchar(Sys.getenv('Disable_vcsRefresh')) && rstudioapi::isAvailable()
44}
45
46#' @export
47print.git_repo_ptr <- function(x, ...){
48  info <- git_info(x)
49
50  type = "git repository"
51  if(info$bare){
52    type = paste(type, "(bare)")
53  }
54
55  cat(sprintf("<%s>: %s[@%s]\n", type, normalizePath(info$path), info$shorthand))
56}
57
58#' @useDynLib gert R_git_repository_path
59git_repo_path <- function(repo){
60  invisible(.Call(R_git_repository_path, repo))
61}
62