GithubHelp home page GithubHelp logo

ropensci / git2r Goto Github PK

View Code? Open in Web Editor NEW
214.0 10.0 56.0 11.54 MB

R bindings to the libgit2 library

Home Page: https://docs.ropensci.org/git2r

License: GNU General Public License v2.0

R 44.07% Shell 4.54% C 43.39% Makefile 0.56% M4 7.43%
libgit2 libgit2-library git git-client r rstats r-package

git2r's Introduction

R-CMD-check CRAN status CRAN RStudio mirror downloads Coverage Status

Introduction

The git2r package gives you programmatic access to Git repositories from R. Internally the package uses the libgit2 library which is a pure C implementation of the Git core methods. For more information about libgit2, check out libgit2's website (http://libgit2.github.com).

Suggestions, bugs, forks and pull requests are appreciated. Get in touch.

Installation

To install the version available on CRAN:

install.packages("git2r")

To install the development version of git2r, it's easiest to use the devtools package:

# install.packages("remotes")
library(remotes)
install_github("ropensci/git2r")

Usage

Repository

The central object in the git2r package is the S3 class git_repository. The following three methods can instantiate a repository; init, repository and clone.

Create a new repository

Create a new repository in a temporary directory using init

library(git2r)
#> Loading required package: methods
## Create a temporary directory to hold the repository
path <- tempfile(pattern="git2r-")
dir.create(path)

## Initialize the repository
repo <- init(path)

## Display a brief summary of the new repository
repo
#> Local:    /tmp/Rtmp7CXPlx/git2r-1ae2305c0e8d/
#> Head:     nothing commited (yet)
## Check if repository is bare
is_bare(repo)
#> [1] FALSE
## Check if repository is empty
is_empty(repo)
#> [1] TRUE

Create a new bare repository

## Create a temporary directory to hold the repository
path <- tempfile(pattern="git2r-")
dir.create(path)

## Initialize the repository
repo <- init(path, bare=TRUE)

## Check if repository is bare
is_bare(repo)
#> [1] TRUE

Clone a repository

## Create a temporary directory to hold the repository
path <- file.path(tempfile(pattern="git2r-"), "git2r")
dir.create(path, recursive=TRUE)

## Clone the git2r repository
repo <- clone("https://github.com/ropensci/git2r", path)
#> cloning into '/tmp/Rtmp7CXPlx/git2r-1ae27d811539/git2r'...
#> Receiving objects:   1% (24/2329),   12 kb
#> Receiving objects:  11% (257/2329),   60 kb
#> Receiving objects:  21% (490/2329),  100 kb
#> Receiving objects:  31% (722/2329),  125 kb
#> Receiving objects:  41% (955/2329),  237 kb
#> Receiving objects:  51% (1188/2329),  574 kb
#> Receiving objects:  61% (1421/2329), 1014 kb
#> Receiving objects:  71% (1654/2329), 1350 kb
#> Receiving objects:  81% (1887/2329), 1733 kb
#> Receiving objects:  91% (2120/2329), 2614 kb
#> Receiving objects: 100% (2329/2329), 2641 kb, done.
## Summary of repository
summary(repo)
#> Remote:   @ origin (https://github.com/ropensci/git2r)
#> Local:    master /tmp/Rtmp7CXPlx/git2r-1ae27d811539/git2r/
#>
#> Branches:          1
#> Tags:              0
#> Commits:         320
#> Contributors:      3
#> Ignored files:     0
#> Untracked files:   0
#> Unstaged files:    0
#> Staged files:      0
## List all references in repository
references(repo)
#> $`refs/heads/master`
#> [6fb440] master
#>
#> $`refs/remotes/origin/master`
#> [6fb440] origin/master
## List all branches in repository
branches(repo)
#> [[1]]
#> [6fb440] (Local) (HEAD) master
#>
#> [[2]]
#> [6fb440] (origin @ https://github.com/ropensci/git2r) master

Open an existing repository

## Open an existing repository
repo <- repository(path)

## Workdir of repository
workdir(repo)
#> [1] "/tmp/Rtmp7CXPlx/git2r-1ae27d811539/git2r/"
## List all commits in repository
commits(repo)[[1]] # Truncated here for readability
#> Commit:  6fb440133765e80649de8d714eaea17b114bd0a7
#> Author:  Stefan Widgren <[email protected]>
#> When:    2014-04-22 21:43:19
#> Summary: Fixed clone progress to end line with newline
## Get HEAD of repository
repository_head(repo)
#> [6fb440] (Local) (HEAD) master
## Check if HEAD is head
is_head(repository_head(repo))
#> [1] TRUE
## Check if HEAD is local
is_local(repository_head(repo))
#> [1] TRUE
## List all tags in repository
tags(repo)
#> list()

