Can't create tags in git repo via dulwich.

Bug #1115283 reported by Ryan Faulkner
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Dulwich
Expired
Undecided
Unassigned

Bug Description

Ideally, I could call Tag.from_string("<tag_name>"). This fails however.

>>> from dulwich.objects import Tag
>>> Tag.from_string('rag_tag')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dulwich/objects.py", line 426, in from_string
    obj.set_raw_string(string)
  File "dulwich/objects.py", line 293, in set_raw_string
    self.set_raw_chunks([text])
  File "dulwich/objects.py", line 298, in set_raw_chunks
    self._deserialize(chunks)
  File "dulwich/objects.py", line 672, in _deserialize
    for field, value in parse_tag("".join(chunks)):
ValueError: need more than 1 value to unpack

This can be fixed by changing https://github.com/jelmer/dulwich/blob/master/dulwich/objects.py#L598 to:

return _parse_tag_or_commit("{0} {1}".format(_TAG_HEADER, text))

Revision history for this message
milki (milki-p) wrote :

You are improperly using Tag.from_string. from_string takes in a the string for a raw SHAFILE. This means you need the entire contents of an actual Tag object.

Perhaps you mean to construct a new Tag object and give it a name? Like all the other objects, you need to create the object and then assign the values like so:

        tag = Tag()
        tag.tagger = tagger
        tag.message = message
        tag.name = tag

Jelmer Vernooij (jelmer)
Changed in dulwich:
status: New → Incomplete
Revision history for this message
Ryan Faulkner (bobs-ur-uncle) wrote :

Ah I see. You're correct in assuming that I'm looking to create a tag. However, the above steps seem to be omitting some calls to actually create the tag in .git/refs/tags or .git/packed-refs nor is it visible when I run 'git tag'. (http://git-scm.com/book/en/Git-Basics-Tagging)

If I try to to add the object to a repo object store I see the following:

>>> from dulwich.repo import Repo
>>> from dulwich.objects import Tag
>>> r = Repo(".")
>>> object_store = r.object_store
>>> tag = Tag()
>>> tag.tagger = 'rfaulk'
>>> tag.message = 'test tag 1,2.'
>>> tag.name = 'test-tag'
>>> object_store.add_object(tag)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/dulwich/object_store.py", line 610, in add_object
    dir = os.path.join(self.path, obj.id[:2])
  File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 492, in id
    return self.sha().hexdigest()
  File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 483, in sha
    new_sha.update(self._header())
  File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 462, in _header
    return object_header(self.type, self.raw_length())
  File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 467, in raw_length
    for chunk in self.as_raw_chunks():
  File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 253, in as_raw_chunks
    self._chunked_text = self._serialize()
  File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 654, in _serialize
    chunks.append("%s %s\n" % (_OBJECT_HEADER, self._object_sha))
AttributeError: _object_sha

Am I missing something here?

Revision history for this message
Jelmer Vernooij (jelmer) wrote : Re: [Bug 1115283] Re: Can't create tags in git repo via dulwich.

On Wed, 2013-02-06 at 11:40 +0000, Ryan Faulkner wrote:
> Ah I see. You're correct in assuming that I'm looking to create a tag.
> However, the above steps seem to be omitting some calls to actually
> create the tag in .git/refs/tags or .git/packed-refs nor is it visible
> when I run 'git tag'. (http://git-scm.com/book/en/Git-Basics-Tagging)
These steps create an annotated 'tag' object. To create a tag, add the
relevant ref to your git repository. E.g.

r['refs/tags/foo'] = o.id

where o is either an annotated tag object or another object the tag
refers to.

> If I try to to add the object to a repo object store I see the
> following:
>
> >>> from dulwich.repo import Repo
> >>> from dulwich.objects import Tag
> >>> r = Repo(".")
> >>> object_store = r.object_store
> >>> tag = Tag()
> >>> tag.tagger = 'rfaulk'
> >>> tag.message = 'test tag 1,2.'
> >>> tag.name = 'test-tag'
> >>> object_store.add_object(tag)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/Library/Python/2.7/site-packages/dulwich/object_store.py", line 610, in add_object
> dir = os.path.join(self.path, obj.id[:2])
> File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 492, in id
> return self.sha().hexdigest()
> File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 483, in sha
> new_sha.update(self._header())
> File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 462, in _header
> return object_header(self.type, self.raw_length())
> File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 467, in raw_length
> for chunk in self.as_raw_chunks():
> File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 253, in as_raw_chunks
> self._chunked_text = self._serialize()
> File "/Library/Python/2.7/site-packages/dulwich/objects.py", line 654, in _serialize
> chunks.append("%s %s\n" % (_OBJECT_HEADER, self._object_sha))
> AttributeError: _object_sha
>
> Am I missing something here?
You need to set an object in the tag as well.

tag.object = (type, somesha)

Cheers,

Jelmer

Revision history for this message
Ryan Faulkner (bobs-ur-uncle) wrote :

Thanks Jelmer, the feedback was very helpful.

I ended up creating tags by doing the following:

        # Open the repo
        _repo = Repo(self.config['top_dir'])
        master_branch = 'master'

        # Build the commit object
        blob = Blob.from_string("empty")
        tree = Tree()
        tree.add(tag, 0100644, blob.id)

        commit = Commit()
        commit.tree = tree.id
        commit.author = commit.committer = author
        commit.commit_time = commit.author_time = int(time())
        tz = parse_timezone('-0200')[0]
        commit.commit_timezone = commit.author_timezone = tz
        commit.encoding = "UTF-8"
        commit.message = 'Tagging repo for deploy: ' + message

        # Add objects to the repo store instance
        object_store = _repo.object_store
        object_store.add_object(blob)
        object_store.add_object(tree)
        object_store.add_object(commit)
        _repo.refs['refs/heads/' + master_branch] = commit.id

        # Build the tag object and tag
        tag = Tag()
        tag.tagger = author
        tag.message = message
        tag.name = tag
        tag.object = (Commit, commit.id)
        tag.tag_time = commit.author_time
        tag.tag_timezone = tz
        object_store.add_object(tag)
        _repo['refs/tags/' + tag] = tag.id

This doesn't seem to be included in the documentation. I'm wondering, would it be helpful to you if I added this to the tutorial docs then sent a pull request to jelmer/dulwich?

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

On Mon, 2013-02-11 at 07:34 +0000, Ryan Faulkner wrote:
> Thanks Jelmer, the feedback was very helpful.
>
> I ended up creating tags by doing the following:

[... creating commit ... ]

>
> # Build the tag object and tag
> tag = Tag()
> tag.tagger = author
> tag.message = message
> tag.name = tag
> tag.object = (Commit, commit.id)
> tag.tag_time = commit.author_time
> tag.tag_timezone = tz
> object_store.add_object(tag)
This last step shouldn't be necessary. Just this line:
> _repo['refs/tags/' + tag] = tag.id

- with commit rather than tag - should be sufficient. You only need the
tag object if you want to create an annotated tag object.

> This doesn't seem to be included in the documentation. I'm wondering,
> would it be helpful to you if I added this to the tutorial docs then
> sent a pull request to jelmer/dulwich?
Please do. Improvements to the documentation are always welcome :)

Jelmer

Revision history for this message
Launchpad Janitor (janitor) wrote :

[Expired for Dulwich because there has been no activity for 60 days.]

Changed in dulwich:
status: Incomplete → Expired
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.