2

This is a follow-up question to the question on pushing to remote repo without local working directory(Python push files to Github remote repo without local working directory). I wonder what if the file already exists on the remote repo and I just want to update it with a modified file of the same name? (e.g., equivalent to on the Github website, upload a modified version of a file already existing on the remote)

Edit: We've come up with a solution:

contents_object = repository.contents(file_path)
push_status = contents_object.update("test_message",contents)

However, while this runs successfully on one machine, it threw an error on another machine (specifically, the first line will get an AttributeError). Is this because of potentially different versions of github3?

2
  • Are you using some package to communicate with their API? The answer from your previous question uses github3. Have you looked at their documentation? Commented Oct 28, 2016 at 15:09
  • sorry, I am using github3, as the original post's answer suggested. I did look at the documentation but it wasn't immediately obvious which method would require having a local working directory or not Commented Oct 28, 2016 at 15:11

1 Answer 1

1

It seems clear that under github3 version 0.9.6, which as of this time is what you will get with pip install github3.py(https://github3py.readthedocs.io/en/master/#installation), this will work(doing the update to the remote repo without any local working directory):

def update_to_git(username,password,path,account,repo,message):
    files_to_upload = [path]
    gh = github3.login(username=username, password=password)
    repository = gh.repository(account, repo)
    for file_info in files_to_upload:
        with open(file_info, 'rb') as fd:
            contents = fd.read()
        contents_object = repository.contents(file_info)
        contents_object.update(message,contents)

However, if you have github3 version 1.0.0a4, this will not work. Specifically, you will get an AttributeError for the contents_object = repository.contents(file_info) line, possibly due to the changes in the implementation in github3.

Sign up to request clarification or add additional context in comments.

1 Comment

In v1.0.0a4, Repository.contents() has been split into Repository.directory_contents() and Repository.file_contents().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.