Configuration

config(repo, user.name="Git2r Readme", user.email="[email protected]")

## Display configuration
config(repo)
#> global:
#>         core.autocrlf=input
#> local:
#>         branch.master.merge=refs/heads/master
#>         branch.master.remote=origin
#>         core.bare=false
#>         core.filemode=true
#>         core.logallrefupdates=true
#>         core.repositoryformatversion=0
#>         remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
#>         remote.origin.url=https://github.com/ropensci/git2r
#>         [email protected]
#>         user.name=Git2r Readme

Commit

## Create a new file
writeLines("Hello world!", file.path(path, "test.txt"))

## Add file and commit
add(repo, "test.txt")
commit(repo, "Commit message")
#> Commit:  0a6af48cedf43208bde34230662280514e0956eb
#> Author:  Git2r Readme <[email protected]>
#> When:    2014-04-22 21:44:57
#> Summary: Commit message

License

The git2r package is licensed under the GPLv2.


git2r's People

Contributors

anniew avatar aoles avatar bfgray3 avatar cderv avatar daattali avatar epipping avatar gaborcsardi avatar ijlyttle avatar jdblischak avatar jefferis avatar jeroen avatar jimhester avatar karthik avatar krlmlr avatar pcarbo avatar petermeissner avatar qulogic avatar sckott avatar stewid avatar tdsmith avatar thierryo avatar trosendal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

git2r's Issues

git2r cookbook

@karthik and @sckott, would it be possible to create something similar to rOpenSci Cookbook for git2r. I believe that end users and the project could benefit from a better documentation. In the end I would like to have good vignettes, but I would also like to have something similar to http://www.pygit2.org/index.html. There are several examples from the test code and the manual that can be used to start creating recipes.

Create functions to generate reports

I think it would be really useful for people using the package to quickly generate commit summaries, contribution charts, etc with simple functions than by pasting the cumbersome code blocks we show in the README. So a few that come to mind:

contributions() # returns a ggplot like the example here. We can add a data_only = TRUE to return the data.frame and not the plot.
contribution_by_user() - similar, but does this by user and repo. Both can have user defined breaks (month, day, weeks).
Would also be good to move the word cloud into a function.

I have a prototype of all three. See here but let's fix the package issues first before moving on.

Note from R CMD check

* checking compiled code ... NOTE
File ‘/home/stefan/projects/packages/git2r/git2r.Rcheck/git2r/libs/git2r.so’:
  Found ‘stdout’, possibly from ‘stdout’ (C)
    Object: ‘libgit2/diff_print.o’

Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console.

Identify location and change to Rprintf

Does `add()` do folders too?

@stewid Curios if add only does files. tried this recently, but couldn't add a folder, just got an error message. Should it work to add a folder too?

Features to add

List and discuss features to add before submitting to CRAN.

Here's one simple idea for adding latest hash to a paper.

{r, eval = TRUE,  echo = FALSE}
library(git2r)
# Most likely that a user will have the ms inside a Git repo
# So current working directory should be manuscript's repo.
repo <- "."
commit <- head(repo)@hex
commit_link <- sprintf("[%s](%s)", commit, remote_url(repo))

Then in the actual manuscript (perhaps mixed in with the acknowledgments.)

The status of all files at the writing of this manuscript can be found in commit r commit_link.

The r commit_link will get replaced by something like this:

"[9de58983b5282459f215b3be8d838d8670cdd556](https://github.com/ropensci/AntWeb)"

but rendered in markdown (and eventually PDF) as:

9de58983b5282459f215b3be8d838d8670cdd556

Update to roxygen2 version 4?

Interested in updating to the newest version of roxygen2? The process is pretty easy. just run roxygen2::upgradeRoxygen() in the dir for the repo, then document() or roxygenize() to update after installing the new roxgyen2 version.

Hadley says to install via devtools::install_github("klutometis/roxygen")

Documentation issues

Even without upgrading roxygen2 we have this issue.

> check_doc()
show-git_repository-method.Rd: non-ASCII input and no declared encoding
show-git_signature-method.Rd: non-ASCII input and no declared encoding
summary-git_repository-method.Rd: non-ASCII input and no declared encoding
problems found in ‘show-git_repository-method.Rd’, ‘show-git_signature-method.Rd’, ‘summary-git_repository-method.RdRd files with duplicated alias 'remote_url':
  ‘remote_url-methods.Rd’ ‘remote_url.RdRd files with duplicated alias 'show,git_signature-method':
  ‘git_signature-class.Rd’ ‘show-git_signature-method.RdDocumented arguments not in \usage in documentation object 'commit-methods':
  ‘tree’ ‘parents’

