Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data repo #303

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
013fe48
render documentation
ThierryO Dec 9, 2017
fbbb60b
Add is.git()
ThierryO Dec 9, 2017
332ca57
Add gitConnection class
ThierryO Dec 9, 2017
9e00b6d
Add write_delim_git
ThierryO Dec 9, 2017
8b63cf6
Add git_connection()
ThierryO Dec 10, 2017
2faf389
Add read_delim_git()
ThierryO Dec 10, 2017
40d4fa4
Add list_files_git()
ThierryO Dec 10, 2017
ae3601b
Add remove_files_git()
ThierryO Dec 10, 2017
b9c5669
Add Thierry Onkelinx as author
ThierryO Dec 10, 2017
888329e
Add git_sha()
ThierryO Dec 10, 2017
9ab7f0e
Add git_recent()
ThierryO Dec 10, 2017
aea93d7
Fix git_connection()
ThierryO Dec 10, 2017
98329ff
Fix gitConnection class
ThierryO Dec 10, 2017
e9e513a
test git_connection()
ThierryO Dec 10, 2017
e9c08f7
Fix gitConnection class
ThierryO Dec 10, 2017
77e67a1
test list_files_git()
ThierryO Dec 10, 2017
4b3c019
test write_delim_git()
ThierryO Dec 10, 2017
5d72b62
test read_delim_git()
ThierryO Dec 10, 2017
f0af212
run unit tests in a sensible order
ThierryO Dec 11, 2017
eeb7e0f
Add unit tests for git_sha()
ThierryO Dec 11, 2017
9041e50
improve git_connection()
ThierryO Dec 11, 2017
b827e34
remove unit test repos after running tests
ThierryO Dec 12, 2017
13ca9a3
git_sha() tests more efficiently for missing commits
ThierryO Dec 12, 2017
c95efc7
add unit tests for remove_files_git()
ThierryO Dec 12, 2017
d76158b
add unit test for git_recent()
ThierryO Dec 12, 2017
e9f6372
add commit method for gitConnection class
ThierryO Dec 12, 2017
65691ac
rename gitConnection class to git_connection class
ThierryO Dec 12, 2017
8a7c870
rename gitCredentials class to git_credentials class
ThierryO Dec 12, 2017
1267d58
add more unit tests for remove_files_git
ThierryO Dec 12, 2017
6efa0b1
add more unit tests for remove_files_git()
ThierryO Dec 14, 2017
36e3d63
use unlink() to remove files from unit tests
ThierryO Dec 14, 2017
e0f5a63
write_delim_git() stages files when local path is the root of the repo
ThierryO Dec 27, 2017
3a44df0
write_delim_git() uses the force argument of add()
ThierryO Dec 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
^\.Rproj\.user$
^windows
^.*\.a$
^man-roxygen$
13 changes: 12 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ BugReports: https://github.com/ropensci/git2r/issues
Maintainer: Stefan Widgren <stefan.widgren@gmail.com>
Author: See AUTHORS file.
Imports:
assertthat,
graphics,
utils
Depends:
R (>= 3.0.2),
methods
Suggests:
dplyr,
testthat,
getPass
Type: Package
LazyData: true
Expand All @@ -26,7 +29,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'
Expand All @@ -40,8 +43,13 @@ Collate:
'diff.r'
'fetch.r'
'git2r.r'
'git_connection.r'
'git_recent.r'
'git_sha.r'
'index.r'
'is_git.R'
'libgit2.r'
'list_files_git.r'
'merge.r'
'note.r'
'odb.r'
Expand All @@ -50,9 +58,11 @@ Collate:
'punch_card.r'
'refspec.r'
'push.r'
'read_delim_git.r'
'reference.r'
'reflog.r'
'remote.r'
'remove_files_git.r'
'repository.r'
'reset.r'
'revparse.r'
Expand All @@ -63,4 +73,5 @@ Collate:
'time.r'
'tree.r'
'when.r'
'write_delim_git.r'
RoxygenNote: 6.0.1
24 changes: 24 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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)
export(is_commit)
Expand All @@ -24,6 +26,7 @@ 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)
Expand Down Expand Up @@ -69,6 +72,8 @@ exportMethods(diff)
exportMethods(discover_repository)
exportMethods(fetch)
exportMethods(fetch_heads)
exportMethods(git_recent)
exportMethods(git_sha)
exportMethods(hash)
exportMethods(hashfile)
exportMethods(head)
Expand All @@ -84,6 +89,7 @@ exportMethods(is_local)
exportMethods(is_merge)
exportMethods(is_shallow)
exportMethods(length)
exportMethods(list_files_git)
exportMethods(lookup)
exportMethods(merge)
exportMethods(merge_base)
Expand All @@ -98,6 +104,7 @@ exportMethods(plot)
exportMethods(pull)
exportMethods(punch_card)
exportMethods(push)
exportMethods(read_delim_git)
exportMethods(references)
exportMethods(reflog)
exportMethods(remote_add)
Expand All @@ -107,6 +114,7 @@ exportMethods(remote_rename)
exportMethods(remote_set_url)
exportMethods(remote_url)
exportMethods(remotes)
exportMethods(remove_files_git)
exportMethods(repository)
exportMethods(reset)
exportMethods(revparse_single)
Expand All @@ -123,13 +131,29 @@ exportMethods(tags)
exportMethods(tree)
exportMethods(when)
exportMethods(workdir)
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)
importFrom(graphics,plot.new)
importFrom(graphics,plot.window)
importFrom(graphics,symbols)
importFrom(methods,new)
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,read.delim)
importFrom(utils,read.table)
importFrom(utils,sessionInfo)
importFrom(utils,write.table)
useDynLib(git2r, .registration=TRUE)
51 changes: 51 additions & 0 deletions R/S4_classes.r
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,54 @@ setClass("git_merge_result",
conflicts = "logical",
sha = "character")
)

