Skip to content
Browse files

Merge pull request #169 from davidcoallier/install_git

Ability to install directly from a Git repository URL
  • Loading branch information...
2 parents 22477fa + 6a63581 commit ae95e9bf5bc277f239702f125fbadaf59d600a95 @wch wch committed
Showing with 154 additions and 0 deletions.
  1. +1 −0 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +116 −0 R/install-git.r
  4. +36 −0 man/install_git.Rd
View
1 DESCRIPTION
@@ -55,6 +55,7 @@ Collate:
'decompress.r'
'install-github.r'
'install-url.r'
+ 'install-git.r'
'install-version.r'
'install-gitorious.r'
'missing-s3.r'
View
1 NAMESPACE
@@ -35,6 +35,7 @@ export(install_bitbucket)
export(install_github)
export(install_gitorious)
export(install_url)
+export(install_git)
export(install_version)
export(is.package)
export(load_all)
View
116 R/install-git.r
@@ -0,0 +1,116 @@
+#' Install a package from a git repository
+#'
+#' It is important to note that this function requires "Git" to be
+#' installed on your system in order to be used.
+#'
+#' This function is vectorised so you can install multiple packages in
+#' a single command.
+#'
+#' @param git_url Location of package on internet. The url should point to a
+#' public or private repository.
+#' @param name Optional package name, used to provide more informative
+#' messages
+#' @param subdir A sub-directory withing a git repository that may
+#' contain the package we are interested in installing.
+#'
+#' @param git_binary A custom git-binary to use instead of default system's git
+#' version.
+#' @param config Additional configuration argument.
+#' @param ... Other arguments passed on to \code{\link{install}}.
+#' @export
+#' @family package installation
+install_git <- function(git_url, name = NULL, subdir = NULL,
+ git_binary = NULL, config = list(), ...) {
+
+ if (is.null(name)) {
+ name <- rep(list(NULL), length(git_url))
+ }
+
+ invisible(mapply(install_git_single, git_url, name,
+ MoreArgs = list(
+ subdir = subdir, git_binary = git_binary, config = config, ...
+ )
+ ))
+}
+
+#' Install a single package from a git repository
+#'
+#' This function allows you to install a single package from a git repository.
+#'
+#' See \code{\link{install_git}} for more information about the paraemeters.
+install_git_single <- function(git_url, name = NULL, subdir = NULL,
+ git_binary = NULL, config = list(), ...) {
+ if (is.null(name)) {
+ name <- basename(git_url)
+ }
+
+ name <- gsub("\\.git$", "", name)
+
+ message("Preparing installation of ", name, " using the Git-URL: ", git_url)
+
+ # Unique temporary file-name.
+ bundle <- tempfile()
+
+ # \code{git_path} will handle the NULL and return the system default.
+ git_binary_path <- git_path(git_binary)
+
+ # Clone the package file from the git repository.
+ # @TODO: Handle configs, this currently only supports public repos
+ # and repositories with the public SSH key set.
+ request <- system2(
+ git_binary_path,
+ args = c('clone', '--depth', '1', '--no-hardlinks', git_url, bundle),
+ stdout = FALSE, stderr = FALSE
+ )
+
+ # This is only looking for an error code above 0-success
+ if (request > 0) {
+ stop("There seems to be a problem retrieving this Git-URL.", call. = FALSE)
+ }
+
+ pkg_path <- if (is.null(subdir)) bundle else file.path(bundle, subdir)
+ on.exit(unlink(bundle), add = TRUE)
+
+ # Check it's an R package
+ if (!file.exists(file.path(pkg_path, "DESCRIPTION"))) {
+ stop("Does not appear to be an R package", call. = FALSE)
+ }
+
+ config_path <- file.path(pkg_path, "configure")
+ if (file.exists(config_path)) {
+ Sys.chmod(config_path, "755")
+ }
+
+ # Install
+ install(pkg_path, ...)
+}
+
+
+#' Retrieve the current running path of the git binary.
+#' @param git_binary_name The name of the binary depending on the OS.
+git_path <- function(git_binary_name = NULL) {
+ os_type <- .Platform$OS.type
+ binary_name <- NULL
+
+ if (!is.null(git_binary_name)) {
+ if (!file.exists(git_binary_name)) {
+ stop(
+ "The Git binary you specified ",
+ "does not appear to be installed on ",
+ "your system.", call. = FALSE
+ )
+ }
+ git_path <- git_binary_name
+ } else {
+ # Decide on system default.
+ binary_name <- if (os_type == "unix") "git" else "git.exe"
+ git_path <- unname(Sys.which(binary_name))
+
+ if (git_path == "") {
+ stop("Git does not seem to be installed on your system.", call. = FALSE)
+ }
+ }
+
+ # Only using explicit return for clarity.
+ return(git_path)
+}
View
36 man/install_git.Rd
@@ -0,0 +1,36 @@
+\name{install_git}
+\alias{install_git}
+\title{Install a package from a Git repository url.}
+\usage{
+ install_git(git_url, name = NULL, subdir = NULL,
+ git_binary = NULL, config = list(), ...)
+}
+\arguments{
+ \item{git_url}{location of repository containing the package on internet.
+ The git repository url should point to a public git repository or a private
+ git repository you have access to.}
+
+ \item{name}{optional package name, used to provide more
+ informative messages.}
+
+ \item{subdir}{subdirectory within url bundle that
+ contains the R package.}
+
+ \item{git_binary}{specific git binary to use to clone the packages.}
+
+ \item{...}{Other arguments passed on to
+ \code{\link{install}}.}
+}
+\description{
+ This function is vectorised so you can install multiple
+ packages from multiple repositories in a single command.
+}
+\seealso{
+ Other package installation: \code{\link{install}},
+ \code{\link{install_url}},
+ \code{\link{install_bitbucket}},
+ \code{\link{install_github}},
+ \code{\link{install_gitorious}},
+ \code{\link{install_version}}
+}
+

0 comments on commit ae95e9b

Please sign in to comment.
Something went wrong with that request. Please try again.