Objects in \usage without \alias in documentation object 'head-methods':
  ‘\S4method{head}{git_repository}’

Undocumented arguments in documentation object 'remote_url-methods'
  ‘remote’

Undocumented arguments in documentation object 'remote_url'
  ‘remote’

Clone under Windows...

The following, under windows 7:

clone(url='https://github.com/ropensci/git2r.git',
          local_path=paste0(getwd(),'/git2r'))

Produces:

Error in clone(url = "https://github.com/ropensci/git2r.git", local_path = paste0(getwd(),: 
Error 2: SSL is not supported by this copy of libgit2.

I suppose this is a Windows problem because you seem to have solved the SSL issue under Linux

Goodluck,
Thomas

Issues with documentation and passing checks

There seems to be a big issue with how we generate documentation and that seems to break the package entirely. I imagine you're still running roxygenise(). Right now, for this version of the package, I can install, and pass check correctly.

As soon as I add any new function, run document("."), bam it all breaks. So we should agree on a method otherwise neither of us will have a working package every time we make a change.

I also think that with devtools, there is no longer a collate field (don't see it any of our recent packages). Running document also removes your collate field, and so does adding an import.

Can we resolve this before moving forward?

Failing test on Windows

R CMD check git2r on Windows:

 :
 :
ERROR
Running the tests in 'tests/blob.R' failed.
Last 13 lines of output:
  +                       NA_character_,
  +                       "d670460b4b4aece5915caf5c68d12f560a9fe3e4")))
  > stopifnot(identical(hash(character(0)), character(0)))
  >
  > ##
  > ## Hash file
  > ##
  > writeLines("Hello, world!", file.path(path, "test-1.txt"))
  > writeLines("test content", file.path(path, "test-2.txt"))
  > stopifnot(identical(hash("Hello, world!\n"),
  +                     hashfile(file.path(path, "test-1.txt"))))
  Error: identical(hash("Hello, world!\n"), hashfile(file.path(path, "test-1.txt
"))) is not TRUE
  Execution halted

writeLines gives LF on Unix/Linux, CRLF on Windows, and therefore the test fails.

Use writeChar and a binary connection instead.

knitr integration

Hi! Was just discussing this with Karthik:

Would love to have an integration with the knit() command such that you can add in a commit message there if you have a git push in the knitr code.

Here is my current workflow:
-- Write knitr .Rmd document
-- Update source script (outside knitr document)
-- Re-knit document (README.Rmd) (which pulls in updated data/results from source script)
-- Commit via command line with updated commit message (github README.md now updated)
-- Push to github repo, see new README.md report!

How I'm envisioning this working:
-- Write knitr document with git push integrated into it using git2r command.
-- Update source script
-- re-knit document using this command: knit("README.Rmd", commit_message = "updated data")
-- Document has been knitted, git committed and pushed to the repo!

Would this be possible? Would it be a useful workflow? For me the advantage is that I'm often editing the source code, not the knitr document, so having it in the knit() command saves me the hassle of reopening the readme document and changing the commit message.

Prefix C object names with git2r_

On some platforms the names from different shared objects can clash, and names like checkout and clone are not specific enough.

This is not urgent, just something to keep in mind if the package will be submitted to cran.

Registering native routines

See http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Registering-native-routines

  • Fix typo in R init function: change R_init_gitr to R_init_git2r in src/git2r.c
  • Change .Call in R code to use the registered symbol i.e. change .Call("git2r_module_name" to .Call(git2r_module_name
  • Improve extract_git2r_calls in tools/build_Makevars.r to check that every .Call have the form .Call(git2r_module_name instead of .Call("git2r_module_name"
  • Add functionality in tools/build_Makevars.r to automatically build src/git2r.c from the information collected in extract_git2r_calls.

Warning from 'R CMD check git2r' on Windows

* checking compiled code ... WARNING
File 'C:/Users/Stefan/projects/git2r/git2r.Rcheck/git2r/libs/i386/git2r.dll':
  Found 'abort', possibly from 'abort' (C), 'runtime' (Fortran)
    Object: 'regex/regex.o'
File 'C:/Users/Stefan/projects/git2r/git2r.Rcheck/git2r/libs/x64/git2r.dll':
  Found 'abort', possibly from 'abort' (C), 'runtime' (Fortran)
    Object: 'regex/regex.o'

Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console.

Replace abort with Rf_error in src/regex/regcomp.c, line 558 & 1097

Add method hash

Add method hash to determine the sha1 hex from a string without writing the blob to the odb.

Add verbose argument to status method

@sckott I ran RRT::git_add_commit and noticed that git2r::status displayed the status several times. Maybe you would like to turn that off and only fetch the list? I was thinking of adding an argument verbose with default value TRUE to determine if to display the status or only return the list.

## Display status and return list
st <- git2r::status(repo)

## Only return the list
st <- git2r::status(repo, verbose=FALSE)

New function idea from the hackathon

git_cache()

Will automatically take all (or specified) objects from the GlobalEnv (except functions) and write them as a stash. This would allow data to be cached temporarily in a git managed project. Then git_uncache() or a better named function would the n apply the stash.

Commit with no staged changes.

Example of commiting not throwing an error when there is nothing to commit

library(git2r)

Make some data

data<-data.frame('ID'=1:10,
'x'= sample(seq(from=0,to=1,length.out=10),replace=TRUE,size=10),
'y'= sample(seq(from=0,to=1,length.out=10),replace=TRUE,size=10))

oldwd<-getwd() #Preserve the working directory
setwd(tempdir()) #Go to a temporary environment
init(getwd()) #init a repo
repo<-repository(getwd()) #make and object of it
write.csv(data, file='points.csv', row.names=FALSE) #Write the data to the repo
add(repo, 'points.csv') #Stage the file
commit(repo, message='initial commit') #Make a commit - Expect no error
data$x[4]<-data$x[4]+0.01 #Change the data
write.csv(data, file='points.csv', row.names=FALSE) #Overwrite the commited file
add(repo, 'points.csv') #Stage the change
commit(repo, message='second commit') #Make a commit - Expect no error

Now we have a repo with one file and two

commits and the repo status has no

working directory changes. If I try to

commit no I expect an error, perhaps 'No

staged changes to commit'

commit(repo, message='initial commit') #Make a commit - Expect error

I get no error

setwd(oldwd) #go back to old environment

Microsoft Windows....

I built the package on Linux and everything worked well.
I tried on Windows 7 and it built fine but it throws errors whenever the C code checks if the repository object is valid. Therefore, the git init function returns an error but does successfully initialize a repo. Other functions in git2R check for the valid repo before running the rest of the function and therefore do not work. I thought this might help you get to the root of the Windows compatibility problem.
Good luck.

push over ssh

push(repo, "origin", "refs/heads/master:refs/heads/master")
# Error in push(repo, "origin", "refs/heads/master:refs/heads/master") : 
#   Error: This transport isn't implemented. Sorry
remotes(repo)
# [1] "origin"   "upstream"
remote_url(repo, "origin")
# [1] "[email protected]:metacran/git2r.git"

I think this should work, at least openssl is found and used during compilation:

...
checking for inflate in -lz... yes
checking for SSL_library_init in -lssl... yes
checking for EVP_EncryptInit in -lcrypto... yes
...
~/works/git2r (master)$ otool -L src/git2r.so 
src/git2r.so:
    git2r.so (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 50.0.0)
    /usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 50.0.0)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
    /Library/Frameworks/R.framework/Versions/3.1/Resources/lib/libR.dylib (compatibility version 3.1.0, current version 3.1.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 855.16.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)

System is OSX Mavericks. Any ideas why this might happen?

Warning from 'R CMD check git2r' on Windows

R CMD check --no-manual --no-vignettes --no-build-vignettes git2r_0.0.8
 :
 :
* using R version 3.1.1 (2014-07-10)
* using platform: i386-w64-mingw32 (32-bit)
 :
 :
* checking whether package 'git2r' can be installed ... WARNING
Found the following significant warnings:
  regex/regcomp.c:3782:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 :
 :

Small issue with documentation

Do you use roxygenise() directly? I use:

library(devtools)
document(".")
check_doc()

These two approaches end up updating all the docs. If we both stick to document(".") it would only update newly changed files.

Create a patch to pass R CMD check

* checking compiled code ... NOTE
File ‘/home/stefan/projects/packages/git2r/git2r.Rcheck/git2r/libs/git2r.so’:
  Found ‘printf’, possibly from ‘printf’ (C)
    Objects: ‘libgit2/cache.o’, ‘libgit2/util.o’
  Found ‘putchar’, possibly from ‘putchar’ (C)
    Object: ‘libgit2/util.o’
  Found ‘stdout’, possibly from ‘stdout’ (C)
    Object: ‘libgit2/diff_print.o’

Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console.

Create a patch that:

  • replace printf with Rprintf in src/libgit2/cache.c and src/libgit2/util.c
  • remove stdout from src/libgit2/diff_print.c: (git_diff_print_callback__to_file_handle) and return error code (-1) instead of using stdout if FILE pointer is NULL.

Running R CMD check with the changed code gives:

* checking compiled code ... OK

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.