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

[BUG] - creating the first commit on a branch other than master fails #145

Closed
oknozor opened this issue Nov 19, 2021 · 2 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@oknozor
Copy link
Collaborator

oknozor commented Nov 19, 2021

Describe the bug

cocogitto_bot_playground on  main
❯ coco chore "First commit"
Error: reference 'refs/heads/main' not found; class=Reference (4); code=UnbornBranch (-9)
@oknozor oknozor added the bug Something isn't working label Nov 19, 2021
@oknozor oknozor self-assigned this Nov 19, 2021
@oknozor oknozor added this to To do in next release via automation Nov 19, 2021
@Zshoham
Copy link
Contributor

Zshoham commented Jan 23, 2022

The issue seems to be in libgit2 libgit2/libgit2#6049 where the is_empty function only returns true if the branch is called "master". This causes the issue in this line where is_empty is set to false for empty repositories with a "main" branch instead of "master".
The fix is pretty simple, just need to try and get the head, if we can't get the head the repo is empty, otherwise, it is not.

 src/git/commit.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/git/commit.rs b/src/git/commit.rs
--- a/src/git/commit.rs
+++ b/src/git/commit.rs
@@ -8,7 +8,7 @@ impl Repository {
         let sig = self.0.signature()?;
         let tree_id = self.0.index()?.write_tree()?;
         let tree = self.0.find_tree(tree_id)?;
-        let is_empty = self.0.is_empty()?;
+        let is_empty = self.0.head().is_err();
         let has_delta = self.get_diff(false).is_some();
 
         if !is_empty && has_delta {

I'd be happy to submit a pull request for this as well if you prefer.

@oknozor
Copy link
Collaborator Author

oknozor commented Jan 24, 2022

@Zshoham thanks for investigating this.
You are more than welcome to submit a PR.

Zshoham added a commit to Zshoham/cocogitto that referenced this issue Jan 24, 2022
The `is_empty` function exposed by libgit2 requires the default branch to be 
"master". If a repository is created with a default branch not named master `is_empty`
will return false even when the repository is empty.

The fix is simply to try and access the HEAD of the repository. If the head
cant be accessed it means that the repository is still empty.

Closes cocogitto#145
Zshoham added a commit to Zshoham/cocogitto that referenced this issue Jan 25, 2022
The `is_empty` function exposed by libgit2 requires the default branch to be
"master". If a repository is created with a default branch not named master `is_empty`
will return false even when the repository is empty.

The fix is simply to try and access the HEAD of the repository. If the head
cant be accessed it means that the repository is still empty.

Closes cocogitto#145
next release automation moved this from To do to Done Jan 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

No branches or pull requests

2 participants