Skip to content

Commit

Permalink
Merge 3a44df0 into fc69738
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryO committed Dec 27, 2017
2 parents fc69738 + 3a44df0 commit a8783b5
Show file tree
Hide file tree
Showing 35 changed files with 1,621 additions and 2 deletions.
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.1),
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

0 comments on commit a8783b5

Please sign in to comment.