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

Give helpful error message when first commit has not yet been made #450

Open
wmacnair opened this issue May 15, 2023 · 1 comment
Open

Comments

@wmacnair
Copy link

Hi

I was working with a student this morning to track down an error in calling git2r::commits(repo), and the issue turned out to be that she had not yet made a first commit (I'm pretty sure that this was the issue, but not 100%).

A small helpful tweak for beginners would be to edit the error message for this specific case. So something informing the user that no commits had been made yet, rather than Error in UseMethod("sha", object): no applicable method for 'sha' applied to an object of class NULL.

Thanks!
Will

@stewid
Copy link
Member

stewid commented May 18, 2023

Thanks for the suggestion. I've not been able to reproduce the error using the following code, so I'm not sure what went wrong in that specific case.

library(git2r)

path <- tempfile(pattern="git2r-")
dir.create(path)

## Try to list the commits in a non-repository.
## Raises "Error in repository(repo) : The 'path' is not in a git repository"
commits(path)

## Try to list the commits in an empty repository.
## Returns an empty list()
repo <- init(path)
commits(repo)

## Create a file and add it to the repository. Then try to list the commits.
## Returns an empty list()
lines <- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do"
writeLines(lines, file.path(path, "example.txt"))
add(repo, "example.txt")
commits(repo)

## Add one commit and then list the commits.
## Returns the first commit.
commit(repo, "First commit message")
commits(repo)

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

2 participants