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

methods for committer, sha, branch, commit message #135

Open
benmarwick opened this issue May 8, 2015 · 9 comments
Open

methods for committer, sha, branch, commit message #135

benmarwick opened this issue May 8, 2015 · 9 comments

Comments

@benmarwick
Copy link

Are there methods for these already?I'm not having much luck finding them. I'd like to have a colophon with git info like so, but feel a bit uncomfortable using @:

```{r, echo=FALSE}
library("git2r")
# create repository
path <- tempfile(pattern="test_repo_")
dir.create(path)
repo <- init(path)
# configure user
config(repo, user.name="Alice", user.email="[email protected]")
## Write to a file and commit
writeLines("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
           file.path(path, "example.txt"))
add(repo, "example.txt")
commit(repo, "First commit message")
last_commit <- commits(repo)[[1]]

The current git commit of this file is r last_commit@sha, which is on the r branches(repo)[[1]]@name branch and was made by r last_commit@committer@name on r when(last_commit). The current commit message is "r last_commit@summary".

Which renders nicely as:

The current git commit of this file is 0d0a2e8b40dbcc2b4ad5125fa6e1f08ec5244160, which is on the master branch and was made by Alice on 2015-05-07 23:17:20. The current commit message is “First commit message”.

@stewid
Copy link
Member

stewid commented May 8, 2015

Nice!

You can use n=1 to fetch the last commit: last_commit <- commits(repo, n=1) and head(repo)@name instead of branches(repo)[[1]]@name

You are right, there are no methods for committer, sha, branch name and commit message.

How would you ideally write?

@stewid
Copy link
Member

stewid commented May 8, 2015

My misstake, commits(repo, n=1) returns a list. Maybe n=1 should be treated as a special case and return the git_commit object and not a list?

@gaborcsardi
Copy link
Contributor

@stewid IMHO that is a bad idea. Whenever I call commits() with an n parameter, I would need to check if it is one. I suggest you always return a list, and add a last_commit (or whatever) function to get the last commit.

@stewid
Copy link
Member

stewid commented May 8, 2015

Thanks @gaborcsardi, good point. Let's keep commits() as it is now and add a wrapper last_commit() to commits(n=1)[[1]]

@benmarwick
Copy link
Author

I've had a go at adding the methods I'm after, let me know if they're consistent with your plans. They work for me in this context:

```{r, echo=FALSE}
library("git2r")
# create repository
path <- tempfile(pattern="test_repo_")
dir.create(path)
repo <- init(path)
# configure user
config(repo, user.name="Alice", user.email="[email protected]")

## Write to a file and commit
writeLines("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
           file.path(path, "example.txt"))
add(repo, "example.txt")
commit(repo, "First commit message")
last_commit <- commits(repo)[[1]]

The current git commit of this file is r last_commit@sha, which is on the r branches(repo)[[1]]@name branch and was made by r last_commit@committer@name on r when(last_commit). The current commit message is "r last_commit@summary"

Proposed methods to avoid use of @ when getting basic info from a specific commit:

sha(last_commit)
branch(last_commit)
committer(last_commit)
message(last_commit)

@stewid
Copy link
Member

stewid commented May 10, 2015

The two methods sha and committer are good additions that I'm happy to add. It might be that it's the author you want to output, see below for an example from https://github.com/cran/git2r where @stewid is the author but @gaborcsardi the committer. Therefore I think we should also add an author method.

library(git2r)
## Clone git2r from github.com/cran/git2r
path <- tempfile()
dir.create(path)
repo <- clone("https://github.com/cran/git2r.git", path, progress = FALSE)
## View content of last commit
str(commits(repo, n = 1)[[1]])
#> Formal class 'git_commit' [package "git2r"] with 6 slots
#>   ..@ sha      : chr "278457e4529a5d8fc33bdb2ca13f72929ce03f06"
#>   ..@ author   :Formal class 'git_signature' [package "git2r"] with 3 slots
#>   .. .. ..@ name : chr "Stefan Widgren"
#>   .. .. ..@ email: chr "[email protected]"
#>   .. .. ..@ when :Formal class 'git_time' [package "git2r"] with 2 slots
#>   .. .. .. .. ..@ time  : num 1.43e+09
#>   .. .. .. .. ..@ offset: num 0
#>   ..@ committer:Formal class 'git_signature' [package "git2r"] with 3 slots
#>   .. .. ..@ name : chr "Gabor Csardi"
#>   .. .. ..@ email: chr "[email protected]"
#>   .. .. ..@ when :Formal class 'git_time' [package "git2r"] with 2 slots
#>   .. .. .. .. ..@ time  : num 1.43e+09
#>   .. .. .. .. ..@ offset: num 0
#>   ..@ summary  : chr "version 0.10.1"
#>   ..@ message  : chr "version 0.10.1\n"
#>   ..@ repo     :Formal class 'git_repository' [package "git2r"] with 1 slot
#>   .. .. ..@ path: chr "/tmp/RtmpZy9wXF/file64ea57998063"

The git commit message is divided into the two slots message and and summary in the S4 class git_commit. The slots are intialized with the libgit2 methods git_commit_message and git_commit_summary. I therefore think a method named message returning the summary from the commit is not what I expect. Moreover, message is also a method in base R. Since summary is already in use to give a summary of the commit (see below), we can not use that name either. How about commit_summary?

## View summary of last commit
summary(commits(repo, n = 1)[[1]])
#> Commit:  278457e4529a5d8fc33bdb2ca13f72929ce03f06
#> Author:  Stefan Widgren <[email protected]>
#> When:    2015-05-07
#>
#>       version 0.10.1
#>
#> DESCRIPTION                                 | - 19 + 18  in  2 hunks
#> MD5                                         | -286 +301  in 17 hunks
#> NAMESPACE                                   | -  1 +  9  in  5 hunks
#> NEWS                                        | -  1 + 93  in  1 hunk
#> R/branch.r                                  | - 40 +  6  in  4 hunks
#> R/bundle_r_package.r                        | -  0 + 79  in  1 hunk
#> R/checkout.r                                | - 42 + 27  in  6 hunks
#> R/commit.r                                  | - 18 + 75  in  7 hunks

[TRUNCATED]

Insted of branch, how about branch_name to be consistent with commit_summary?

@cderv
Copy link
Contributor

cderv commented Aug 1, 2019

Thanks @gaborcsardi, good point. Let's keep commits() as it is now and add a wrapper last_commit() to commits(n=1)[[1]]

Would it still be consider to add a last_commit wrapper ?
I am still in needs for that each time I want to create a branch from the head of the repo.
I may have miss a way to do that though... 🤔

@stewid
Copy link
Member

stewid commented Aug 1, 2019

@cderv last_commit() already exists, see ?last_commit

@cderv
Copy link
Contributor

cderv commented Aug 1, 2019

Oh sorry. I did not see that. 😔 Looking for this I found this thread and not comment about it being implemented. I should have looked better. 😓
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants