Skip to content

Different errors and inconsistency while trying to push files to remote using GitHub token. #1001

Closed
@praveenkanna

Description

@praveenkanna

I'm a newbie to pygit2 and seeing different errors while pushing to remote, some times it succeeds and sometimes it is raising errors. I'm using the python Python 3.7.7 in OSX, pygit2==1.2.0, pip version: pip 20.0.2. Here are the below errors:

Pushing updated files to remote:
   - Added: git_update.py to the repository
   - Commited: git_update.py to the repository
   - Pushing files to the remote repository
Traceback (most recent call last):
  File "git_update.py", line 98, in <module>
    pull_push_new_commits()
  File "git_update.py", line 94, in pull_push_new_commits
    push_new_commits(repo, remote, callbacks)
  File "git_update.py", line 77, in push_new_commits
    remote.push([repo.head.name], callbacks=callbacks)
  File "/usr/local/lib/python3.7/site-packages/pygit2/remote.py", line 536, in push
    check_error(err)
  File "/usr/local/lib/python3.7/site-packages/pygit2/errors.py", line 61, in check_error
    raise GitError(message)
_pygit2.GitError: unexpected EOF
Pushing updated files to remote:
   - Added: git_update.py to the repository
   - Committed: git_update.py to the repository
   - Pushing files to the remote repository
Fatal Python error: ffi.from_handle() detected that the address passed points to garbage. If it is really the result of ffi.new_handle(), then the Python object has already been garbage collected

Current thread 0x000000011350fdc0 (most recent call first):
  File "/usr/local/lib/python3.7/site-packages/pygit2/remote.py", line 317 in _certificate_cb
  File "/usr/local/lib/python3.7/site-packages/pygit2/remote.py", line 535 in push
  File "git_update.py", line 77 in push_new_commits
  File "git_update.py", line 94 in pull_push_new_commits
  File "git_update.py", line 98 in <module>
Abort trap: 6

Below is the code:

import pygit2
import datetime
import os


def git_url_update(repo):
    repo_url = repo.remotes[0].url
    repo_url_list = repo_url.split('@')

    if repo_url.startswith('git'):
        repo_url_path = repo_url_list[0].replace('git', 'https://')
        repo_path_change = repo_url_list[1].replace(':', '/')
        remote_url = repo_url_path + repo_path_change

    elif len(repo_url_list) == 1:
        remote_url = repo_url_list[0]
    else:
        if ':' in repo_url_list[1]:
            repo_path_change = repo_url_list[1].replace(':', '/')
        else:
            repo_path_change = repo_url_list[1]
        remote_url = repo_url_list[0] + '@' + \
            repo_path_change

    repo.remotes.set_url('origin', remote_url)
    repo.remotes.set_push_url('origin', remote_url)


def update_local_repo(repo, remote, callbacks):
    remote.fetch(callbacks=callbacks)
    remote_master_id = repo.lookup_reference(
        'refs/remotes/origin/%s' % (repo.head.shorthand)).target
    merge_result, _ = repo.merge_analysis(remote_master_id)

    if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
        print("current directory is up to date")
    elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
        repo.checkout_tree(repo.get(remote_master_id))
        try:
            master_ref = repo.lookup_reference(
                'refs/heads/%s' % (repo.head.shorthand))
            master_ref.set_target(remote_master_id)
        except KeyError:
            repo.create_branch(repo.head.shorthand, repo.get(remote_master_id))
        repo.head.set_target(remote_master_id)
    else:
        raise AssertionError('Unknown merge analysis result')


def push_new_commits(repo, remote, callbacks):
    print('\nPushing updated files to remote:')
    date_object = datetime.date.today()
    commit_message = 'Snapshot {date_object}'.format(
        date_object=date_object)

    index = repo.index
    status = repo.status()

    for changed_file in status:
        if not repo.path_is_ignored(changed_file):
            index.add(changed_file)
            index.write()
            print('   - Added: {} to the repository'.format(changed_file))

    tree = index.write_tree()
    author = pygit2.Signature('<user>', '<email>')
    commiter = pygit2.Signature('<user>', '<email>')

    print('   - Commited: {} to the repository'.format(changed_file))
    oid = repo.create_commit(repo.head.name,
                             author, commiter, commit_message, tree, [repo.head.target])

    repo.head.set_target(oid)
    print('   - Pushing files to the remote repository')
    remote.push([repo.head.name], callbacks=callbacks)


def pull_push_new_commits():
    currentDirectory = os.getcwd()
    print("Pulling the repo for new changes")
    repository_path = pygit2.discover_repository(currentDirectory)
    repo = pygit2.Repository(repository_path)
    git_url_update(repo)
    remoteName = repo.remotes[0].name
    remote = repo.remotes[remoteName]

    callbacks = pygit2.RemoteCallbacks(pygit2.UserPass(
        '<TOKEN>', 'x-oauth-basic'))

    update_local_repo(repo, remote, callbacks)
    push_new_commits(repo, remote, callbacks)


if __name__ == "__main__":
    pull_push_new_commits()

I'm tweaking the git URL because of using only Github token since we want to run this as a cron job inside the docker container and our pipeline uses a different user and I can't copy that ssh keys of that user those to the container. (lot of reasons) so I chose GitHub token approach since it is the easiest approach for me.

Also, I'm seeing SSL unknown error inside the python docker container ( tried with different flavours )

Pushing updated files to remote:
   - Added: Dockerfile to the repository
   - Commited: Dockerfile to the repository
   - Pushing files to the remote repository
Traceback (most recent call last):
  File "git_update.py", line 98, in <module>
    pull_push_new_commits()
  File "git_update.py", line 94, in pull_push_new_commits
    push_new_commits(repo, remote, callbacks)
  File "git_update.py", line 77, in push_new_commits
    remote.push([repo.head.name], callbacks=callbacks)
  File "/usr/local/lib/python3.7/site-packages/pygit2/remote.py", line 536, in push
    check_error(err)
  File "/usr/local/lib/python3.7/site-packages/pygit2/errors.py", line 61, in check_error
    raise GitError(message)
_pygit2.GitError: SSL error: unknown error

To explain a bit more about this code, I'm pulling the new changes from remote and run a script which updates some files or creates new files, commits and pushes back to the repository.

Tried different things but seeing different issues and been blocked from a week so posting for help. Can anyone help me out, if I'm missing anything or doing any mistake?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions