Skip to content
Permalink
Browse files
Add 'repository_head' function
Signed-off-by: Stefan Widgren <stefan.widgren@gmail.com>
  • Loading branch information
stewid committed Feb 26, 2018
1 parent ea1b9b2 commit 24019226b771b10b5ccda48e1fece5880cc6ddbe
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 52 deletions.
@@ -129,6 +129,7 @@ export(remote_set_url)
export(remote_url)
export(remotes)
export(repository)
export(repository_head)
export(reset)
export(revparse_single)
export(rm_file)
@@ -148,7 +148,7 @@ branch_remote_name <- function(branch = NULL) {
##' push(repo, "origin", "refs/heads/master")
##'
##' ## Get remote url of tracking branch to branch 'master'
##' branch_remote_url(branch_get_upstream(head(repo)))
##' branch_remote_url(branch_get_upstream(repository_head(repo)))
##' }
branch_remote_url <- function(branch = NULL) {
.Call(git2r_branch_remote_url, branch)
@@ -177,7 +177,7 @@ branch_remote_url <- function(branch = NULL) {
##'
##' ## Rename 'master' branch to 'dev'
##' branches(repo)
##' branch_rename(head(repo), "dev")
##' branch_rename(repository_head(repo), "dev")
##' branches(repo)
##' }
branch_rename <- function(branch = NULL, name = NULL, force = FALSE) {
@@ -204,7 +204,7 @@ branch_rename <- function(branch = NULL, name = NULL, force = FALSE) {
##' commit(repo, "First commit message")
##'
##' ## Get target (sha) pointed to by 'master' branch
##' branch_target(head(repo))
##' branch_target(repository_head(repo))
##' }
branch_target <- function(branch = NULL) {
.Call(git2r_branch_target, branch)
@@ -241,7 +241,7 @@ branch_target <- function(branch = NULL) {
##' push(repo, "origin", "refs/heads/master")
##'
##' ## Get remote tracking branch
##' branch_get_upstream(head(repo))
##' branch_get_upstream(repository_head(repo))
##' }
branch_get_upstream <- function(branch = NULL) {
.Call(git2r_branch_get_upstream, branch)
@@ -279,13 +279,13 @@ branch_get_upstream <- function(branch = NULL) {
##' push(repo, "origin", "refs/heads/master")
##'
##' ## Unset remote remote tracking branch
##' branch_get_upstream(head(repo))
##' branch_set_upstream(head(repo), NULL)
##' branch_get_upstream(head(repo))
##' branch_get_upstream(repository_head(repo))
##' branch_set_upstream(repository_head(repo), NULL)
##' branch_get_upstream(repository_head(repo))
##'
##' ## Set remote tracking branch
##' branch_set_upstream(head(repo), "origin/master")
##' branch_get_upstream(head(repo))
##' branch_set_upstream(repository_head(repo), "origin/master")
##' branch_get_upstream(repository_head(repo))
##' }
branch_set_upstream <- function(branch = NULL, name) {
if (missing(name))
@@ -215,7 +215,7 @@ checkout <- function(object = NULL,

if (isTRUE(create)) {
## Create branch
commit <- lookup(object, branch_target(head(object)))
commit <- lookup(object, branch_target(repository_head(object)))
checkout_branch(branch_create(commit, branch), force)
return(invisible(NULL))
}
@@ -213,7 +213,7 @@ commits <- function(repo = ".",
result <- list()

## Get latest commit
x <- lookup(repo, branch_target(head(repo)))
x <- lookup(repo, branch_target(repository_head(repo)))

## Repeat until no more parent commits
repeat {
@@ -386,10 +386,10 @@ is_commit <- function(object) {
##' merge(repo, "fix")
##'
##' ## Display parents of last commit
##' parents(lookup(repo, branch_target(head(repo))))
##' parents(lookup(repo, branch_target(repository_head(repo))))
##'
##' ## Check that last commit is a merge
##' is_merge(lookup(repo, branch_target(head(repo))))
##' is_merge(lookup(repo, branch_target(repository_head(repo))))
##' }
is_merge <- function(commit = NULL) {
length(parents(commit)) > 1
@@ -91,7 +91,7 @@ pull <- function(repo = ".", credentials = NULL, merger = NULL) {
repo <- lookup_repository(repo)
if (is.null(merger))
merger <- default_signature(repo)
current_branch <- head(repo)
current_branch <- repository_head(repo)

if (is.null(current_branch))
stop("'branch' is NULL")
@@ -88,7 +88,7 @@ push <- function(object = ".",
}

if (all(is.null(name), is.null(refspec))) {
b <- head(object)
b <- repository_head(object)
upstream <- branch_get_upstream(b)
if (is.null(upstream)) {
stop("The branch '", b@name, "' that you are ",
@@ -51,7 +51,7 @@ get_refspec <- function(repo = NULL, remote = NULL, spec = NULL, opts = NULL) {
if (is.null(remote)) {
remote <- .Call(git2r_config_get_string,
repo,
paste0("branch.", head(repo)$name, ".remote"))
paste0("branch.", repository_head(repo)$name, ".remote"))
if (is.null(remote))
remote <- "origin"
}
@@ -139,13 +139,13 @@ as.data.frame.git_repository <- function(x, ...) {
##' branches(repo)
##'
##' ## Get HEAD of repository
##' head(repo)
##' repository_head(repo)
##'
##' ## Check if HEAD is head
##' is_head(head(repo))
##' is_head(repository_head(repo))
##'
##' ## Check if HEAD is local
##' is_local(head(repo))
##' is_local(repository_head(repo))
##'
##' ## List all tags in repository
##' tags(repo)
@@ -294,9 +294,36 @@ clone <- function(url = NULL,
##' head(repo)
##' }
head.git_repository <- function(x, ...) {
stop("test")
.Call(git2r_repository_head, x)
}

##' Get HEAD for a repository
##'
##' @template repo-param
##' @return NULL if unborn branch or not found. A git_branch if not a
##' detached head. A git_commit if detached head
##' @export
##' @examples
##' \dontrun{
##' ## Create and initialize a repository in a temporary directory
##' path <- tempfile(pattern="git2r-")
##' dir.create(path)
##' repo <- init(path)
##' config(repo, user.name="Alice", user.email="alice@@example.org")
##'
##' ## Create a file, add and commit
##' writeLines("Hello world!", file.path(path, "example.txt"))
##' add(repo, "example.txt")
##' commit(repo, "Commit message")
##'
##' ## Get HEAD of repository
##' repository_head(repo)
##' }
repository_head <- function(repo = ".") {
.Call(git2r_repository_head, repo)
}

##' Check if repository is bare
##'
##' @template repo-param
@@ -525,20 +552,20 @@ lookup <- function(repo = ".", sha = NULL) {

##' @export
print.git_repository <- function(x, ...) {
if (any(is_empty(x), is.null(head(x)))) {
if (any(is_empty(x), is.null(repository_head(x)))) {
cat(sprintf("Local: %s\n", workdir(x)))
cat("Head: nothing commited (yet)\n")
} else {
if (is_detached(x)) {
cat(sprintf("Local: (detached) %s\n", workdir(x)))

h <- head(x)
h <- repository_head(x)
} else {
cat(sprintf("Local: %s %s\n",
head(x)$name,
repository_head(x)$name,
workdir(x)))

h <- head(x)
h <- repository_head(x)
u <- branch_get_upstream(h)
if (!is.null(u)) {
rn <- branch_remote_name(u)
@@ -548,7 +575,7 @@ print.git_repository <- function(x, ...) {
branch_remote_url(u)))
}

h <- lookup(x, branch_target(head(x)))
h <- lookup(x, branch_target(repository_head(x)))
}

cat(sprintf("Head: [%s] %s: %s\n",

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -39,8 +39,8 @@ stopifnot(identical(length(branches(repo)), 1L))
stopifnot(identical(is_head(branches(repo)[[1]]), TRUE))
stopifnot(identical(is_local(branches(repo)[[1]]), TRUE))
stopifnot(identical(branches(repo)[[1]]$name, "master"))
stopifnot(identical(branches(repo)[[1]], head(repo)))
stopifnot(identical(branches(repo)$master, head(repo)))
stopifnot(identical(branches(repo)[[1]], repository_head(repo)))
stopifnot(identical(branches(repo)$master, repository_head(repo)))

## Create a branch
b <- branch_create(commit.1, name = "test")
@@ -75,7 +75,7 @@ stopifnot(identical(c("Lorem ipsum dolor sit amet, consectetur adipisicing elit,

## Checkout previous branch
checkout(repo_2, "-")
stopifnot(identical(head(repo_2)$name, "master"))
stopifnot(identical(repository_head(repo_2)$name, "master"))

## Cleanup
unlink(path_bare, recursive=TRUE)
@@ -74,7 +74,7 @@ commit.3 <- commit(repo, "Third commit message")

## Check HEAD
stopifnot(identical(is_detached(repo), FALSE))
stopifnot(identical(head(repo)$name, "master"))
stopifnot(identical(repository_head(repo)$name, "master"))

## Check show and summary
repo
@@ -83,7 +83,7 @@ summary(repo)
## Checkout first commit
checkout(commit.1, TRUE)
stopifnot(identical(is_detached(repo), TRUE))
stopifnot(identical(head(repo), commit.1))
stopifnot(identical(repository_head(repo), commit.1))
stopifnot(identical(readLines(file.path(path, "test.txt")), "Hello world!"))

## Check show and summary
@@ -69,7 +69,7 @@ config(repo_2, user.name="Bob", user.email="bob@example.org")

## Check branch and commits
stopifnot(identical(length(commits(repo_2)), 3L))
stopifnot(identical(head(repo_2)$name, "dev"))
stopifnot(identical(repository_head(repo_2)$name, "dev"))

## Cleanup
unlink(path_bare, recursive=TRUE)
@@ -62,7 +62,7 @@ stopifnot(identical(sapply(commits(repo), function(x) length(parents(x))),
c(2L, 1L, 1L, 1L, 0L)))

## Check that last commit is a merge
stopifnot(is_merge(lookup(repo, branch_target(head(repo)))))
stopifnot(is_merge(lookup(repo, branch_target(repository_head(repo)))))

## Check that metadata associated with merge is removed
stopifnot(!file.exists(file.path(path, ".git", "MERGE_HEAD")))

0 comments on commit 2401922

Please sign in to comment.