1

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?

Community
  • 1
  • 1
avck
  • 3,535
  • 3
  • 26
  • 38

1 Answers1

2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This makes thing a bit clear. So after `create_reference()` my `'refs/heads/master'` will point to `'refs/remotes/origin/master'`. Right? After remote.fetch() where are the received objects kept? Suppose I want to check the diff bw my remote and index file how would I go about it? – avck Jul 09 '14 at 05:56
  • 1
    @avck "After `remote.fetch()` where are the received objects kept?": in "`refs/remotes/xxx`", with `xxx` being the names of the remotes you have declared in your repo (by default '`origin`') – VonC Jul 09 '14 at 06:03
  • When do we use `Reference.target=` and which Reference object will be used here? – avck Jul 09 '14 at 06:08
  • 1
    @avck to set the target of the reference (https://github.com/libgit2/pygit2/blob/master/docs/references.rst): usually a SHA1 or a branch/tag name (https://github.com/libgit2/pygit2/blob/7e41bd4af3b980f06f5f6ecae6193270e264da96/src/reference.c#L205-L208): so you can modify an existing reference or create one. In the case of the master branch, since you likely already have master, you would use `Reference.target` instead of `create_reference()`. – VonC Jul 09 '14 at 06:13
  • 1
    @avck see a lots of examples of '`reference.target`' usage in https://github.com/libgit2/pygit2/blob/master/test/test_refs.py – VonC Jul 09 '14 at 06:14
  • cool. So after fetch() I can use `'refs/remotes/xxx'` To check the diff bw my remote and index file Is this the recommended method of checking the diff of remote repo and current repo before pushing? – avck Jul 09 '14 at 06:23
  • 1
    @avck yes, but you need to chose a reference within `refs/remotes/xxx`, like `refs/remotes/origin/master` for instance. And once you have examine the diff, you still need to merge the remote tracking branch to your local branch before pushing anything, or your push won't be a fast-forward one. – VonC Jul 09 '14 at 06:36
  • I will try this out. Thanks for giving your time! – avck Jul 09 '14 at 07:03
  • what is the difference between Repository.checkout_head() and Repository.checkout('HEAD'). Documentation mentions that calling Repository.checkout without arguments will take checkout_index as default. After updating reference if i do Repository.checkout('HEAD'), the index stays modified ie the Repository.status() shows a file with changes. While using Repository.checkout_head() clears the index too. Could you explain what is happening here? – avck Jul 11 '14 at 10:06
  • @avck I suspect (not tested) that `checkout_head()` is the same as `git checkout HEAD -- .`, meaning a checkout on the working tree (which will reset the working tree content: http://stackoverflow.com/a/3107870/6309). While `git checkout HEAD` is for checking out the HEAD of the branch (branch, not files): if that branch is *already checked out*, this is a no-op: nothing happens. – VonC Jul 11 '14 at 10:34
  • 1
    @avck note that a checkout(HEAD) with the FORCE option (`repo.checkout('HEAD', pygit2.GIT_CHECKOUT_FORCE)`) would *also* reset the working tree: https://github.com/libgit2/pygit2/blob/d2a62c526862ccdb05bea25d4c77dcc36e080f3f/test/test_repository.py#L234-L247 – VonC Jul 11 '14 at 10:39
  • After I do `Reference.target=remoteReference.target.hex` the commit history is updated. Resetting the working tree will not be a problem then I guess? – avck Jul 11 '14 at 10:48