#' @importFrom methods setClassUnion
setClassUnion("git_credentials", c("NULL", "cred_user_pass", "cred_ssh_key"))

#' The git_connection 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 git_connection-class
#' @rdname git_connection-class
#' @exportClass git_connection
#' @aliases git_connection-class
#' @importFrom methods setClass
#' @docType class
#' @template thierry
setClass(
Class = "git_connection",
representation = representation(
Repository = "git_repository",
LocalPath = "character",
Credentials = "git_credentials",
CommitUser = "character",
CommitEmail = "character"
)
)

#' @importFrom methods setValidity
#' @importFrom assertthat assert_that is.string has_name
#' @importFrom utils file_test
setValidity(
"git_connection",
function(object){
assert_that(is.string(object@CommitUser))
assert_that(is.string(object@CommitEmail))
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"))
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)
}
)
17 changes: 17 additions & 0 deletions R/commit.r
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ setMethod("commit",
}
)

##' @rdname commit-methods
##' @export
##' @include S4_classes.r
setMethod("commit",
signature(repo = "git_connection"),
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
Expand Down
109 changes: 109 additions & 0 deletions R/git_connection.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#' Open a git connection
#' @name git_connection
#' @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
#' 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
#' @template thierry
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))
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)
assert_that(
dir.exists(paste(repo.path, local.path, sep = "/")),
msg = paste(local.path, "is not a directory")
)

if (missing(key) & missing(username)) {
return(
new(
"git_connection",
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(
"git_connection",
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(
"git_connection",
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(
"git_connection",
Repository = repo,
LocalPath = local.path,
Credentials = cred_user_pass(
username = username,
password = password
),
CommitUser = commit.user,
CommitEmail = commit.email
)
)
}
65 changes: 65 additions & 0 deletions R/git_recent.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' Get the info from the latest commit of a file
#' @inheritParams read_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 = "git_connection"),
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(
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
))
}
)
Loading