From 013fe486c86397878d1d026f73c1e81ac391aa50 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sat, 9 Dec 2017 22:28:20 +0100 Subject: [PATCH 01/33] render documentation Signed-off-by: Thierry Onkelinx --- man/tree-index-methods.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/tree-index-methods.Rd b/man/tree-index-methods.Rd index 86b6fbe88..5ad601c83 100644 --- a/man/tree-index-methods.Rd +++ b/man/tree-index-methods.Rd @@ -64,7 +64,7 @@ tree_object[1] tree_object[1:3] ## Select all blobs in tree -tree_object[sapply(as(tree_object, 'list'), is_blob)] +tree_object[vapply(as(tree_object, 'list'), is_blob, logical(1))] } } \keyword{methods} From fbbb60bddce982380015c853ee73a3b6518b4299 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sat, 9 Dec 2017 22:38:19 +0100 Subject: [PATCH 02/33] Add is.git() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 3 ++- NAMESPACE | 2 ++ R/is_git.R | 9 +++++++++ man/is.git.Rd | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 R/is_git.R create mode 100644 man/is.git.Rd diff --git a/DESCRIPTION b/DESCRIPTION index ec716d175..25532e915 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,7 @@ NeedsCompilation: yes SystemRequirements: zlib headers and library. OpenSSL headers and library. LibSSH2 (optional on non-Windows) to enable the SSH transport. -Collate: +Collate: 'S4_classes.r' 'blame.r' 'blob.r' @@ -41,6 +41,7 @@ Collate: 'fetch.r' 'git2r.r' 'index.r' + 'is_git.R' 'libgit2.r' 'merge.r' 'note.r' diff --git a/NAMESPACE b/NAMESPACE index 725dadac2..f7a09451a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ S3method(print,git_status) export(config) export(cred_ssh_key) export(cred_token) +export(is.git) export(is_blob) export(is_branch) export(is_commit) @@ -131,5 +132,6 @@ importFrom(graphics,plot.new) importFrom(graphics,plot.window) importFrom(graphics,symbols) importFrom(utils,capture.output) +importFrom(utils,file_test) importFrom(utils,sessionInfo) useDynLib(git2r, .registration=TRUE) diff --git a/R/is_git.R b/R/is_git.R new file mode 100644 index 000000000..d4c1be538 --- /dev/null +++ b/R/is_git.R @@ -0,0 +1,9 @@ +#' Is the path a git repository +#' Checks if a '.git' subdirectory exists in 'path' +#' @param path the path to check +#' @export +#' @return A logical vector with the same length as path +#' @importFrom utils file_test +is.git <- function(path){ + file_test("-d", paste(path, ".git", sep = "/")) +} diff --git a/man/is.git.Rd b/man/is.git.Rd new file mode 100644 index 000000000..9a3a681b9 --- /dev/null +++ b/man/is.git.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_git.R +\name{is.git} +\alias{is.git} +\title{Is the path a git repository +Checks if a '.git' subdirectory exists in 'path'} +\usage{ +is.git(path) +} +\arguments{ +\item{path}{the path to check} +} +\value{ +A logical vector with the same length as path +} +\description{ +Is the path a git repository +Checks if a '.git' subdirectory exists in 'path' +} From 332ca575b1179ad8f637305cda22d49e6ad82bbb Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sat, 9 Dec 2017 23:10:45 +0100 Subject: [PATCH 03/33] Add gitConnection class Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 7 +++++ R/S4_classes.r | 59 ++++++++++++++++++++++++++++++++++++++ man/gitConnection-class.Rd | 20 +++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 man/gitConnection-class.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 25532e915..eaf77fa85 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,6 +12,7 @@ BugReports: https://github.com/ropensci/git2r/issues Maintainer: Stefan Widgren Author: See AUTHORS file. Imports: + assertthat, graphics, utils Depends: diff --git a/NAMESPACE b/NAMESPACE index f7a09451a..3585b9918 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -20,6 +20,7 @@ exportClasses(cred_env) exportClasses(cred_ssh_key) exportClasses(cred_token) exportClasses(cred_user_pass) +exportClasses(gitConnection) exportClasses(git_blame) exportClasses(git_blame_hunk) exportClasses(git_blob) @@ -125,12 +126,18 @@ exportMethods(tree) exportMethods(when) exportMethods(workdir) import(methods) +importFrom(assertthat,assert_that) +importFrom(assertthat,has_name) +importFrom(assertthat,is.string) importFrom(graphics,axis) importFrom(graphics,barplot) importFrom(graphics,par) importFrom(graphics,plot.new) importFrom(graphics,plot.window) importFrom(graphics,symbols) +importFrom(methods,setClass) +importFrom(methods,setClassUnion) +importFrom(methods,setValidity) importFrom(utils,capture.output) importFrom(utils,file_test) importFrom(utils,sessionInfo) diff --git a/R/S4_classes.r b/R/S4_classes.r index 97209e153..90a47ba1e 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -840,3 +840,62 @@ setClass("git_merge_result", conflicts = "logical", sha = "character") ) + +#' @importFrom methods setClassUnion +setClassUnion("gitCredentials", c("NULL", "cred_user_pass", "cred_ssh_key")) + +#' The gitConnection class +#' +#' @section Slots: +#' \describe{ +#' \item{\code{Repository}}{a git repository} +#' \item{\code{LocalPath}}{a subdirectory wihtin the repository} +#' \item{\code{Credentials}}{the credentials for the repository} +#' \item{\code{CommitUser}}{the name of the user how will commit} +#' \item{\code{CommitEmail}}{the email of the user how will commit} +#' } +#' @name gitConnection-class +#' @rdname gitConnection-class +#' @exportClass gitConnection +#' @aliases gitConnection-class +#' @importFrom methods setClass +#' @docType class +setClass( + Class = "gitConnection", + representation = representation( + Repository = "git_repository", + LocalPath = "character", + Credentials = "gitCredentials", + CommitUser = "character", + CommitEmail = "character" + ) +) + +#' @importFrom methods setValidity +#' @importFrom assertthat assert_that is.string has_name +setValidity( + "gitConnection", + function(object){ + assert_that(is.string(object@CommitUser)) + assert_that(is.string(object@CommitEmail)) + root <- paste(object@Repository@path, ".", sep = "/") + root <- check_path(path = root, type = "directory") + full.path <- paste(root, object@LocalPath, sep = "/") + full.path <- check_path(full.path, type = "directory") + if (length(grep(root, full.path)) == 0) { + return( + paste0( + "Wrong local path. '", full.path, + "' is not a subdirectory of '", root, "'" + ) + ) + } + repo <- repository(root) + repo.config <- config(repo) + assert_that(has_name(repo.config$local, "user.name")) + assert_that(has_name(repo.config$local, "user.email")) + assert_that(repo.config$local$user.name == object@CommitUser) + assert_that(repo.config$local$user.email == object@CommitEmail) + return(TRUE) + } +) diff --git a/man/gitConnection-class.Rd b/man/gitConnection-class.Rd new file mode 100644 index 000000000..58a1f8572 --- /dev/null +++ b/man/gitConnection-class.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/S4_classes.r +\docType{class} +\name{gitConnection-class} +\alias{gitConnection-class} +\title{The gitConnection class} +\description{ +The gitConnection class +} +\section{Slots}{ + + \describe{ + \item{\code{Repository}}{a git repository} + \item{\code{LocalPath}}{a subdirectory wihtin the repository} + \item{\code{Credentials}}{the credentials for the repository} + \item{\code{CommitUser}}{the name of the user how will commit} + \item{\code{CommitEmail}}{the email of the user how will commit} + } +} + From 9e00b6da11ebd0321cb4a1e77a9864589629d463 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sat, 9 Dec 2017 23:29:59 +0100 Subject: [PATCH 04/33] Add write_delim_git Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 4 +++ R/write_delim_git.r | 71 ++++++++++++++++++++++++++++++++++++++++++ man/write_delim_git.Rd | 34 ++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 R/write_delim_git.r create mode 100644 man/write_delim_git.Rd diff --git a/DESCRIPTION b/DESCRIPTION index eaf77fa85..8f9c26cb7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -65,4 +65,5 @@ Collate: 'time.r' 'tree.r' 'when.r' + 'write_delim_git.r' RoxygenNote: 6.0.1 diff --git a/NAMESPACE b/NAMESPACE index 3585b9918..9fdc5cf77 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -125,6 +125,7 @@ exportMethods(tags) exportMethods(tree) exportMethods(when) exportMethods(workdir) +exportMethods(write_delim_git) import(methods) importFrom(assertthat,assert_that) importFrom(assertthat,has_name) @@ -137,8 +138,11 @@ importFrom(graphics,plot.window) importFrom(graphics,symbols) importFrom(methods,setClass) importFrom(methods,setClassUnion) +importFrom(methods,setGeneric) +importFrom(methods,setMethod) importFrom(methods,setValidity) importFrom(utils,capture.output) importFrom(utils,file_test) importFrom(utils,sessionInfo) +importFrom(utils,write.table) useDynLib(git2r, .registration=TRUE) diff --git a/R/write_delim_git.r b/R/write_delim_git.r new file mode 100644 index 000000000..9e5c12f93 --- /dev/null +++ b/R/write_delim_git.r @@ -0,0 +1,71 @@ +#' Write a dataframe as a tab delimited file to a git repository and stage it +#' +#' The existing file will be overwritten. +#' @param x the data.frame +#' @param file the name of the file +#' @param connection The path of a git repository or a \code{gitConnection} +#' object +#' @param ... parameters passed to \code{git_connection()} when +#' \code{connection} is a path +#' @return the SHA1 of the file +#' @name write_delim_git +#' @rdname write_delim_git +#' @exportMethod write_delim_git +#' @docType methods +#' @importFrom methods setGeneric +#' @importFrom utils write.table +#' @include S4_classes.r +setGeneric( + name = "write_delim_git", + def = function(x, file, connection, ...){ + standard.generic("write_delim_git") + } +) + +#' @rdname write_delim_git +#' @aliases write_delim_git,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "write_delim_git", + signature = signature(connection = "ANY"), + definition = function(x, file, connection, ...){ + this.connection <- git_connection(repo.path = connection, ...) + write_delim_git(x = x, file = file, connection = this.connection) + } +) + +#' @rdname write_delim_git +#' @aliases write_delim_git,git_connection-methods +#' @importFrom methods setMethod +#' @importFrom assertthat assert_that is.string +setMethod( + f = "write_delim_git", + signature = signature(connection = "gitConnection"), + definition = function(x, file, connection, ...){ + assert_that(inherits(x, "data.frame")) + assert_that(is.string(file)) + + # write the file + filename.full <- paste( + connection@Repository@path, + connection@LocalPath, + file, + sep = "/" + ) + filename.full <- normalizePath( + path = filename.full, + winslash = "/", + mustWork = FALSE + ) + write.table( + x = x, file = filename.full, append = FALSE, + quote = FALSE, sep = "\t", row.names = FALSE, fileEncoding = "UTF-8" + ) + + # stage the file + filename.local <- paste(connection@LocalPath, file, sep = "/") + add(connection@Repository, filename.local) + + return(hashfile(filename.full)) + } +) diff --git a/man/write_delim_git.Rd b/man/write_delim_git.Rd new file mode 100644 index 000000000..5d43650f8 --- /dev/null +++ b/man/write_delim_git.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/write_delim_git.r +\docType{methods} +\name{write_delim_git} +\alias{write_delim_git} +\alias{write_delim_git,ANY,ANY,ANY-method} +\alias{write_delim_git,git_connection-methods} +\alias{write_delim_git,ANY,ANY,gitConnection-method} +\alias{write_delim_git,git_connection-methods} +\title{Write a dataframe as a tab delimited file to a git repository and stage it} +\usage{ +write_delim_git(x, file, connection, ...) + +\S4method{write_delim_git}{ANY,ANY,ANY}(x, file, connection, ...) + +\S4method{write_delim_git}{ANY,ANY,gitConnection}(x, file, connection, ...) +} +\arguments{ +\item{x}{the data.frame} + +\item{file}{the name of the file} + +\item{connection}{The path of a git repository or a \code{gitConnection} +object} + +\item{...}{parameters passed to \code{git_connection()} when +\code{connection} is a path} +} +\value{ +the SHA1 of the file +} +\description{ +The existing file will be overwritten. +} From 8b63cf6d8378d0e1e5ab57781e1e032d81ba8632 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:08:05 +0100 Subject: [PATCH 05/33] Add git_connection() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 2 + R/git_connection.r | 101 +++++++++++++++++++++++++++++++++++++ man/gitConnection-class.Rd | 28 +++++++++- 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 R/git_connection.r diff --git a/DESCRIPTION b/DESCRIPTION index 8f9c26cb7..85b980b0d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,6 +41,7 @@ Collate: 'diff.r' 'fetch.r' 'git2r.r' + 'git_connection.r' 'index.r' 'is_git.R' 'libgit2.r' diff --git a/NAMESPACE b/NAMESPACE index 9fdc5cf77..560904122 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ S3method(print,git_status) export(config) export(cred_ssh_key) export(cred_token) +export(git_connection) export(is.git) export(is_blob) export(is_branch) @@ -136,6 +137,7 @@ importFrom(graphics,par) importFrom(graphics,plot.new) importFrom(graphics,plot.window) importFrom(graphics,symbols) +importFrom(methods,new) importFrom(methods,setClass) importFrom(methods,setClassUnion) importFrom(methods,setGeneric) diff --git a/R/git_connection.r b/R/git_connection.r new file mode 100644 index 000000000..334d5fafc --- /dev/null +++ b/R/git_connection.r @@ -0,0 +1,101 @@ +#' Open a git connection +#' @name git_connection +#' @rdname gitConnection-class +#' @param repo.path The path of the root of the repository +#' @param local.path A path within the repository +#' @param key Optional: the path to a private ssh key. The public key is assumed +#' to have the same path with a '.pub' extension. Using in case of ssh +#' authentication. +#' @param username The optional username used in case of https authentication. +#' Ignored when \code{key} is provided. +#' @param password The password required for the ssh key or the username. Should +#' be missing when the ssh-key doesn't require a password. +#' @param commit.user the name of the user how will commit +#' @param commit.email the email of the user how will commit +#' @export +#' @importFrom methods new +#' @importFrom assertthat assert_that is.string +#' @include S4_classes.r +git_connection <- function( + repo.path, + local.path = ".", + key, + username, + password, + commit.user, + commit.email +){ + assert_that(is.string(local.path)) + assert_that(is.string(commit.user)) + assert_that(is.string(commit.email)) + repo.path <- is.git(path = repo.path) + repo <- repository(repo.path) + config(repo, user.name = commit.user, user.email = commit.email) + + if (missing(key) & missing(username)) { + return( + new( + "gitConnection", + Repository = repo, + LocalPath = local.path, + Credentials = NULL, + CommitUser = commit.user, + CommitEmail = commit.email + ) + ) + } + + if (missing(username)) { + assert_that(is.string(key)) + + if (missing(password)) { + return( + new( + "gitConnection", + Repository = repo, + LocalPath = local.path, + Credentials = cred_ssh_key( + publickey = paste0(key, ".pub"), + privatekey = key + ), + CommitUser = commit.user, + CommitEmail = commit.email + ) + ) + } + + assert_that(is.string(password)) + return( + new( + "gitConnection", + Repository = repo, + LocalPath = local.path, + Credentials = cred_ssh_key( + publickey = paste0(key, ".pub"), + privatekey = key, + passphrase = password + ), + CommitUser = commit.user, + CommitEmail = commit.email + ) + ) + } + + assert_that(is.string(username)) + assert_that(username != "") + assert_that(is.string(password)) + assert_that(password != "") + return( + new( + "gitConnection", + Repository = repo, + LocalPath = local.path, + Credentials = cred_user_pass( + username = username, + password = password + ), + CommitUser = commit.user, + CommitEmail = commit.email + ) + ) +} diff --git a/man/gitConnection-class.Rd b/man/gitConnection-class.Rd index 58a1f8572..8f56db9c3 100644 --- a/man/gitConnection-class.Rd +++ b/man/gitConnection-class.Rd @@ -1,11 +1,37 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/S4_classes.r +% Please edit documentation in R/S4_classes.r, R/git_connection.r \docType{class} \name{gitConnection-class} \alias{gitConnection-class} +\alias{git_connection} \title{The gitConnection class} +\usage{ +git_connection(repo.path, local.path = ".", key, username, password, + commit.user, commit.email) +} +\arguments{ +\item{repo.path}{The path of the root of the repository} + +\item{local.path}{A path within the repository} + +\item{key}{Optional: the path to a private ssh key. The public key is assumed +to have the same path with a '.pub' extension. Using in case of ssh +authentication.} + +\item{username}{The optional username used in case of https authentication. +Ignored when \code{key} is provided.} + +\item{password}{The password required for the ssh key or the username. Should +be missing when the ssh-key doesn't require a password.} + +\item{commit.user}{the name of the user how will commit} + +\item{commit.email}{the email of the user how will commit} +} \description{ The gitConnection class + +Open a git connection } \section{Slots}{ From 2faf3898b61db77d1cab3090ab6d3e97381cd6ef Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:13:51 +0100 Subject: [PATCH 06/33] Add read_delim_git() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 2 ++ R/read_delim_git.r | 50 +++++++++++++++++++++++++++++++++++++++++++ R/write_delim_git.r | 2 +- man/read_delim_git.Rd | 29 +++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 R/read_delim_git.r create mode 100644 man/read_delim_git.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 85b980b0d..bae23a8a6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -53,6 +53,7 @@ Collate: 'punch_card.r' 'refspec.r' 'push.r' + 'read_delim_git.r' 'reference.r' 'reflog.r' 'remote.r' diff --git a/NAMESPACE b/NAMESPACE index 560904122..dab6ea185 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -101,6 +101,7 @@ exportMethods(plot) exportMethods(pull) exportMethods(punch_card) exportMethods(push) +exportMethods(read_delim_git) exportMethods(references) exportMethods(reflog) exportMethods(remote_add) @@ -145,6 +146,7 @@ importFrom(methods,setMethod) importFrom(methods,setValidity) importFrom(utils,capture.output) importFrom(utils,file_test) +importFrom(utils,read.delim) importFrom(utils,sessionInfo) importFrom(utils,write.table) useDynLib(git2r, .registration=TRUE) diff --git a/R/read_delim_git.r b/R/read_delim_git.r new file mode 100644 index 000000000..7bfa0c257 --- /dev/null +++ b/R/read_delim_git.r @@ -0,0 +1,50 @@ +#' Read a tab delimited file from a git repository +#' @inheritParams write_delim_git +#' @name read_delim_git +#' @rdname read_delim_git +#' @exportMethod read_delim_git +#' @docType methods +#' @importFrom methods setGeneric +#' @include git_connection.r +setGeneric( + name = "read_delim_git", + def = function(file, connection, ...){ + standard.generic("read_delim_git") + } +) + +#' @rdname read_delim_git +#' @aliases read_delim_git,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "read_delim_git", + signature = signature(connection = "ANY"), + definition = function(file, connection, ...){ + this.connection <- git_connection(repo.path = connection, ...) + read_delim_git(file = file, connection = this.connection) + } +) + +#' @rdname read_delim_git +#' @aliases read_delim_git,git_connection-methods +#' @importFrom methods setMethod +#' @importFrom utils read.delim +#' @importFrom assertthat assert_that is.string +setMethod( + f = "read_delim_git", + signature = signature(connection = "gitConnection"), + definition = function(file, connection, ...){ + assert_that(is.string(file)) + + filename <- normalizePath( + sprintf( + "%s/%s/%s", + connection@Repository@path, + connection@LocalPath, + file + ), + mustWork = TRUE + ) + return(read.delim(filename, stringsAsFactors = FALSE)) + } +) diff --git a/R/write_delim_git.r b/R/write_delim_git.r index 9e5c12f93..a4b337e99 100644 --- a/R/write_delim_git.r +++ b/R/write_delim_git.r @@ -14,7 +14,7 @@ #' @docType methods #' @importFrom methods setGeneric #' @importFrom utils write.table -#' @include S4_classes.r +#' @include git_connection.r setGeneric( name = "write_delim_git", def = function(x, file, connection, ...){ diff --git a/man/read_delim_git.Rd b/man/read_delim_git.Rd new file mode 100644 index 000000000..a0f87d6f0 --- /dev/null +++ b/man/read_delim_git.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_delim_git.r +\docType{methods} +\name{read_delim_git} +\alias{read_delim_git} +\alias{read_delim_git,ANY,ANY-method} +\alias{read_delim_git,git_connection-methods} +\alias{read_delim_git,ANY,gitConnection-method} +\alias{read_delim_git,git_connection-methods} +\title{Read a tab delimited file from a git repository} +\usage{ +read_delim_git(file, connection, ...) + +\S4method{read_delim_git}{ANY,ANY}(file, connection, ...) + +\S4method{read_delim_git}{ANY,gitConnection}(file, connection, ...) +} +\arguments{ +\item{file}{the name of the file} + +\item{connection}{The path of a git repository or a \code{gitConnection} +object} + +\item{...}{parameters passed to \code{git_connection()} when +\code{connection} is a path} +} +\description{ +Read a tab delimited file from a git repository +} From 40d4fa4136f67996c937a8b8c9853ae826170120 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:21:39 +0100 Subject: [PATCH 07/33] Add list_files_git() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 3 +++ R/list_files_git.r | 54 +++++++++++++++++++++++++++++++++++++++++++ man/list_files_git.Rd | 36 +++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 R/list_files_git.r create mode 100644 man/list_files_git.Rd diff --git a/DESCRIPTION b/DESCRIPTION index bae23a8a6..70dcdcbea 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,6 +45,7 @@ Collate: 'index.r' 'is_git.R' 'libgit2.r' + 'list_files_git.r' 'merge.r' 'note.r' 'odb.r' diff --git a/NAMESPACE b/NAMESPACE index dab6ea185..c4445dc4c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -87,6 +87,7 @@ exportMethods(is_local) exportMethods(is_merge) exportMethods(is_shallow) exportMethods(length) +exportMethods(list_files_git) exportMethods(lookup) exportMethods(merge) exportMethods(merge_base) @@ -131,7 +132,9 @@ exportMethods(write_delim_git) import(methods) importFrom(assertthat,assert_that) importFrom(assertthat,has_name) +importFrom(assertthat,is.flag) importFrom(assertthat,is.string) +importFrom(assertthat,noNA) importFrom(graphics,axis) importFrom(graphics,barplot) importFrom(graphics,par) diff --git a/R/list_files_git.r b/R/list_files_git.r new file mode 100644 index 000000000..314d64f8d --- /dev/null +++ b/R/list_files_git.r @@ -0,0 +1,54 @@ +#' List the files in a path of a git repository +#' @inheritParams write_delim_git +#' @inheritParams base::list.files +#' @name list_files_git +#' @rdname list_files_git +#' @exportMethod list_files_git +#' @docType methods +#' @importFrom methods setGeneric +#' @include git_connection.r +setGeneric( + name = "list_files_git", + def = function(connection, pattern = NULL, full.names = FALSE, ...){ + standard.generic("list_files_git") + } +) + +#' @rdname list_files_git +#' @aliases list_files_git,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "list_files_git", + signature = signature(connection = "ANY"), + definition = function(connection, pattern = NULL, full.names = FALSE, ...){ + this.connection <- git_connection(repo.path = connection, ...) + list_files_git( + connection = this.connection, + pattern = pattern, + full.names = full.names + ) + } +) + +#' @rdname list_files_git +#' @aliases list_files_git,git_connection-methods +#' @importFrom methods setMethod +#' @importFrom assertthat assert_that is.string is.flag noNA +setMethod( + f = "list_files_git", + signature = signature(connection = "gitConnection"), + definition = function(connection, pattern = NULL, full.names = FALSE, ...){ + if (!is.null(pattern)) { + assert_that(is.string(pattern)) + } + assert_that(is.flag(full.names)) + assert_that(noNA(full.names)) + + full.path <- sprintf( + "%s/%s", + connection@Repository@path, + connection@LocalPath + ) + list.files(path = full.path, pattern = pattern, full.names = full.names) + } +) diff --git a/man/list_files_git.Rd b/man/list_files_git.Rd new file mode 100644 index 000000000..fae89d5d2 --- /dev/null +++ b/man/list_files_git.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/list_files_git.r +\docType{methods} +\name{list_files_git} +\alias{list_files_git} +\alias{list_files_git,ANY-method} +\alias{list_files_git,git_connection-methods} +\alias{list_files_git,gitConnection-method} +\alias{list_files_git,git_connection-methods} +\title{List the files in a path of a git repository} +\usage{ +list_files_git(connection, pattern = NULL, full.names = FALSE, ...) + +\S4method{list_files_git}{ANY}(connection, pattern = NULL, + full.names = FALSE, ...) + +\S4method{list_files_git}{gitConnection}(connection, pattern = NULL, + full.names = FALSE, ...) +} +\arguments{ +\item{connection}{The path of a git repository or a \code{gitConnection} +object} + +\item{pattern}{an optional \link{regular expression}. Only file names + which match the regular expression will be returned.} + +\item{full.names}{a logical value. If \code{TRUE}, the directory + path is prepended to the file names to give a relative file path. + If \code{FALSE}, the file names (rather than paths) are returned.} + +\item{...}{parameters passed to \code{git_connection()} when +\code{connection} is a path} +} +\description{ +List the files in a path of a git repository +} From ae3601b4eebbfe213da9d6c7ce894b7cd99da324 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:30:17 +0100 Subject: [PATCH 08/33] Add remove_files_git() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 1 + R/remove_files_git.r | 66 +++++++++++++++++++++++++++++++++++++++++ man/remove_files_git.Rd | 30 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 R/remove_files_git.r create mode 100644 man/remove_files_git.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 70dcdcbea..15b267eb5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -58,6 +58,7 @@ Collate: 'reference.r' 'reflog.r' 'remote.r' + 'remove_files_git.r' 'repository.r' 'reset.r' 'revparse.r' diff --git a/NAMESPACE b/NAMESPACE index c4445dc4c..79e3cc897 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -112,6 +112,7 @@ exportMethods(remote_rename) exportMethods(remote_set_url) exportMethods(remote_url) exportMethods(remotes) +exportMethods(remove_files_git) exportMethods(repository) exportMethods(reset) exportMethods(revparse_single) diff --git a/R/remove_files_git.r b/R/remove_files_git.r new file mode 100644 index 000000000..9becfec2f --- /dev/null +++ b/R/remove_files_git.r @@ -0,0 +1,66 @@ +#' Remove all the files in a path of a git repository +#' +#' @inheritParams write_delim_git +#' @inheritParams base::list.files +#' @name remove_files_git +#' @rdname remove_files_git +#' @exportMethod remove_files_git +#' @docType methods +#' @importFrom methods setGeneric +#' @include list_files_git.r +setGeneric( + name = "remove_files_git", + def = function(connection, pattern = NULL, ...){ + standard.generic("remove_files_git") + } +) + +#' @rdname remove_files_git +#' @aliases remove_files_git,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "remove_files_git", + signature = signature(connection = "ANY"), + definition = function(connection, pattern = NULL, ...){ + this.connection <- git_connection( + repo.path = connection, + local.path = list(...)$path, + ... + ) + remove_files_git(connection = this.connection, pattern = pattern) + } +) + +#' @rdname remove_files_git +#' @aliases remove_files_git,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "remove_files_git", + signature = signature(connection = "gitConnection"), + definition = function(connection, pattern = NULL, ...){ + to.remove <- list_files_git( + connection = connection, + pattern = pattern, + full.names = TRUE + ) + + success <- file.remove(to.remove) + + if (length(success) > 0 && !all(success)) { + stop( + "Error cleaning existing files in the git repository. Repository: '", + connection@Repository@path, "', Path: '", connection@LocalPath, + "', pattern: '", pattern, "'" + ) + } + if (any(success)) { + to.stage <- gsub( + paste0("^", connection@Repository@path, "/"), + "", + to.remove[success] + ) + add(repo = connection@Repository, path = to.stage) + } + return(invisible(TRUE)) + } +) diff --git a/man/remove_files_git.Rd b/man/remove_files_git.Rd new file mode 100644 index 000000000..bb814e122 --- /dev/null +++ b/man/remove_files_git.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/remove_files_git.r +\docType{methods} +\name{remove_files_git} +\alias{remove_files_git} +\alias{remove_files_git,ANY-method} +\alias{remove_files_git,git_connection-methods} +\alias{remove_files_git,gitConnection-method} +\alias{remove_files_git,git_connection-methods} +\title{Remove all the files in a path of a git repository} +\usage{ +remove_files_git(connection, pattern = NULL, ...) + +\S4method{remove_files_git}{ANY}(connection, pattern = NULL, ...) + +\S4method{remove_files_git}{gitConnection}(connection, pattern = NULL, ...) +} +\arguments{ +\item{connection}{The path of a git repository or a \code{gitConnection} +object} + +\item{pattern}{an optional \link{regular expression}. Only file names + which match the regular expression will be returned.} + +\item{...}{parameters passed to \code{git_connection()} when +\code{connection} is a path} +} +\description{ +Remove all the files in a path of a git repository +} From b9c5669a2543dd902b1946456bfa1a95ddf0e5dc Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:46:35 +0100 Subject: [PATCH 09/33] Add Thierry Onkelinx as author Signed-off-by: Thierry Onkelinx --- R/S4_classes.r | 1 + R/git_connection.r | 1 + R/is_git.R | 1 + R/list_files_git.r | 1 + R/read_delim_git.r | 1 + R/remove_files_git.r | 1 + R/write_delim_git.r | 1 + inst/AUTHORS | 1 + man-roxygen/thierry.r | 1 + man/gitConnection-class.Rd | 5 +++++ man/is.git.Rd | 3 +++ man/list_files_git.Rd | 3 +++ man/read_delim_git.Rd | 3 +++ man/remove_files_git.Rd | 3 +++ man/write_delim_git.Rd | 3 +++ 15 files changed, 29 insertions(+) create mode 100644 man-roxygen/thierry.r diff --git a/R/S4_classes.r b/R/S4_classes.r index 90a47ba1e..8b09b1bf3 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -860,6 +860,7 @@ setClassUnion("gitCredentials", c("NULL", "cred_user_pass", "cred_ssh_key")) #' @aliases gitConnection-class #' @importFrom methods setClass #' @docType class +#' @template thierry setClass( Class = "gitConnection", representation = representation( diff --git a/R/git_connection.r b/R/git_connection.r index 334d5fafc..90980e91b 100644 --- a/R/git_connection.r +++ b/R/git_connection.r @@ -16,6 +16,7 @@ #' @importFrom methods new #' @importFrom assertthat assert_that is.string #' @include S4_classes.r +#' @template thierry git_connection <- function( repo.path, local.path = ".", diff --git a/R/is_git.R b/R/is_git.R index d4c1be538..de9579aaf 100644 --- a/R/is_git.R +++ b/R/is_git.R @@ -4,6 +4,7 @@ #' @export #' @return A logical vector with the same length as path #' @importFrom utils file_test +#' @template thierry is.git <- function(path){ file_test("-d", paste(path, ".git", sep = "/")) } diff --git a/R/list_files_git.r b/R/list_files_git.r index 314d64f8d..7d33f969b 100644 --- a/R/list_files_git.r +++ b/R/list_files_git.r @@ -7,6 +7,7 @@ #' @docType methods #' @importFrom methods setGeneric #' @include git_connection.r +#' @template thierry setGeneric( name = "list_files_git", def = function(connection, pattern = NULL, full.names = FALSE, ...){ diff --git a/R/read_delim_git.r b/R/read_delim_git.r index 7bfa0c257..e61550317 100644 --- a/R/read_delim_git.r +++ b/R/read_delim_git.r @@ -6,6 +6,7 @@ #' @docType methods #' @importFrom methods setGeneric #' @include git_connection.r +#' @template thierry setGeneric( name = "read_delim_git", def = function(file, connection, ...){ diff --git a/R/remove_files_git.r b/R/remove_files_git.r index 9becfec2f..8d72815f2 100644 --- a/R/remove_files_git.r +++ b/R/remove_files_git.r @@ -8,6 +8,7 @@ #' @docType methods #' @importFrom methods setGeneric #' @include list_files_git.r +#' @template thierry setGeneric( name = "remove_files_git", def = function(connection, pattern = NULL, ...){ diff --git a/R/write_delim_git.r b/R/write_delim_git.r index a4b337e99..8f959316e 100644 --- a/R/write_delim_git.r +++ b/R/write_delim_git.r @@ -15,6 +15,7 @@ #' @importFrom methods setGeneric #' @importFrom utils write.table #' @include git_connection.r +#' @template thierry setGeneric( name = "write_delim_git", def = function(x, file, connection, ...){ diff --git a/inst/AUTHORS b/inst/AUTHORS index 5f9710344..d4b9db5b6 100644 --- a/inst/AUTHORS +++ b/inst/AUTHORS @@ -14,6 +14,7 @@ Jim Hester List references in a remote repository Karthik Ram Summarize contributions to a repository Various fixes of code and documentation Stefan Widgren Most of the git2r bindings to libgit2 +Thierry Onkelinx Functions to work with git data repositories Other contributors to git2r diff --git a/man-roxygen/thierry.r b/man-roxygen/thierry.r new file mode 100644 index 000000000..6fee92567 --- /dev/null +++ b/man-roxygen/thierry.r @@ -0,0 +1 @@ +#' @author Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} diff --git a/man/gitConnection-class.Rd b/man/gitConnection-class.Rd index 8f56db9c3..46cb15862 100644 --- a/man/gitConnection-class.Rd +++ b/man/gitConnection-class.Rd @@ -44,3 +44,8 @@ Open a git connection } } +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} + +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} diff --git a/man/is.git.Rd b/man/is.git.Rd index 9a3a681b9..05bb3272f 100644 --- a/man/is.git.Rd +++ b/man/is.git.Rd @@ -17,3 +17,6 @@ A logical vector with the same length as path Is the path a git repository Checks if a '.git' subdirectory exists in 'path' } +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} diff --git a/man/list_files_git.Rd b/man/list_files_git.Rd index fae89d5d2..5400e6a92 100644 --- a/man/list_files_git.Rd +++ b/man/list_files_git.Rd @@ -34,3 +34,6 @@ object} \description{ List the files in a path of a git repository } +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} diff --git a/man/read_delim_git.Rd b/man/read_delim_git.Rd index a0f87d6f0..58baf387b 100644 --- a/man/read_delim_git.Rd +++ b/man/read_delim_git.Rd @@ -27,3 +27,6 @@ object} \description{ Read a tab delimited file from a git repository } +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} diff --git a/man/remove_files_git.Rd b/man/remove_files_git.Rd index bb814e122..abfed4c54 100644 --- a/man/remove_files_git.Rd +++ b/man/remove_files_git.Rd @@ -28,3 +28,6 @@ object} \description{ Remove all the files in a path of a git repository } +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} diff --git a/man/write_delim_git.Rd b/man/write_delim_git.Rd index 5d43650f8..33a27e01b 100644 --- a/man/write_delim_git.Rd +++ b/man/write_delim_git.Rd @@ -32,3 +32,6 @@ the SHA1 of the file \description{ The existing file will be overwritten. } +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} From 888329e110abf445d6bc4b8cfdaa37cb38b3d5a0 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:50:29 +0100 Subject: [PATCH 10/33] Add git_sha() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + NAMESPACE | 2 ++ R/git_sha.r | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ man/git_sha.Rd | 32 +++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 R/git_sha.r create mode 100644 man/git_sha.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 15b267eb5..006c5707d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,6 +42,7 @@ Collate: 'fetch.r' 'git2r.r' 'git_connection.r' + 'git_sha.r' 'index.r' 'is_git.R' 'libgit2.r' diff --git a/NAMESPACE b/NAMESPACE index 79e3cc897..23ea80c90 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -72,6 +72,7 @@ exportMethods(diff) exportMethods(discover_repository) exportMethods(fetch) exportMethods(fetch_heads) +exportMethods(git_sha) exportMethods(hash) exportMethods(hashfile) exportMethods(head) @@ -151,6 +152,7 @@ importFrom(methods,setValidity) importFrom(utils,capture.output) importFrom(utils,file_test) importFrom(utils,read.delim) +importFrom(utils,read.table) importFrom(utils,sessionInfo) importFrom(utils,write.table) useDynLib(git2r, .registration=TRUE) diff --git a/R/git_sha.r b/R/git_sha.r new file mode 100644 index 000000000..5347b437e --- /dev/null +++ b/R/git_sha.r @@ -0,0 +1,63 @@ +#' Get the SHA of the files at the HEAD +#' @inheritParams write_delim_git +#' @name git_sha +#' @rdname git_sha +#' @exportMethod git_sha +#' @docType methods +#' @importFrom methods setGeneric +#' @include git_connection.r +#' @template thierry +setGeneric( + name = "git_sha", + def = function(file, connection, ...){ + standard.generic("git_sha") + } +) + +#' @rdname git_sha +#' @aliases git_sha,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "git_sha", + signature = signature(connection = "ANY"), + definition = function(file, connection, ...){ + this.connection <- git_connection(repo.path = connection, ...) + git_sha(file = file, connection = this.connection) + } +) + +#' @rdname git_sha +#' @aliases git_sha,git_connection-methods +#' @importFrom methods setMethod +#' @importFrom assertthat assert_that is.string +#' @importFrom utils read.table +setMethod( + f = "git_sha", + signature = signature(connection = "gitConnection"), + definition = function(file, connection, ...){ + assert_that(is.string(file)) + + old.wd <- getwd() + setwd(connection@Repository@path) + blobs <- system( + paste( + "git ls-tree -r HEAD", + connection@LocalPath + ), + intern = TRUE + ) + setwd(old.wd) + blobs <- read.table( + textConnection(paste(blobs, collapse = "\n")), + header = FALSE, + sep = "\t", + col.names = c("SHA", "Path") + ) + blobs$File <- basename(blobs$Path) + blobs <- blobs[blobs$File %in% file, ] + blobs$Path <- dirname(blobs$Path) + blobs$SHA <- gsub("^.*blob ", "", blobs$SHA) + + return(blobs) + } +) diff --git a/man/git_sha.Rd b/man/git_sha.Rd new file mode 100644 index 000000000..e83f7b614 --- /dev/null +++ b/man/git_sha.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/git_sha.r +\docType{methods} +\name{git_sha} +\alias{git_sha} +\alias{git_sha,ANY,ANY-method} +\alias{git_sha,git_connection-methods} +\alias{git_sha,ANY,gitConnection-method} +\alias{git_sha,git_connection-methods} +\title{Get the SHA of the files at the HEAD} +\usage{ +git_sha(file, connection, ...) + +\S4method{git_sha}{ANY,ANY}(file, connection, ...) + +\S4method{git_sha}{ANY,gitConnection}(file, connection, ...) +} +\arguments{ +\item{file}{the name of the file} + +\item{connection}{The path of a git repository or a \code{gitConnection} +object} + +\item{...}{parameters passed to \code{git_connection()} when +\code{connection} is a path} +} +\description{ +Get the SHA of the files at the HEAD +} +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} From 9ab7f0e4fa5d823317321c89db7a421c50c2c6e9 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 13:59:00 +0100 Subject: [PATCH 11/33] Add git_recent() Signed-off-by: Thierry Onkelinx --- .Rbuildignore | 1 + DESCRIPTION | 1 + NAMESPACE | 1 + R/git_recent.r | 61 +++++++++++++++++++++++++++++++++++++++++++++++ man/git_recent.Rd | 32 +++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 R/git_recent.r create mode 100644 man/git_recent.Rd diff --git a/.Rbuildignore b/.Rbuildignore index ed485bb90..f6acda6e3 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -16,3 +16,4 @@ ^\.Rproj\.user$ ^windows ^.*\.a$ +^man-roxygen$ diff --git a/DESCRIPTION b/DESCRIPTION index 006c5707d..aee3e43d0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,6 +42,7 @@ Collate: 'fetch.r' 'git2r.r' 'git_connection.r' + 'git_recent.r' 'git_sha.r' 'index.r' 'is_git.R' diff --git a/NAMESPACE b/NAMESPACE index 23ea80c90..3fa957bf4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -72,6 +72,7 @@ exportMethods(diff) exportMethods(discover_repository) exportMethods(fetch) exportMethods(fetch_heads) +exportMethods(git_recent) exportMethods(git_sha) exportMethods(hash) exportMethods(hashfile) diff --git a/R/git_recent.r b/R/git_recent.r new file mode 100644 index 000000000..7f017072c --- /dev/null +++ b/R/git_recent.r @@ -0,0 +1,61 @@ +#' Get the info from the latest commit of a file +#' @inheritParams write_delim_git +#' @name git_recent +#' @rdname git_recent +#' @exportMethod git_recent +#' @docType methods +#' @importFrom methods setGeneric +#' @include git_connection.r +#' @template thierry +setGeneric( + name = "git_recent", + def = function(file, connection, ...){ + standard.generic("git_recent") + } +) + +#' @rdname git_recent +#' @aliases git_recent,git_connection-methods +#' @importFrom methods setMethod +setMethod( + f = "git_recent", + signature = signature(connection = "ANY"), + definition = function(file, connection, ...){ + this.connection <- git_connection(repo.path = connection, ...) + git_recent(file = file, connection = this.connection) + } +) + +#' @rdname git_recent +#' @aliases git_recent,git_connection-methods +#' @importFrom methods setMethod +#' @importFrom assertthat assert_that is.string +setMethod( + f = "git_recent", + signature = signature(connection = "gitConnection"), + definition = function(file, connection, ...){ + assert_that(is.string(file)) + + old.wd <- getwd() + setwd(connection@Repository@path) + commit.info <- system( + paste0( + "git log -n 1 --date=iso ", + connection@LocalPath, "/", file + ), + intern = TRUE + ) + setwd(old.wd) + date <- commit.info[grep("^Date:", commit.info)] + date <- as.POSIXct(date, format = "Date: %F %T %z") + commit <- commit.info[grep("^commit", commit.info)] + commit <- gsub("^commit ", "", commit) + author <- commit.info[grep("^Author:", commit.info)] + author <- gsub("^Author: ", "", author) + return(list( + Commit = commit, + Author = author, + Date = date + )) + } +) diff --git a/man/git_recent.Rd b/man/git_recent.Rd new file mode 100644 index 000000000..f98b22bd3 --- /dev/null +++ b/man/git_recent.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/git_recent.r +\docType{methods} +\name{git_recent} +\alias{git_recent} +\alias{git_recent,ANY,ANY-method} +\alias{git_recent,git_connection-methods} +\alias{git_recent,ANY,gitConnection-method} +\alias{git_recent,git_connection-methods} +\title{Get the info from the latest commit of a file} +\usage{ +git_recent(file, connection, ...) + +\S4method{git_recent}{ANY,ANY}(file, connection, ...) + +\S4method{git_recent}{ANY,gitConnection}(file, connection, ...) +} +\arguments{ +\item{file}{the name of the file} + +\item{connection}{The path of a git repository or a \code{gitConnection} +object} + +\item{...}{parameters passed to \code{git_connection()} when +\code{connection} is a path} +} +\description{ +Get the info from the latest commit of a file +} +\author{ +Thierry Onkelinx, \email{thierry.onkelinx@inbo.be} +} From aea93d7bf7fc9b576a0233425c44f2664e436a23 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:21:12 +0100 Subject: [PATCH 12/33] Fix git_connection() Signed-off-by: Thierry Onkelinx --- R/git_connection.r | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/git_connection.r b/R/git_connection.r index 90980e91b..ed58284d6 100644 --- a/R/git_connection.r +++ b/R/git_connection.r @@ -29,7 +29,10 @@ git_connection <- function( assert_that(is.string(local.path)) assert_that(is.string(commit.user)) assert_that(is.string(commit.email)) - repo.path <- is.git(path = repo.path) + assert_that( + is.git(path = repo.path), + msg = paste(repo.path, "is not a git repo") + ) repo <- repository(repo.path) config(repo, user.name = commit.user, user.email = commit.email) From 98329ff880d568b6f82d70dbc19926226ab8c66c Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:21:53 +0100 Subject: [PATCH 13/33] Fix gitConnection class Signed-off-by: Thierry Onkelinx --- R/S4_classes.r | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/S4_classes.r b/R/S4_classes.r index 8b09b1bf3..813aa5598 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -874,15 +874,16 @@ setClass( #' @importFrom methods setValidity #' @importFrom assertthat assert_that is.string has_name +#' @importFrom utils file_test setValidity( "gitConnection", function(object){ assert_that(is.string(object@CommitUser)) assert_that(is.string(object@CommitEmail)) root <- paste(object@Repository@path, ".", sep = "/") - root <- check_path(path = root, type = "directory") + assert_that(file_test("-d", root)) full.path <- paste(root, object@LocalPath, sep = "/") - full.path <- check_path(full.path, type = "directory") + assert_that(file_test("-d", full.path)) if (length(grep(root, full.path)) == 0) { return( paste0( From e9e513af25813e0365c69d141cef9f4b1852ef01 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:26:58 +0100 Subject: [PATCH 14/33] test git_connection() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + tests/testthat.R | 4 + tests/testthat/test_git_connection.R | 167 +++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test_git_connection.R diff --git a/DESCRIPTION b/DESCRIPTION index aee3e43d0..d23d601bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Depends: R (>= 3.0.2), methods Suggests: + testthat, getPass Type: Package LazyData: true diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 000000000..7e967cd8d --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(git2r) + +test_check("git2r") diff --git a/tests/testthat/test_git_connection.R b/tests/testthat/test_git_connection.R new file mode 100644 index 000000000..876ec9776 --- /dev/null +++ b/tests/testthat/test_git_connection.R @@ -0,0 +1,167 @@ +context("git_connection") + +connection <- tempfile(pattern = "git2r-") +commit.user <- "me" +commit.email <- "me@me.com" + +# test repo.path +expect_error( + git_connection( + repo.path = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "is not a git repo" +) +dir.create(connection) +expect_error( + git_connection( + repo.path = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "is not a git repo" +) +repo <- git2r::init(connection) + +#test commit.user +expect_error( + git_connection( + repo.path = connection, + commit.user = 1, + commit.email = commit.email + ), + "commit.user is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + commit.user = NA, + commit.email = commit.email + ), + "commit.user is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + commit.user = rep(commit.user, 2), + commit.email = commit.email + ), + "commit.user is not a string \\(a length one character vector\\)\\." +) + +# test commit.email +expect_error( + git_connection( + repo.path = connection, + commit.user = commit.user, + commit.email = 1 + ), + "commit.email is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + commit.user = commit.user, + commit.email = NA + ), + "commit.email is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + commit.user = commit.user, + commit.email = rep(commit.email, 2) + ), + "commit.email is not a string \\(a length one character vector\\)\\." +) + +expect_is( + git.connection <- git_connection( + repo.path = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "gitConnection" +) +expect_identical( + git2r::config(repo)$local$user.name, + commit.user +) +expect_identical( + git2r::config(repo)$local$user.email, + commit.email +) + +expect_is( + git_connection( + repo.path = connection, + username = "me", + password = "junk", + commit.user = commit.user, + commit.email = commit.email + ), + "gitConnection" +) + +expect_error( + git_connection( + repo.path = connection, + username = rep("me", 2), + password = "junk", + commit.user = commit.user, + commit.email = commit.email + ), + "username is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + username = NA, + password = "junk", + commit.user = commit.user, + commit.email = commit.email + ), + "username is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + username = "", + password = "junk", + commit.user = commit.user, + commit.email = commit.email + ), + "username not not equal to \"\"" +) + +expect_error( + git_connection( + repo.path = connection, + username = "me", + password = rep("junk", 2), + commit.user = commit.user, + commit.email = commit.email + ), + "password is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + username = "me", + password = NA, + commit.user = commit.user, + commit.email = commit.email + ), + "password is not a string \\(a length one character vector\\)\\." +) +expect_error( + git_connection( + repo.path = connection, + username = "me", + password = "", + commit.user = commit.user, + commit.email = commit.email + ), + "password not not equal to \"\"" +) From e9c08f73a469424392246713b5ed9829719aaee7 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:39:57 +0100 Subject: [PATCH 15/33] Fix gitConnection class Signed-off-by: Thierry Onkelinx --- R/S4_classes.r | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/R/S4_classes.r b/R/S4_classes.r index 813aa5598..0ef19749e 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -881,17 +881,12 @@ setValidity( assert_that(is.string(object@CommitUser)) assert_that(is.string(object@CommitEmail)) root <- paste(object@Repository@path, ".", sep = "/") - assert_that(file_test("-d", root)) + assert_that(file_test("-d", root), msg = paste(root, "is not a directory")) full.path <- paste(root, object@LocalPath, sep = "/") - assert_that(file_test("-d", full.path)) - if (length(grep(root, full.path)) == 0) { - return( - paste0( - "Wrong local path. '", full.path, - "' is not a subdirectory of '", root, "'" - ) - ) - } + assert_that( + file_test("-d", full.path), + msg = paste(full.path, "is not a directory") + ) repo <- repository(root) repo.config <- config(repo) assert_that(has_name(repo.config$local, "user.name")) From 77e67a1cec5beae9d27d68924f67ab10660cc82c Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:40:51 +0100 Subject: [PATCH 16/33] test list_files_git() Signed-off-by: Thierry Onkelinx --- tests/testthat/test_list_files_git.R | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/testthat/test_list_files_git.R diff --git a/tests/testthat/test_list_files_git.R b/tests/testthat/test_list_files_git.R new file mode 100644 index 000000000..858fc17b4 --- /dev/null +++ b/tests/testthat/test_list_files_git.R @@ -0,0 +1,83 @@ +context("list files for a git repository") +describe("list_files_git()", { + files <- sort(c("test.txt", "0123456.txt", "test", "0123456")) + local.path <- "test" + connection <- tempfile(pattern = "git2r-") + connection <- normalizePath(connection, winslash = "/", mustWork = FALSE) + commit.user <- "me" + commit.email <- "me@me.com" + + + it("stops if connection is not a git repository", { + expect_error( + list_files_git( + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "is not a git repo" + ) + }) + + dir.create(connection) + repo <- git2r::init(connection) + connection <- normalizePath(connection, winslash = "/", mustWork = FALSE) + full.path <- paste(connection, local.path, sep = "/") + full.path <- normalizePath(full.path, winslash = "/", mustWork = FALSE) + it("stops if the local.path doesn't exist", { + expect_error( + list_files_git( + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "is not a directory" + ) + }) + + dir.create(paste(connection, local.path, sep = "/")) + file.create(paste(full.path, files, sep = "/")) + it("list the files according to the pattern", { + expect_that( + list_files_git( + local.path = local.path, + pattern = "^[0123456789].*\\.txt$", + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + is_identical_to(files[grep("^[0123456789].*\\.txt$", files)]) + ) + expect_that( + list_files_git( + local.path = local.path, + pattern = "\\.txt$", + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + is_identical_to(files[grep("\\.txt$", files)]) + ) + expect_that( + list_files_git( + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + is_identical_to(files) + ) + expect_that( + list_files_git( + local.path = local.path, + pattern = ".exe", + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + is_identical_to(character(0)) + ) + }) +}) From 4b3c019d2f8a1ca0b3742e21bd6c503f4defc3c1 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:47:09 +0100 Subject: [PATCH 17/33] test write_delim_git() Signed-off-by: Thierry Onkelinx --- DESCRIPTION | 1 + tests/testthat/test_write_delim_git.R | 108 ++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 tests/testthat/test_write_delim_git.R diff --git a/DESCRIPTION b/DESCRIPTION index d23d601bd..201126a35 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Depends: R (>= 3.0.2), methods Suggests: + dplyr, testthat, getPass Type: Package diff --git a/tests/testthat/test_write_delim_git.R b/tests/testthat/test_write_delim_git.R new file mode 100644 index 000000000..2e84aa6d1 --- /dev/null +++ b/tests/testthat/test_write_delim_git.R @@ -0,0 +1,108 @@ +context("write data.frame to git") +describe("write_delim_git()", { + commit.user <- "me" + commit.email <- "me@me.com" + x <- data.frame(0) + x1 <- data.frame(1) + file <- "test.txt" + local.path <- "test/subdir" + connection <- normalizePath( + tempfile(pattern = "git2r-"), + winslash = "/", + mustWork = FALSE + ) + + it("stops if connection is not a git repository", { + expect_error( + write_delim_git( + x = x, + file = file, + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "is not a git repo" + ) + }) + + dir.create(connection) + repo <- git2r::init(connection) + git2r::config(repo, user.name = "me", user.email = "me@me.com") + it("stops if the path doesn't exist", { + expect_error( + write_delim_git( + x = x, + file = file, + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "is not a directory" + ) + }) + full.path <- paste(connection, local.path, sep = "/") + dir.create(full.path, recursive = TRUE) + it("stops if x is not a data.frame", { + expect_error( + write_delim_git( + x = matrix(0), + file = file, + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + "x does not inherit from class data.frame" + ) + }) + + full.file.path <- paste(connection, local.path, file, sep = "/") + it("returns the sha1 of the file", { + expect_that( + write_delim_git( + x = x, + file = file, + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + is_identical_to(hashfile(full.file.path)) + ) + }) + it("can handle tbl_df", { + expect_that( + write_delim_git( + x = dplyr::as.tbl(x), + file = file, + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ), + is_identical_to(hashfile(full.file.path)) + ) + }) + + it("stages the file", { + expect_that( + status(repo)$staged$new, + is_identical_to(paste(local.path, file, sep = "/")) + ) + junk <- commit(repo, "a") + write_delim_git( + x = x1, + file = file, + local.path = local.path, + connection = connection, + commit.user = commit.user, + commit.email = commit.email + ) + expect_that( + status(repo)$staged$modified, + is_identical_to(paste(local.path, file, sep = "/")) + ) + }) +}) From 5d72b62bf0a99901964a66db8087e75a1c32913b Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sun, 10 Dec 2017 14:49:54 +0100 Subject: [PATCH 18/33] test read_delim_git() Signed-off-by: Thierry Onkelinx --- tests/testthat/test_read_delim_git.R | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/testthat/test_read_delim_git.R diff --git a/tests/testthat/test_read_delim_git.R b/tests/testthat/test_read_delim_git.R new file mode 100644 index 000000000..4877f2d07 --- /dev/null +++ b/tests/testthat/test_read_delim_git.R @@ -0,0 +1,61 @@ +context("read data.frame from git") +describe("read_delim_git()", { + file <- "test.txt" + local.path <- "test" + connection <- normalizePath( + tempfile(pattern = "git2r-"), + winslash = "/", + mustWork = FALSE + ) + df <- data.frame(x = 1, y = 1:10) + + + it("stops if connection is not a git repository", { + expect_error( + read_delim_git( + file = file, + local.path = local.path, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + "is not a git repo" + ) + }) + + dir.create(paste(connection, local.path, sep = "/"), recursive = TRUE) + repo <- init(connection) + + it("returns FALSE when the file doesn't exists", { + expect_error( + read_delim_git( + file = file, + local.path = local.path, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + paste0(repo@path, "/", local.path, "/", file) + ) + }) + write_delim_git( + x = df, + file = file, + local.path = local.path, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ) + it("read the tab-delimited file", { + expect_that( + read_delim_git( + file = file, + local.path = local.path, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + is_equivalent_to(df) + ) + }) +}) From f0af212ed892bfa9feec66575b58d645157b8463 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Mon, 11 Dec 2017 10:07:22 +0100 Subject: [PATCH 19/33] run unit tests in a sensible order Signed-off-by: Thierry Onkelinx --- tests/testthat/{test_git_connection.R => test_a_git_connection.R} | 0 .../testthat/{test_write_delim_git.R => test_b_write_delim_git.R} | 0 tests/testthat/{test_list_files_git.R => test_c_list_files_git.R} | 0 tests/testthat/{test_read_delim_git.R => test_c_read_delim_git.R} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename tests/testthat/{test_git_connection.R => test_a_git_connection.R} (100%) rename tests/testthat/{test_write_delim_git.R => test_b_write_delim_git.R} (100%) rename tests/testthat/{test_list_files_git.R => test_c_list_files_git.R} (100%) rename tests/testthat/{test_read_delim_git.R => test_c_read_delim_git.R} (100%) diff --git a/tests/testthat/test_git_connection.R b/tests/testthat/test_a_git_connection.R similarity index 100% rename from tests/testthat/test_git_connection.R rename to tests/testthat/test_a_git_connection.R diff --git a/tests/testthat/test_write_delim_git.R b/tests/testthat/test_b_write_delim_git.R similarity index 100% rename from tests/testthat/test_write_delim_git.R rename to tests/testthat/test_b_write_delim_git.R diff --git a/tests/testthat/test_list_files_git.R b/tests/testthat/test_c_list_files_git.R similarity index 100% rename from tests/testthat/test_list_files_git.R rename to tests/testthat/test_c_list_files_git.R diff --git a/tests/testthat/test_read_delim_git.R b/tests/testthat/test_c_read_delim_git.R similarity index 100% rename from tests/testthat/test_read_delim_git.R rename to tests/testthat/test_c_read_delim_git.R From eeb7e0fd8023392cd1bf7e80e1ac9546940b05f3 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Mon, 11 Dec 2017 12:00:49 +0100 Subject: [PATCH 20/33] Add unit tests for git_sha() Signed-off-by: Thierry Onkelinx --- R/git_sha.r | 10 ++++-- tests/testthat/test_c_git_sha.R | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test_c_git_sha.R diff --git a/R/git_sha.r b/R/git_sha.r index 5347b437e..b3fec1908 100644 --- a/R/git_sha.r +++ b/R/git_sha.r @@ -29,13 +29,14 @@ setMethod( #' @rdname git_sha #' @aliases git_sha,git_connection-methods #' @importFrom methods setMethod -#' @importFrom assertthat assert_that is.string +#' @importFrom assertthat assert_that noNA #' @importFrom utils read.table setMethod( f = "git_sha", signature = signature(connection = "gitConnection"), definition = function(file, connection, ...){ - assert_that(is.string(file)) + assert_that(is.character(file)) + assert_that(noNA(file)) old.wd <- getwd() setwd(connection@Repository@path) @@ -44,13 +45,18 @@ setMethod( "git ls-tree -r HEAD", connection@LocalPath ), + ignore.stderr = TRUE, intern = TRUE ) + if (length(blobs) == 0) { + stop("no commits available") + } setwd(old.wd) blobs <- read.table( textConnection(paste(blobs, collapse = "\n")), header = FALSE, sep = "\t", + stringsAsFactors = FALSE, col.names = c("SHA", "Path") ) blobs$File <- basename(blobs$Path) diff --git a/tests/testthat/test_c_git_sha.R b/tests/testthat/test_c_git_sha.R new file mode 100644 index 000000000..7902b2458 --- /dev/null +++ b/tests/testthat/test_c_git_sha.R @@ -0,0 +1,62 @@ +context("git_sha") +test_that("git_sha returns the correct value", { + file <- "test.txt" + local.path <- "test" + connection <- normalizePath( + tempfile(pattern = "git2r-git_sha"), + winslash = "/", + mustWork = FALSE + ) + df <- data.frame(x = 1, y = 1:10) + + expect_error( + git_sha( + file = file, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + "is not a git repo" + ) + + dir.create(paste(connection, local.path, sep = "/"), recursive = TRUE) + repo <- init(connection) + expect_error( + git_sha( + file = file, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + "no commits available" + ) + + connection <- git_connection( + repo.path = connection, + local.path = local.path, + commit.user = "me", + commit.email = "me@me.com" + ) + hash <- write_delim_git( + x = df, + file = file, + connection = connection + ) + commit(connection@Repository, message = "test") + expect_is( + sha <- git_sha( + file = file, + connection = connection + ), + "data.frame" + ) + expect_identical( + sha, + data.frame( + SHA = hash, + Path = local.path, + File = file, + stringsAsFactors = FALSE + ) + ) +}) From 9041e5032016b06977d54a087638b7c15ee0c844 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Mon, 11 Dec 2017 17:49:12 +0100 Subject: [PATCH 21/33] improve git_connection() Signed-off-by: Thierry Onkelinx --- R/S4_classes.r | 9 ++---- R/git_connection.r | 4 +++ tests/testthat/test_a_git_connection.R | 42 ++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/R/S4_classes.r b/R/S4_classes.r index 0ef19749e..13d58d84c 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -880,13 +880,8 @@ setValidity( function(object){ assert_that(is.string(object@CommitUser)) assert_that(is.string(object@CommitEmail)) - root <- paste(object@Repository@path, ".", sep = "/") - assert_that(file_test("-d", root), msg = paste(root, "is not a directory")) - full.path <- paste(root, object@LocalPath, sep = "/") - assert_that( - file_test("-d", full.path), - msg = paste(full.path, "is not a directory") - ) + root <- normalizePath(object@Repository@path) + normalizePath(paste(root, object@LocalPath, sep = "/")) repo <- repository(root) repo.config <- config(repo) assert_that(has_name(repo.config$local, "user.name")) diff --git a/R/git_connection.r b/R/git_connection.r index ed58284d6..215b034a1 100644 --- a/R/git_connection.r +++ b/R/git_connection.r @@ -35,6 +35,10 @@ git_connection <- function( ) repo <- repository(repo.path) config(repo, user.name = commit.user, user.email = commit.email) + assert_that( + dir.exists(paste(repo.path, local.path, sep = "/")), + msg = paste(local.path, "is not a directory") + ) if (missing(key) & missing(username)) { return( diff --git a/tests/testthat/test_a_git_connection.R b/tests/testthat/test_a_git_connection.R index 876ec9776..2ec9c13d0 100644 --- a/tests/testthat/test_a_git_connection.R +++ b/tests/testthat/test_a_git_connection.R @@ -1,8 +1,10 @@ context("git_connection") -connection <- tempfile(pattern = "git2r-") +tmpdir <- tempfile(pattern = "git2r-git_connection") +connection <- tmpdir commit.user <- "me" commit.email <- "me@me.com" +local.path <- "junk" # test repo.path expect_error( @@ -85,11 +87,11 @@ expect_is( "gitConnection" ) expect_identical( - git2r::config(repo)$local$user.name, + config(repo)$local$user.name, commit.user ) expect_identical( - git2r::config(repo)$local$user.email, + config(repo)$local$user.email, commit.email ) @@ -165,3 +167,37 @@ expect_error( ), "password not not equal to \"\"" ) +expect_error( + git_connection( + repo.path = connection, + local.path = local.path, + username = "me", + password = "junk", + commit.user = commit.user, + commit.email = commit.email + ), + "is not a directory" +) +dir.create(sprintf("%s/%s", tmpdir, local.path), recursive = TRUE) +expect_is( + z <- git_connection( + repo.path = connection, + local.path = local.path, + username = "me", + password = "junk", + commit.user = commit.user, + commit.email = commit.email + ), + "gitConnection" +) +expect_identical(z@LocalPath, local.path) +expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) +) +expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) +) From b827e34f54d77d83e242fde6aae0495374236801 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 11:28:26 +0100 Subject: [PATCH 22/33] remove unit test repos after running tests Signed-off-by: Thierry Onkelinx --- tests/testthat/test_b_write_delim_git.R | 17 ++++++++++++++--- tests/testthat/test_c_git_sha.R | 13 ++++++++++++- tests/testthat/test_c_list_files_git.R | 18 ++++++++++++++++-- tests/testthat/test_c_read_delim_git.R | 13 ++++++++++++- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test_b_write_delim_git.R b/tests/testthat/test_b_write_delim_git.R index 2e84aa6d1..3c85e6966 100644 --- a/tests/testthat/test_b_write_delim_git.R +++ b/tests/testthat/test_b_write_delim_git.R @@ -6,8 +6,9 @@ describe("write_delim_git()", { x1 <- data.frame(1) file <- "test.txt" local.path <- "test/subdir" + tmpdir <- tempfile(pattern = "git2r-write_delim_git") connection <- normalizePath( - tempfile(pattern = "git2r-"), + tmpdir, winslash = "/", mustWork = FALSE ) @@ -27,8 +28,8 @@ describe("write_delim_git()", { }) dir.create(connection) - repo <- git2r::init(connection) - git2r::config(repo, user.name = "me", user.email = "me@me.com") + repo <- init(connection) + config(repo, user.name = "me", user.email = "me@me.com") it("stops if the path doesn't exist", { expect_error( write_delim_git( @@ -105,4 +106,14 @@ describe("write_delim_git()", { is_identical_to(paste(local.path, file, sep = "/")) ) }) + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) }) diff --git a/tests/testthat/test_c_git_sha.R b/tests/testthat/test_c_git_sha.R index 7902b2458..278e26718 100644 --- a/tests/testthat/test_c_git_sha.R +++ b/tests/testthat/test_c_git_sha.R @@ -2,8 +2,9 @@ context("git_sha") test_that("git_sha returns the correct value", { file <- "test.txt" local.path <- "test" + tmpdir <- tempfile(pattern = "git2r-git_sha") connection <- normalizePath( - tempfile(pattern = "git2r-git_sha"), + tmpdir, winslash = "/", mustWork = FALSE ) @@ -59,4 +60,14 @@ test_that("git_sha returns the correct value", { stringsAsFactors = FALSE ) ) + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) }) diff --git a/tests/testthat/test_c_list_files_git.R b/tests/testthat/test_c_list_files_git.R index 858fc17b4..74813b17f 100644 --- a/tests/testthat/test_c_list_files_git.R +++ b/tests/testthat/test_c_list_files_git.R @@ -2,8 +2,12 @@ context("list files for a git repository") describe("list_files_git()", { files <- sort(c("test.txt", "0123456.txt", "test", "0123456")) local.path <- "test" - connection <- tempfile(pattern = "git2r-") - connection <- normalizePath(connection, winslash = "/", mustWork = FALSE) + tmpdir <- tempfile(pattern = "git2r-list_files_git") + connection <- normalizePath( + tmpdir, + winslash = "/", + mustWork = FALSE + ) commit.user <- "me" commit.email <- "me@me.com" @@ -80,4 +84,14 @@ describe("list_files_git()", { is_identical_to(character(0)) ) }) + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) }) diff --git a/tests/testthat/test_c_read_delim_git.R b/tests/testthat/test_c_read_delim_git.R index 4877f2d07..5b6c68e30 100644 --- a/tests/testthat/test_c_read_delim_git.R +++ b/tests/testthat/test_c_read_delim_git.R @@ -2,8 +2,9 @@ context("read data.frame from git") describe("read_delim_git()", { file <- "test.txt" local.path <- "test" + tmpdir <- tempfile(pattern = "git2r-read_delim_git") connection <- normalizePath( - tempfile(pattern = "git2r-"), + tmpdir, winslash = "/", mustWork = FALSE ) @@ -58,4 +59,14 @@ describe("read_delim_git()", { is_equivalent_to(df) ) }) + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) }) From 13ca9a3eb87899e724a11fe01b0d45381c6427fd Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 13:10:22 +0100 Subject: [PATCH 23/33] git_sha() tests more efficiently for missing commits Signed-off-by: Thierry Onkelinx --- R/git_sha.r | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/git_sha.r b/R/git_sha.r index b3fec1908..6c534eaf4 100644 --- a/R/git_sha.r +++ b/R/git_sha.r @@ -38,6 +38,10 @@ setMethod( assert_that(is.character(file)) assert_that(noNA(file)) + if (is.null(head(connection@Repository))) { + stop("no commits available") + } + old.wd <- getwd() setwd(connection@Repository@path) blobs <- system( @@ -48,9 +52,6 @@ setMethod( ignore.stderr = TRUE, intern = TRUE ) - if (length(blobs) == 0) { - stop("no commits available") - } setwd(old.wd) blobs <- read.table( textConnection(paste(blobs, collapse = "\n")), From c95efc7fe9d38997110fe3dba79eeea2fedb1641 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 13:11:00 +0100 Subject: [PATCH 24/33] add unit tests for remove_files_git() Signed-off-by: Thierry Onkelinx --- R/remove_files_git.r | 1 - tests/testthat/test_d_remove_files_git.R | 69 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test_d_remove_files_git.R diff --git a/R/remove_files_git.r b/R/remove_files_git.r index 8d72815f2..5430e3a02 100644 --- a/R/remove_files_git.r +++ b/R/remove_files_git.r @@ -25,7 +25,6 @@ setMethod( definition = function(connection, pattern = NULL, ...){ this.connection <- git_connection( repo.path = connection, - local.path = list(...)$path, ... ) remove_files_git(connection = this.connection, pattern = pattern) diff --git a/tests/testthat/test_d_remove_files_git.R b/tests/testthat/test_d_remove_files_git.R new file mode 100644 index 000000000..e7d05d452 --- /dev/null +++ b/tests/testthat/test_d_remove_files_git.R @@ -0,0 +1,69 @@ +context("remove_files_git") +test_that("remove_files_git returns the correct value", { + file <- "test.txt" + local.path <- "test" + tmpdir <- tempfile(pattern = "git2r-remove_files_git") + connection <- normalizePath( + tmpdir, + winslash = "/", + mustWork = FALSE + ) + df <- data.frame(x = 1, y = 1:10) + pattern <- "txt$" + + expect_error( + remove_files_git( + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + "is not a git repo" + ) + + dir.create(paste(connection, local.path, sep = "/"), recursive = TRUE) + repo <- init(connection) + + git_con <- git_connection( + repo.path = connection, + local.path = local.path, + commit.user = "me", + commit.email = "me@me.com" + ) + hash <- write_delim_git( + x = df, + file = file, + connection = git_con, + commit.user = "me", + commit.email = "me@me.com" + ) + z <- commit(git_con@Repository, message = "test") + expect_true( + remove_files_git( + connection = connection, + local.path = local.path, + commit.user = "me", + commit.email = "me@me.com", + pattern = "junk" + ) + ) + expect_true( + remove_files_git( + connection = git_con, + pattern = pattern + ) + ) + expect_identical( + list_files_git(git_con), + character(0) + ) + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) +}) From d76158b1256d4d8554c33d5e6018dc9f7ce22cde Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 16:48:59 +0100 Subject: [PATCH 25/33] add unit test for git_recent() Signed-off-by: Thierry Onkelinx --- R/git_recent.r | 4 ++ tests/testthat/test_c_git_recent.R | 63 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/testthat/test_c_git_recent.R diff --git a/R/git_recent.r b/R/git_recent.r index 7f017072c..f7f36070e 100644 --- a/R/git_recent.r +++ b/R/git_recent.r @@ -36,6 +36,10 @@ setMethod( definition = function(file, connection, ...){ assert_that(is.string(file)) + if (is.null(head(connection@Repository))) { + stop("no commits in current branch") + } + old.wd <- getwd() setwd(connection@Repository@path) commit.info <- system( diff --git a/tests/testthat/test_c_git_recent.R b/tests/testthat/test_c_git_recent.R new file mode 100644 index 000000000..5838f0e90 --- /dev/null +++ b/tests/testthat/test_c_git_recent.R @@ -0,0 +1,63 @@ +context("git_recent") +test_that("git_recent works", { + file <- "test.txt" + local.path <- "test" + tmpdir <- tempfile(pattern = "git2r-git_recent") + connection <- normalizePath( + tmpdir, + winslash = "/", + mustWork = FALSE + ) + df <- data.frame(x = 1, y = 1:10) + + dir.create(paste(connection, local.path, sep = "/"), recursive = TRUE) + repo <- init(connection) + + expect_error( + git_recent( + file = file, + local.path = local.path, + connection = connection, + commit.user = "me", + commit.email = "me@me.com" + ), + "no commits in current branch" + ) + + connection <- git_connection( + repo.path = connection, + local.path = local.path, + commit.user = "me", + commit.email = "me@me.com" + ) + write_delim_git( + x = df, + file = file, + connection = connection + ) + z <- commit(connection@Repository, "test") + expect_is( + x <- git_recent(file = file, connection = connection), + "list" + ) + + expect_identical( + x, + list( + Commit = z@sha, + Author = sprintf("%s <%s>", z@author@name, z@author@email), + Date = as.POSIXct(as(z@author@when, "character")) + ) + ) + + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) +}) From e9f63728c1a5ac628b555189938ea6322e207e99 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 17:11:39 +0100 Subject: [PATCH 26/33] add commit method for gitConnection class Signed-off-by: Thierry Onkelinx --- R/commit.r | 17 +++++++++++++++++ R/repository.r | 11 +++++++++++ man/commit-methods.Rd | 5 +++++ man/default_signature-methods.Rd | 3 +++ tests/testthat/test_c_git_recent.R | 2 +- tests/testthat/test_c_git_sha.R | 2 +- tests/testthat/test_d_remove_files_git.R | 2 +- 7 files changed, 39 insertions(+), 3 deletions(-) diff --git a/R/commit.r b/R/commit.r index 823a3dd33..d148e1853 100644 --- a/R/commit.r +++ b/R/commit.r @@ -172,6 +172,23 @@ setMethod("commit", } ) +##' @rdname commit-methods +##' @export +##' @include S4_classes.r +setMethod("commit", + signature(repo = "gitConnection"), + function(repo, message, all, session, reference, author, committer) { + commit( + repo = repo@Repository, + message = message, + all = all, + session = session, + reference = reference, + author = author, + committer = committer + ) + } +) ##' Commits ##' ##' @rdname commits-methods diff --git a/R/repository.r b/R/repository.r index 5123882b5..6d0d74e74 100644 --- a/R/repository.r +++ b/R/repository.r @@ -785,6 +785,17 @@ setMethod("default_signature", } ) +##' @rdname default_signature-methods +##' @export +##' @include S4_classes.r +setMethod("default_signature", + signature(repo = "gitConnection"), + function(repo) + { + .Call(git2r_signature_default, repo@Repository) + } +) + ##' Brief summary of repository ##' ##' @aliases show,git_repository-methods diff --git a/man/commit-methods.Rd b/man/commit-methods.Rd index 593c06384..550d049ce 100644 --- a/man/commit-methods.Rd +++ b/man/commit-methods.Rd @@ -4,6 +4,7 @@ \name{commit} \alias{commit} \alias{commit,git_repository-method} +\alias{commit,gitConnection-method} \title{Commit} \usage{ commit(repo, message = NULL, all = FALSE, session = FALSE, @@ -13,6 +14,10 @@ commit(repo, message = NULL, all = FALSE, session = FALSE, \S4method{commit}{git_repository}(repo, message = NULL, all = FALSE, session = FALSE, reference = "HEAD", author = default_signature(repo), committer = default_signature(repo)) + +\S4method{commit}{gitConnection}(repo, message = NULL, all = FALSE, + session = FALSE, reference = "HEAD", author = default_signature(repo), + committer = default_signature(repo)) } \arguments{ \item{repo}{The repository \code{object}.} diff --git a/man/default_signature-methods.Rd b/man/default_signature-methods.Rd index 25af98a69..57db18077 100644 --- a/man/default_signature-methods.Rd +++ b/man/default_signature-methods.Rd @@ -4,11 +4,14 @@ \name{default_signature} \alias{default_signature} \alias{default_signature,git_repository-method} +\alias{default_signature,gitConnection-method} \title{Get the signature} \usage{ default_signature(repo) \S4method{default_signature}{git_repository}(repo) + +\S4method{default_signature}{gitConnection}(repo) } \arguments{ \item{repo}{The repository \code{object} to check signature} diff --git a/tests/testthat/test_c_git_recent.R b/tests/testthat/test_c_git_recent.R index 5838f0e90..7a4e60dd9 100644 --- a/tests/testthat/test_c_git_recent.R +++ b/tests/testthat/test_c_git_recent.R @@ -35,7 +35,7 @@ test_that("git_recent works", { file = file, connection = connection ) - z <- commit(connection@Repository, "test") + z <- commit(connection, "test") expect_is( x <- git_recent(file = file, connection = connection), "list" diff --git a/tests/testthat/test_c_git_sha.R b/tests/testthat/test_c_git_sha.R index 278e26718..a36e47c87 100644 --- a/tests/testthat/test_c_git_sha.R +++ b/tests/testthat/test_c_git_sha.R @@ -43,7 +43,7 @@ test_that("git_sha returns the correct value", { file = file, connection = connection ) - commit(connection@Repository, message = "test") + commit(connection, message = "test") expect_is( sha <- git_sha( file = file, diff --git a/tests/testthat/test_d_remove_files_git.R b/tests/testthat/test_d_remove_files_git.R index e7d05d452..137a9f181 100644 --- a/tests/testthat/test_d_remove_files_git.R +++ b/tests/testthat/test_d_remove_files_git.R @@ -36,7 +36,7 @@ test_that("remove_files_git returns the correct value", { commit.user = "me", commit.email = "me@me.com" ) - z <- commit(git_con@Repository, message = "test") + z <- commit(git_con, message = "test") expect_true( remove_files_git( connection = connection, From 65691acbdc0a75ea90d8a81be3959c507f394ced Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 17:25:08 +0100 Subject: [PATCH 27/33] rename gitConnection class to git_connection class Signed-off-by: Thierry Onkelinx --- NAMESPACE | 2 +- R/S4_classes.r | 14 +++++++------- R/commit.r | 2 +- R/git_connection.r | 10 +++++----- R/git_recent.r | 2 +- R/git_sha.r | 2 +- R/list_files_git.r | 2 +- R/read_delim_git.r | 2 +- R/remove_files_git.r | 2 +- R/repository.r | 2 +- R/write_delim_git.r | 4 ++-- man/commit-methods.Rd | 4 ++-- man/default_signature-methods.Rd | 4 ++-- ...Connection-class.Rd => git_connection-class.Rd} | 8 ++++---- man/git_recent.Rd | 6 +++--- man/git_sha.Rd | 6 +++--- man/list_files_git.Rd | 6 +++--- man/read_delim_git.Rd | 6 +++--- man/remove_files_git.Rd | 6 +++--- man/write_delim_git.Rd | 6 +++--- tests/testthat/test_a_git_connection.R | 6 +++--- 21 files changed, 51 insertions(+), 51 deletions(-) rename man/{gitConnection-class.Rd => git_connection-class.Rd} (92%) diff --git a/NAMESPACE b/NAMESPACE index 3fa957bf4..e07988f0c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,12 +21,12 @@ exportClasses(cred_env) exportClasses(cred_ssh_key) exportClasses(cred_token) exportClasses(cred_user_pass) -exportClasses(gitConnection) exportClasses(git_blame) exportClasses(git_blame_hunk) exportClasses(git_blob) exportClasses(git_branch) exportClasses(git_commit) +exportClasses(git_connection) exportClasses(git_diff) exportClasses(git_diff_file) exportClasses(git_diff_hunk) diff --git a/R/S4_classes.r b/R/S4_classes.r index 13d58d84c..291ce2613 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -844,7 +844,7 @@ setClass("git_merge_result", #' @importFrom methods setClassUnion setClassUnion("gitCredentials", c("NULL", "cred_user_pass", "cred_ssh_key")) -#' The gitConnection class +#' The git_connection class #' #' @section Slots: #' \describe{ @@ -854,15 +854,15 @@ setClassUnion("gitCredentials", c("NULL", "cred_user_pass", "cred_ssh_key")) #' \item{\code{CommitUser}}{the name of the user how will commit} #' \item{\code{CommitEmail}}{the email of the user how will commit} #' } -#' @name gitConnection-class -#' @rdname gitConnection-class -#' @exportClass gitConnection -#' @aliases gitConnection-class +#' @name git_connection-class +#' @rdname git_connection-class +#' @exportClass git_connection +#' @aliases git_connection-class #' @importFrom methods setClass #' @docType class #' @template thierry setClass( - Class = "gitConnection", + Class = "git_connection", representation = representation( Repository = "git_repository", LocalPath = "character", @@ -876,7 +876,7 @@ setClass( #' @importFrom assertthat assert_that is.string has_name #' @importFrom utils file_test setValidity( - "gitConnection", + "git_connection", function(object){ assert_that(is.string(object@CommitUser)) assert_that(is.string(object@CommitEmail)) diff --git a/R/commit.r b/R/commit.r index d148e1853..06a9af4a3 100644 --- a/R/commit.r +++ b/R/commit.r @@ -176,7 +176,7 @@ setMethod("commit", ##' @export ##' @include S4_classes.r setMethod("commit", - signature(repo = "gitConnection"), + signature(repo = "git_connection"), function(repo, message, all, session, reference, author, committer) { commit( repo = repo@Repository, diff --git a/R/git_connection.r b/R/git_connection.r index 215b034a1..1cf8e29e0 100644 --- a/R/git_connection.r +++ b/R/git_connection.r @@ -1,6 +1,6 @@ #' Open a git connection #' @name git_connection -#' @rdname gitConnection-class +#' @rdname git_connection-class #' @param repo.path The path of the root of the repository #' @param local.path A path within the repository #' @param key Optional: the path to a private ssh key. The public key is assumed @@ -43,7 +43,7 @@ git_connection <- function( if (missing(key) & missing(username)) { return( new( - "gitConnection", + "git_connection", Repository = repo, LocalPath = local.path, Credentials = NULL, @@ -59,7 +59,7 @@ git_connection <- function( if (missing(password)) { return( new( - "gitConnection", + "git_connection", Repository = repo, LocalPath = local.path, Credentials = cred_ssh_key( @@ -75,7 +75,7 @@ git_connection <- function( assert_that(is.string(password)) return( new( - "gitConnection", + "git_connection", Repository = repo, LocalPath = local.path, Credentials = cred_ssh_key( @@ -95,7 +95,7 @@ git_connection <- function( assert_that(password != "") return( new( - "gitConnection", + "git_connection", Repository = repo, LocalPath = local.path, Credentials = cred_user_pass( diff --git a/R/git_recent.r b/R/git_recent.r index f7f36070e..c546e8f94 100644 --- a/R/git_recent.r +++ b/R/git_recent.r @@ -32,7 +32,7 @@ setMethod( #' @importFrom assertthat assert_that is.string setMethod( f = "git_recent", - signature = signature(connection = "gitConnection"), + signature = signature(connection = "git_connection"), definition = function(file, connection, ...){ assert_that(is.string(file)) diff --git a/R/git_sha.r b/R/git_sha.r index 6c534eaf4..c8a5b3f4b 100644 --- a/R/git_sha.r +++ b/R/git_sha.r @@ -33,7 +33,7 @@ setMethod( #' @importFrom utils read.table setMethod( f = "git_sha", - signature = signature(connection = "gitConnection"), + signature = signature(connection = "git_connection"), definition = function(file, connection, ...){ assert_that(is.character(file)) assert_that(noNA(file)) diff --git a/R/list_files_git.r b/R/list_files_git.r index 7d33f969b..baae7e14f 100644 --- a/R/list_files_git.r +++ b/R/list_files_git.r @@ -37,7 +37,7 @@ setMethod( #' @importFrom assertthat assert_that is.string is.flag noNA setMethod( f = "list_files_git", - signature = signature(connection = "gitConnection"), + signature = signature(connection = "git_connection"), definition = function(connection, pattern = NULL, full.names = FALSE, ...){ if (!is.null(pattern)) { assert_that(is.string(pattern)) diff --git a/R/read_delim_git.r b/R/read_delim_git.r index e61550317..b2f54ec00 100644 --- a/R/read_delim_git.r +++ b/R/read_delim_git.r @@ -33,7 +33,7 @@ setMethod( #' @importFrom assertthat assert_that is.string setMethod( f = "read_delim_git", - signature = signature(connection = "gitConnection"), + signature = signature(connection = "git_connection"), definition = function(file, connection, ...){ assert_that(is.string(file)) diff --git a/R/remove_files_git.r b/R/remove_files_git.r index 5430e3a02..340d4434d 100644 --- a/R/remove_files_git.r +++ b/R/remove_files_git.r @@ -36,7 +36,7 @@ setMethod( #' @importFrom methods setMethod setMethod( f = "remove_files_git", - signature = signature(connection = "gitConnection"), + signature = signature(connection = "git_connection"), definition = function(connection, pattern = NULL, ...){ to.remove <- list_files_git( connection = connection, diff --git a/R/repository.r b/R/repository.r index 6d0d74e74..7a5de4700 100644 --- a/R/repository.r +++ b/R/repository.r @@ -789,7 +789,7 @@ setMethod("default_signature", ##' @export ##' @include S4_classes.r setMethod("default_signature", - signature(repo = "gitConnection"), + signature(repo = "git_connection"), function(repo) { .Call(git2r_signature_default, repo@Repository) diff --git a/R/write_delim_git.r b/R/write_delim_git.r index 8f959316e..9d8f83758 100644 --- a/R/write_delim_git.r +++ b/R/write_delim_git.r @@ -3,7 +3,7 @@ #' The existing file will be overwritten. #' @param x the data.frame #' @param file the name of the file -#' @param connection The path of a git repository or a \code{gitConnection} +#' @param connection The path of a git repository or a \code{git_connection} #' object #' @param ... parameters passed to \code{git_connection()} when #' \code{connection} is a path @@ -41,7 +41,7 @@ setMethod( #' @importFrom assertthat assert_that is.string setMethod( f = "write_delim_git", - signature = signature(connection = "gitConnection"), + signature = signature(connection = "git_connection"), definition = function(x, file, connection, ...){ assert_that(inherits(x, "data.frame")) assert_that(is.string(file)) diff --git a/man/commit-methods.Rd b/man/commit-methods.Rd index 550d049ce..d77ff8a53 100644 --- a/man/commit-methods.Rd +++ b/man/commit-methods.Rd @@ -4,7 +4,7 @@ \name{commit} \alias{commit} \alias{commit,git_repository-method} -\alias{commit,gitConnection-method} +\alias{commit,git_connection-method} \title{Commit} \usage{ commit(repo, message = NULL, all = FALSE, session = FALSE, @@ -15,7 +15,7 @@ commit(repo, message = NULL, all = FALSE, session = FALSE, session = FALSE, reference = "HEAD", author = default_signature(repo), committer = default_signature(repo)) -\S4method{commit}{gitConnection}(repo, message = NULL, all = FALSE, +\S4method{commit}{git_connection}(repo, message = NULL, all = FALSE, session = FALSE, reference = "HEAD", author = default_signature(repo), committer = default_signature(repo)) } diff --git a/man/default_signature-methods.Rd b/man/default_signature-methods.Rd index 57db18077..f66f03cb2 100644 --- a/man/default_signature-methods.Rd +++ b/man/default_signature-methods.Rd @@ -4,14 +4,14 @@ \name{default_signature} \alias{default_signature} \alias{default_signature,git_repository-method} -\alias{default_signature,gitConnection-method} +\alias{default_signature,git_connection-method} \title{Get the signature} \usage{ default_signature(repo) \S4method{default_signature}{git_repository}(repo) -\S4method{default_signature}{gitConnection}(repo) +\S4method{default_signature}{git_connection}(repo) } \arguments{ \item{repo}{The repository \code{object} to check signature} diff --git a/man/gitConnection-class.Rd b/man/git_connection-class.Rd similarity index 92% rename from man/gitConnection-class.Rd rename to man/git_connection-class.Rd index 46cb15862..1bd15e559 100644 --- a/man/gitConnection-class.Rd +++ b/man/git_connection-class.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/S4_classes.r, R/git_connection.r \docType{class} -\name{gitConnection-class} -\alias{gitConnection-class} +\name{git_connection-class} +\alias{git_connection-class} \alias{git_connection} -\title{The gitConnection class} +\title{The git_connection class} \usage{ git_connection(repo.path, local.path = ".", key, username, password, commit.user, commit.email) @@ -29,7 +29,7 @@ be missing when the ssh-key doesn't require a password.} \item{commit.email}{the email of the user how will commit} } \description{ -The gitConnection class +The git_connection class Open a git connection } diff --git a/man/git_recent.Rd b/man/git_recent.Rd index f98b22bd3..7afd845de 100644 --- a/man/git_recent.Rd +++ b/man/git_recent.Rd @@ -5,7 +5,7 @@ \alias{git_recent} \alias{git_recent,ANY,ANY-method} \alias{git_recent,git_connection-methods} -\alias{git_recent,ANY,gitConnection-method} +\alias{git_recent,ANY,git_connection-method} \alias{git_recent,git_connection-methods} \title{Get the info from the latest commit of a file} \usage{ @@ -13,12 +13,12 @@ git_recent(file, connection, ...) \S4method{git_recent}{ANY,ANY}(file, connection, ...) -\S4method{git_recent}{ANY,gitConnection}(file, connection, ...) +\S4method{git_recent}{ANY,git_connection}(file, connection, ...) } \arguments{ \item{file}{the name of the file} -\item{connection}{The path of a git repository or a \code{gitConnection} +\item{connection}{The path of a git repository or a \code{git_connection} object} \item{...}{parameters passed to \code{git_connection()} when diff --git a/man/git_sha.Rd b/man/git_sha.Rd index e83f7b614..41e0d1cc9 100644 --- a/man/git_sha.Rd +++ b/man/git_sha.Rd @@ -5,7 +5,7 @@ \alias{git_sha} \alias{git_sha,ANY,ANY-method} \alias{git_sha,git_connection-methods} -\alias{git_sha,ANY,gitConnection-method} +\alias{git_sha,ANY,git_connection-method} \alias{git_sha,git_connection-methods} \title{Get the SHA of the files at the HEAD} \usage{ @@ -13,12 +13,12 @@ git_sha(file, connection, ...) \S4method{git_sha}{ANY,ANY}(file, connection, ...) -\S4method{git_sha}{ANY,gitConnection}(file, connection, ...) +\S4method{git_sha}{ANY,git_connection}(file, connection, ...) } \arguments{ \item{file}{the name of the file} -\item{connection}{The path of a git repository or a \code{gitConnection} +\item{connection}{The path of a git repository or a \code{git_connection} object} \item{...}{parameters passed to \code{git_connection()} when diff --git a/man/list_files_git.Rd b/man/list_files_git.Rd index 5400e6a92..33899b36b 100644 --- a/man/list_files_git.Rd +++ b/man/list_files_git.Rd @@ -5,7 +5,7 @@ \alias{list_files_git} \alias{list_files_git,ANY-method} \alias{list_files_git,git_connection-methods} -\alias{list_files_git,gitConnection-method} +\alias{list_files_git,git_connection-method} \alias{list_files_git,git_connection-methods} \title{List the files in a path of a git repository} \usage{ @@ -14,11 +14,11 @@ list_files_git(connection, pattern = NULL, full.names = FALSE, ...) \S4method{list_files_git}{ANY}(connection, pattern = NULL, full.names = FALSE, ...) -\S4method{list_files_git}{gitConnection}(connection, pattern = NULL, +\S4method{list_files_git}{git_connection}(connection, pattern = NULL, full.names = FALSE, ...) } \arguments{ -\item{connection}{The path of a git repository or a \code{gitConnection} +\item{connection}{The path of a git repository or a \code{git_connection} object} \item{pattern}{an optional \link{regular expression}. Only file names diff --git a/man/read_delim_git.Rd b/man/read_delim_git.Rd index 58baf387b..0685b6b96 100644 --- a/man/read_delim_git.Rd +++ b/man/read_delim_git.Rd @@ -5,7 +5,7 @@ \alias{read_delim_git} \alias{read_delim_git,ANY,ANY-method} \alias{read_delim_git,git_connection-methods} -\alias{read_delim_git,ANY,gitConnection-method} +\alias{read_delim_git,ANY,git_connection-method} \alias{read_delim_git,git_connection-methods} \title{Read a tab delimited file from a git repository} \usage{ @@ -13,12 +13,12 @@ read_delim_git(file, connection, ...) \S4method{read_delim_git}{ANY,ANY}(file, connection, ...) -\S4method{read_delim_git}{ANY,gitConnection}(file, connection, ...) +\S4method{read_delim_git}{ANY,git_connection}(file, connection, ...) } \arguments{ \item{file}{the name of the file} -\item{connection}{The path of a git repository or a \code{gitConnection} +\item{connection}{The path of a git repository or a \code{git_connection} object} \item{...}{parameters passed to \code{git_connection()} when diff --git a/man/remove_files_git.Rd b/man/remove_files_git.Rd index abfed4c54..9017473a6 100644 --- a/man/remove_files_git.Rd +++ b/man/remove_files_git.Rd @@ -5,7 +5,7 @@ \alias{remove_files_git} \alias{remove_files_git,ANY-method} \alias{remove_files_git,git_connection-methods} -\alias{remove_files_git,gitConnection-method} +\alias{remove_files_git,git_connection-method} \alias{remove_files_git,git_connection-methods} \title{Remove all the files in a path of a git repository} \usage{ @@ -13,10 +13,10 @@ remove_files_git(connection, pattern = NULL, ...) \S4method{remove_files_git}{ANY}(connection, pattern = NULL, ...) -\S4method{remove_files_git}{gitConnection}(connection, pattern = NULL, ...) +\S4method{remove_files_git}{git_connection}(connection, pattern = NULL, ...) } \arguments{ -\item{connection}{The path of a git repository or a \code{gitConnection} +\item{connection}{The path of a git repository or a \code{git_connection} object} \item{pattern}{an optional \link{regular expression}. Only file names diff --git a/man/write_delim_git.Rd b/man/write_delim_git.Rd index 33a27e01b..21a21e33d 100644 --- a/man/write_delim_git.Rd +++ b/man/write_delim_git.Rd @@ -5,7 +5,7 @@ \alias{write_delim_git} \alias{write_delim_git,ANY,ANY,ANY-method} \alias{write_delim_git,git_connection-methods} -\alias{write_delim_git,ANY,ANY,gitConnection-method} +\alias{write_delim_git,ANY,ANY,git_connection-method} \alias{write_delim_git,git_connection-methods} \title{Write a dataframe as a tab delimited file to a git repository and stage it} \usage{ @@ -13,14 +13,14 @@ write_delim_git(x, file, connection, ...) \S4method{write_delim_git}{ANY,ANY,ANY}(x, file, connection, ...) -\S4method{write_delim_git}{ANY,ANY,gitConnection}(x, file, connection, ...) +\S4method{write_delim_git}{ANY,ANY,git_connection}(x, file, connection, ...) } \arguments{ \item{x}{the data.frame} \item{file}{the name of the file} -\item{connection}{The path of a git repository or a \code{gitConnection} +\item{connection}{The path of a git repository or a \code{git_connection} object} \item{...}{parameters passed to \code{git_connection()} when diff --git a/tests/testthat/test_a_git_connection.R b/tests/testthat/test_a_git_connection.R index 2ec9c13d0..096a65cc9 100644 --- a/tests/testthat/test_a_git_connection.R +++ b/tests/testthat/test_a_git_connection.R @@ -84,7 +84,7 @@ expect_is( commit.user = commit.user, commit.email = commit.email ), - "gitConnection" + "git_connection" ) expect_identical( config(repo)$local$user.name, @@ -103,7 +103,7 @@ expect_is( commit.user = commit.user, commit.email = commit.email ), - "gitConnection" + "git_connection" ) expect_error( @@ -188,7 +188,7 @@ expect_is( commit.user = commit.user, commit.email = commit.email ), - "gitConnection" + "git_connection" ) expect_identical(z@LocalPath, local.path) expect_true( From 8a7c8707aab04ae3efc76984c09842140653d4bf Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 17:27:50 +0100 Subject: [PATCH 28/33] rename gitCredentials class to git_credentials class Signed-off-by: Thierry Onkelinx --- R/S4_classes.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/S4_classes.r b/R/S4_classes.r index 291ce2613..701fa4e7e 100644 --- a/R/S4_classes.r +++ b/R/S4_classes.r @@ -842,7 +842,7 @@ setClass("git_merge_result", ) #' @importFrom methods setClassUnion -setClassUnion("gitCredentials", c("NULL", "cred_user_pass", "cred_ssh_key")) +setClassUnion("git_credentials", c("NULL", "cred_user_pass", "cred_ssh_key")) #' The git_connection class #' @@ -866,7 +866,7 @@ setClass( representation = representation( Repository = "git_repository", LocalPath = "character", - Credentials = "gitCredentials", + Credentials = "git_credentials", CommitUser = "character", CommitEmail = "character" ) From 1267d584bd16fc65f0a08b73feacfc171c466b0e Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 12 Dec 2017 17:38:38 +0100 Subject: [PATCH 29/33] add more unit tests for remove_files_git Signed-off-by: Thierry Onkelinx --- tests/testthat/test_d_remove_files_git.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test_d_remove_files_git.R b/tests/testthat/test_d_remove_files_git.R index 137a9f181..b3eb33b1d 100644 --- a/tests/testthat/test_d_remove_files_git.R +++ b/tests/testthat/test_d_remove_files_git.R @@ -46,6 +46,10 @@ test_that("remove_files_git returns the correct value", { pattern = "junk" ) ) + expect_identical( + list_files_git(git_con), + file + ) expect_true( remove_files_git( connection = git_con, @@ -56,6 +60,11 @@ test_that("remove_files_git returns the correct value", { list_files_git(git_con), character(0) ) + expect_identical( + status(git_con@Repository)$staged$deleted, + paste(local.path, file, sep = "/") + ) + expect_true( all(file.remove( list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) From 6efa0b17e36159a824e8e5aa4153158f53565985 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Thu, 14 Dec 2017 21:50:42 +0100 Subject: [PATCH 30/33] add more unit tests for remove_files_git() Signed-off-by: Thierry Onkelinx --- R/remove_files_git.r | 2 +- tests/testthat/test_d_remove_files_git.R | 64 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/R/remove_files_git.r b/R/remove_files_git.r index 340d4434d..084098414 100644 --- a/R/remove_files_git.r +++ b/R/remove_files_git.r @@ -44,7 +44,7 @@ setMethod( full.names = TRUE ) - success <- file.remove(to.remove) + success <- suppressWarnings(file.remove(to.remove)) if (length(success) > 0 && !all(success)) { stop( diff --git a/tests/testthat/test_d_remove_files_git.R b/tests/testthat/test_d_remove_files_git.R index b3eb33b1d..4a1bab3d8 100644 --- a/tests/testthat/test_d_remove_files_git.R +++ b/tests/testthat/test_d_remove_files_git.R @@ -76,3 +76,67 @@ test_that("remove_files_git returns the correct value", { )) ) }) + +test_that("returns an error when removing files fails", { + skip_on_os(c("windows", "mac", "solaris")) + + file <- "test.txt" + local.path <- "test" + tmpdir <- tempfile(pattern = "git2r-remove_files_git_error") + connection <- normalizePath( + tmpdir, + winslash = "/", + mustWork = FALSE + ) + df <- data.frame(x = 1, y = 1:10) + pattern <- "txt$" + dir.create(paste(connection, local.path, sep = "/"), recursive = TRUE) + repo <- init(connection) + + git_con <- git_connection( + repo.path = connection, + local.path = local.path, + commit.user = "me", + commit.email = "me@me.com" + ) + hash <- write_delim_git( + x = df, + file = file, + connection = git_con + ) + z <- commit(git_con, message = "test") + + Sys.chmod( + paste(git_con@Repository@path, git_con@LocalPath, sep = "/"), + mode = "0400" + ) + expect_error( + remove_files_git( + connection = git_con, + pattern = pattern + ), + "Error cleaning existing files" + ) + + Sys.chmod( + paste(git_con@Repository@path, git_con@LocalPath, sep = "/"), + mode = "0777" + ) + expect_true( + remove_files_git( + connection = git_con, + pattern = pattern + ), + "Error cleaning existing files" + ) + expect_true( + all(file.remove( + list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) + )) + ) + expect_true( + all(file.remove( + rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) + )) + ) +}) From 36e3d638e094c5472e377f433c4f4cbdb5a073c4 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Thu, 14 Dec 2017 22:19:39 +0100 Subject: [PATCH 31/33] use unlink() to remove files from unit tests Signed-off-by: Thierry Onkelinx --- tests/testthat/test_a_git_connection.R | 11 +---------- tests/testthat/test_b_write_delim_git.R | 11 +---------- tests/testthat/test_c_git_recent.R | 11 +---------- tests/testthat/test_c_git_sha.R | 11 +---------- tests/testthat/test_c_list_files_git.R | 11 +---------- tests/testthat/test_c_read_delim_git.R | 11 +---------- tests/testthat/test_d_remove_files_git.R | 22 ++-------------------- 7 files changed, 8 insertions(+), 80 deletions(-) diff --git a/tests/testthat/test_a_git_connection.R b/tests/testthat/test_a_git_connection.R index 096a65cc9..f016e6dda 100644 --- a/tests/testthat/test_a_git_connection.R +++ b/tests/testthat/test_a_git_connection.R @@ -191,13 +191,4 @@ expect_is( "git_connection" ) expect_identical(z@LocalPath, local.path) -expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) -) -expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) -) +expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) diff --git a/tests/testthat/test_b_write_delim_git.R b/tests/testthat/test_b_write_delim_git.R index 3c85e6966..4d70c3c6f 100644 --- a/tests/testthat/test_b_write_delim_git.R +++ b/tests/testthat/test_b_write_delim_git.R @@ -106,14 +106,5 @@ describe("write_delim_git()", { is_identical_to(paste(local.path, file, sep = "/")) ) }) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) diff --git a/tests/testthat/test_c_git_recent.R b/tests/testthat/test_c_git_recent.R index 7a4e60dd9..9b0c21d4a 100644 --- a/tests/testthat/test_c_git_recent.R +++ b/tests/testthat/test_c_git_recent.R @@ -50,14 +50,5 @@ test_that("git_recent works", { ) ) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) diff --git a/tests/testthat/test_c_git_sha.R b/tests/testthat/test_c_git_sha.R index a36e47c87..52a90dca1 100644 --- a/tests/testthat/test_c_git_sha.R +++ b/tests/testthat/test_c_git_sha.R @@ -60,14 +60,5 @@ test_that("git_sha returns the correct value", { stringsAsFactors = FALSE ) ) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) diff --git a/tests/testthat/test_c_list_files_git.R b/tests/testthat/test_c_list_files_git.R index 74813b17f..3d4ee06b9 100644 --- a/tests/testthat/test_c_list_files_git.R +++ b/tests/testthat/test_c_list_files_git.R @@ -84,14 +84,5 @@ describe("list_files_git()", { is_identical_to(character(0)) ) }) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) diff --git a/tests/testthat/test_c_read_delim_git.R b/tests/testthat/test_c_read_delim_git.R index 5b6c68e30..f2378ff76 100644 --- a/tests/testthat/test_c_read_delim_git.R +++ b/tests/testthat/test_c_read_delim_git.R @@ -59,14 +59,5 @@ describe("read_delim_git()", { is_equivalent_to(df) ) }) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) diff --git a/tests/testthat/test_d_remove_files_git.R b/tests/testthat/test_d_remove_files_git.R index 4a1bab3d8..a65453492 100644 --- a/tests/testthat/test_d_remove_files_git.R +++ b/tests/testthat/test_d_remove_files_git.R @@ -65,16 +65,7 @@ test_that("remove_files_git returns the correct value", { paste(local.path, file, sep = "/") ) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) test_that("returns an error when removing files fails", { @@ -129,14 +120,5 @@ test_that("returns an error when removing files fails", { ), "Error cleaning existing files" ) - expect_true( - all(file.remove( - list.files(tmpdir, all.files = TRUE, recursive = TRUE, full.names = TRUE) - )) - ) - expect_true( - all(file.remove( - rev(list.dirs(tmpdir, recursive = TRUE, full.names = TRUE)) - )) - ) + expect_identical(unlink(tmpdir, recursive = TRUE, force = TRUE), 0L) }) From e0f5a63e5cbd56912baba08eedafe727b2d91116 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Wed, 27 Dec 2017 11:00:06 +0100 Subject: [PATCH 32/33] write_delim_git() stages files when local path is the root of the repo --- R/write_delim_git.r | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/write_delim_git.r b/R/write_delim_git.r index 9d8f83758..d0aade56d 100644 --- a/R/write_delim_git.r +++ b/R/write_delim_git.r @@ -64,8 +64,12 @@ setMethod( ) # stage the file - filename.local <- paste(connection@LocalPath, file, sep = "/") - add(connection@Repository, filename.local) + if (connection@LocalPath == ".") { + filename.local <- file + } else { + filename.local <- paste(connection@LocalPath, file, sep = "/") + } + add(repo = connection@Repository, path = filename.local) return(hashfile(filename.full)) } From 3a44df00384e6cc7c7af03345f6125f6be2cbd39 Mon Sep 17 00:00:00 2001 From: ThierryO Date: Wed, 27 Dec 2017 11:17:13 +0100 Subject: [PATCH 33/33] write_delim_git() uses the force argument of add() --- R/git_recent.r | 2 +- R/git_sha.r | 2 +- R/list_files_git.r | 2 +- R/read_delim_git.r | 6 +++++- R/remove_files_git.r | 2 +- R/write_delim_git.r | 20 ++++++++++++++------ man/write_delim_git.Rd | 3 ++- 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/R/git_recent.r b/R/git_recent.r index c546e8f94..ca6cb4ea9 100644 --- a/R/git_recent.r +++ b/R/git_recent.r @@ -1,5 +1,5 @@ #' Get the info from the latest commit of a file -#' @inheritParams write_delim_git +#' @inheritParams read_delim_git #' @name git_recent #' @rdname git_recent #' @exportMethod git_recent diff --git a/R/git_sha.r b/R/git_sha.r index c8a5b3f4b..0722d88e9 100644 --- a/R/git_sha.r +++ b/R/git_sha.r @@ -1,5 +1,5 @@ #' Get the SHA of the files at the HEAD -#' @inheritParams write_delim_git +#' @inheritParams read_delim_git #' @name git_sha #' @rdname git_sha #' @exportMethod git_sha diff --git a/R/list_files_git.r b/R/list_files_git.r index baae7e14f..b637efd93 100644 --- a/R/list_files_git.r +++ b/R/list_files_git.r @@ -1,5 +1,5 @@ #' List the files in a path of a git repository -#' @inheritParams write_delim_git +#' @inheritParams read_delim_git #' @inheritParams base::list.files #' @name list_files_git #' @rdname list_files_git diff --git a/R/read_delim_git.r b/R/read_delim_git.r index b2f54ec00..89b16f9f9 100644 --- a/R/read_delim_git.r +++ b/R/read_delim_git.r @@ -1,5 +1,9 @@ #' Read a tab delimited file from a git repository -#' @inheritParams write_delim_git +#' @param file the name of the file +#' @param connection The path of a git repository or a \code{git_connection} +#' object +#' @param ... parameters passed to \code{git_connection()} when +#' \code{connection} is a path #' @name read_delim_git #' @rdname read_delim_git #' @exportMethod read_delim_git diff --git a/R/remove_files_git.r b/R/remove_files_git.r index 084098414..47901309b 100644 --- a/R/remove_files_git.r +++ b/R/remove_files_git.r @@ -1,6 +1,6 @@ #' Remove all the files in a path of a git repository #' -#' @inheritParams write_delim_git +#' @inheritParams read_delim_git #' @inheritParams base::list.files #' @name remove_files_git #' @rdname remove_files_git diff --git a/R/write_delim_git.r b/R/write_delim_git.r index d0aade56d..5f9cf2db9 100644 --- a/R/write_delim_git.r +++ b/R/write_delim_git.r @@ -2,12 +2,11 @@ #' #' The existing file will be overwritten. #' @param x the data.frame -#' @param file the name of the file -#' @param connection The path of a git repository or a \code{git_connection} -#' object #' @param ... parameters passed to \code{git_connection()} when -#' \code{connection} is a path +#' \code{connection} is a path. When \code{force} is available it is passed +#' to \code{add()}. #' @return the SHA1 of the file +#' @inheritParams read_delim_git #' @name write_delim_git #' @rdname write_delim_git #' @exportMethod write_delim_git @@ -38,7 +37,7 @@ setMethod( #' @rdname write_delim_git #' @aliases write_delim_git,git_connection-methods #' @importFrom methods setMethod -#' @importFrom assertthat assert_that is.string +#' @importFrom assertthat assert_that is.string has_name setMethod( f = "write_delim_git", signature = signature(connection = "git_connection"), @@ -69,7 +68,16 @@ setMethod( } else { filename.local <- paste(connection@LocalPath, file, sep = "/") } - add(repo = connection@Repository, path = filename.local) + dots <- list(...) + if (has_name(dots, "force")) { + add( + repo = connection@Repository, + path = filename.local, + force = dots$force + ) + } else { + add(repo = connection@Repository, path = filename.local) + } return(hashfile(filename.full)) } diff --git a/man/write_delim_git.Rd b/man/write_delim_git.Rd index 21a21e33d..f2293dacb 100644 --- a/man/write_delim_git.Rd +++ b/man/write_delim_git.Rd @@ -24,7 +24,8 @@ write_delim_git(x, file, connection, ...) object} \item{...}{parameters passed to \code{git_connection()} when -\code{connection} is a path} +\code{connection} is a path. When \code{force} is available it is passed +to \code{add()}.} } \value{ the SHA1 of the file