Search code examples
gitpygit2

Steps for pulling from remote using pygit2


While using the pygit2 library a simple repo.fetch() fetches all the diffs. The answer here describes the steps viz
1. Remote.fetch()
2. Repository.create_reference() or Reference.target=
3. Repository.checkout_head()

I am not sure about what is happening under the hood in second step and what parameters need to be passed. r.repo.create_reference: (self, name, target, force=False)

Create a new reference "name" which points to an object or to another
reference.

Based on the type and value of the target parameter, this method tries
to guess whether it is a direct or a symbolic reference.

Keyword arguments:

force
If True references will be overridden, otherwise (the default) an
exception is raised.

Examples::

repo.create_reference('refs/heads/foo', repo.head.target)
repo.create_reference('refs/tags/foo', 'refs/heads/master')
repo.create_reference('refs/tags/foo', 'bbb78a9cec580')

What is happening in the second step and what is meant by Reference.target= Where should it point to? And how does all this use the Remote.fetch() action?


Solution

  • The create_reference method changes the target in order for said target to contains (reference) the name.

    Here, after a fetch, you would want to set the reference of the local branch (you want to checkout) to the right remote tracking branch (you just fetched):

    repo.create_reference('refs/remotes/origin/master', 'refs/heads/master')
    

    You can look up the file "refs/heads/master" in your .git folder: it is a simple file with the reference it has to contain in order for that branch to point to (reference) another branch SHA1.