libgit2 / libgit2/pygit2

_pygit2.GitError: reference 'refs/heads/master' not found

Open
#981 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

support
Dominant language
Python
Stars
1.7k
Forks
408
Avg merge
2d 57m
Merged PRs (30d)
7

Description

import pygit2
from github import Github

github_username = "user"
github_password = "password"
github_email = "email"

# login to github and
authenticated_github_user = Github(github_username, github_password)
user = authenticated_github_user.get_user()

is_org: bool = False
# create the new repository
if is_org:
    org = authenticated_github_user.get_organization('someorg')
    repo = org.create_repo('projectname', description='somedescription')
else:
    repo = user.create_repo('testrepo_github_api', description='this is a test')

# clone the repository
repoClone = pygit2.clone_repository(repo.git_url, '/tmp/test_clone')

#create some new files in the repo
repo.create_file("README.md", "init commit", 'readmeText')

# Commit it
repoClone.remotes.set_url("origin", repo.clone_url)
index = repoClone.index
index.add_all()
index.write()
author = pygit2.Signature("Jesus Christ", github_email)
commiter = pygit2.Signature("Jesus Christ", github_email)
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/master', author, commiter, "init commit", tree, [repoClone.head.get_object().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass(github_username, github_password)
remote.credentials = credentials

callbacks = pygit2.RemoteCallbacks(credentials=credentials)

remote.push(['refs/heads/master'], callbacks=callbacks)

This results in

  oid = repoClone.create_commit('HEAD', author, commiter, "init commit", tree, [repoClone.head.get_object().hex])
_pygit2.GitError: reference 'refs/heads/master' not found

git show-ref is empty, but this is pretty much expected, no?

This commit and push still go through, but the error is nevertheless thrown.

Am I doing something wrong?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction around pygit2.clone_repository, repoClone.create_commit, and remote.push; inspect the empty git show-ref state and the reference passed to create_commit. Confirm why the commit and push appear to succeed despite the reported exception, then verify the same script completes without the error.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.