From 9ee31065abea645cbc2cf3e54b691d5983a228b2 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 11:01:12 +0200 Subject: Intermediate commit: commit,tree and blob objects now derive from object - test is in place which still fails on purpose. Need to integrate tags which can be objects or just a special form of a ref --- lib/git/tag.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/git/tag.py') diff --git a/lib/git/tag.py b/lib/git/tag.py index 8413ce73..df3158a6 100644 --- a/lib/git/tag.py +++ b/lib/git/tag.py @@ -7,6 +7,12 @@ from commit import Commit class Tag(object): + """ + Class representing a tag reference which either points to a commit + or to a tag object. In the latter case additional information, like the signature + or the tag-creator, is available. + """ + def __init__(self, name, commit): """ Initialize a newly instantiated Tag -- cgit v1.2.3 From 20f202d83bdf1f332a3cb8f010bcf8bf3c2807bd Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 16:36:51 +0200 Subject: Re-designed the tag testing - it does not use fixtures anymore but dyamically checks the existance of tags within the repository - it basically tests the interface and checks that expected return types are actually returned --- lib/git/tag.py | 166 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 68 deletions(-) (limited to 'lib/git/tag.py') diff --git a/lib/git/tag.py b/lib/git/tag.py index df3158a6..0c4122ab 100644 --- a/lib/git/tag.py +++ b/lib/git/tag.py @@ -4,95 +4,125 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php -from commit import Commit +import commit +import base -class Tag(object): +class TagRef(base.Ref): """ - Class representing a tag reference which either points to a commit + Class representing a lightweight tag reference which either points to a commit or to a tag object. In the latter case additional information, like the signature or the tag-creator, is available. + + This tag object will always point to a commit object, but may carray additional + information in a tag object:: + + tagref = TagRef.find_all(repo)[0] + print tagref.commit.message + if tagref.tag is not None: + print tagref.tag.message """ - def __init__(self, name, commit): + __slots__ = "tag" + + def __init__(self, path, commit_or_tag): """ Initialize a newly instantiated Tag - ``name`` - is the name of the head + ``path`` + is the full path to the tag - ``commit`` - is the Commit that the head points to + ``commit_or_tag`` + is the Commit or TagObject that this tag ref points to """ - self.name = name - self.commit = commit - - @classmethod - def find_all(cls, repo, **kwargs): + super(TagRef, self).__init__(path, commit_or_tag) + self.tag = None + + if commit_or_tag.type == "tag": + self.tag = commit_or_tag + # END tag object handling + + @property + def commit(self): """ - Find all Tags in the repository - - ``repo`` - is the Repo - - ``kwargs`` - Additional options given as keyword arguments, will be passed - to git-for-each-ref - Returns - ``git.Tag[]`` - - List is sorted by committerdate + Commit object the tag ref points to """ - options = {'sort': "committerdate", - 'format': "%(refname)%00%(objectname)"} - options.update(**kwargs) - - output = repo.git.for_each_ref("refs/tags", **options) - return cls.list_from_string(repo, output) + if self.object.type == "commit": + return self.object + # it is a tag object + return self.object.object @classmethod - def list_from_string(cls, repo, text): + def find_all(cls, repo, common_path = "refs/tags", **kwargs): """ - Parse out tag information into an array of Tag objects - - ``repo`` - is the Repo - - ``text`` - is the text output from the git-for-each command - Returns git.Tag[] + + For more documentation, please refer to git.base.Ref.find_all """ - tags = [] - for line in text.splitlines(): - tags.append(cls.from_string(repo, line)) - return tags - - @classmethod - def from_string(cls, repo, line): + return super(TagRef,cls).find_all(repo, common_path, **kwargs) + + +# provide an alias +Tag = TagRef + +class TagObject(base.Object): + """ + Non-Lightweight tag carrying additional information about an object we are pointing + to. + """ + type = "tag" + __slots__ = ( "object", "tag", "tagger", "tagged_date", "message" ) + + def __init__(self, repo, id, size=None, object=None, tag=None, + tagger=None, tagged_date=None, message=None): """ - Create a new Tag instance from the given string. - + Initialize a tag object with additional data + ``repo`` - is the Repo - - ``line`` - is the formatted tag information - - Format:: + repository this object is located in - name: [a-zA-Z_/]+ - - id: [0-9A-Fa-f]{40} - - Returns - git.Tag + ``id`` + SHA1 or ref suitable for git-rev-parse + + ``size`` + Size of the object's data in bytes + + ``object`` + Object instance of object we are pointing to + + ``tag`` + name of this tag + + ``tagger`` + Actor identifying the tagger + + ``tagged_date`` : (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) + is the DateTime of the tag creation """ - full_name, ids = line.split("\x00") - name = full_name.split("/")[-1] - commit = Commit(repo, id=ids) - return Tag(name, commit) - - def __repr__(self): - return '' % self.name + super(TagObject, self).__init__(repo, id , size) + self.object = object + self.tag = tag + self.tagger = tagger + self.tagged_date = tagged_date + self.message = message + + def __bake__(self): + super(TagObject, self).__bake__() + + output = self.repo.git.cat_file(self.type,self.id) + lines = output.split("\n") + + obj, hexsha = lines[0].split(" ") # object + type_token, type_name = lines[1].split(" ") # type + self.object = base.Object.get_type_by_name(type_name)(self.repo, hexsha) + + self.tag = lines[2][4:] # tag + + tagger_info = lines[3][7:]# tagger + self.tagger, self.tagged_date = commit.Commit._actor(tagger_info) + + # line 4 empty - check git source to figure out purpose + self.message = "\n".join(lines[5:]) + + -- cgit v1.2.3 From 9374a916588d9fe7169937ba262c86ad710cfa74 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 16:49:05 +0200 Subject: converted all spaces to tabs ( 4 spaces = 1 tab ) just to allow me and my editor to work with the files properly. Can convert it back for releaes --- lib/git/tag.py | 224 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 112 insertions(+), 112 deletions(-) (limited to 'lib/git/tag.py') diff --git a/lib/git/tag.py b/lib/git/tag.py index 0c4122ab..4266a7a9 100644 --- a/lib/git/tag.py +++ b/lib/git/tag.py @@ -8,121 +8,121 @@ import commit import base class TagRef(base.Ref): - """ - Class representing a lightweight tag reference which either points to a commit - or to a tag object. In the latter case additional information, like the signature - or the tag-creator, is available. - - This tag object will always point to a commit object, but may carray additional - information in a tag object:: - - tagref = TagRef.find_all(repo)[0] - print tagref.commit.message - if tagref.tag is not None: - print tagref.tag.message - """ - - __slots__ = "tag" - - def __init__(self, path, commit_or_tag): - """ - Initialize a newly instantiated Tag + """ + Class representing a lightweight tag reference which either points to a commit + or to a tag object. In the latter case additional information, like the signature + or the tag-creator, is available. + + This tag object will always point to a commit object, but may carray additional + information in a tag object:: + + tagref = TagRef.find_all(repo)[0] + print tagref.commit.message + if tagref.tag is not None: + print tagref.tag.message + """ + + __slots__ = "tag" + + def __init__(self, path, commit_or_tag): + """ + Initialize a newly instantiated Tag - ``path`` - is the full path to the tag + ``path`` + is the full path to the tag - ``commit_or_tag`` - is the Commit or TagObject that this tag ref points to - """ - super(TagRef, self).__init__(path, commit_or_tag) - self.tag = None - - if commit_or_tag.type == "tag": - self.tag = commit_or_tag - # END tag object handling - - @property - def commit(self): - """ - Returns - Commit object the tag ref points to - """ - if self.object.type == "commit": - return self.object - # it is a tag object - return self.object.object + ``commit_or_tag`` + is the Commit or TagObject that this tag ref points to + """ + super(TagRef, self).__init__(path, commit_or_tag) + self.tag = None + + if commit_or_tag.type == "tag": + self.tag = commit_or_tag + # END tag object handling + + @property + def commit(self): + """ + Returns + Commit object the tag ref points to + """ + if self.object.type == "commit": + return self.object + # it is a tag object + return self.object.object - @classmethod - def find_all(cls, repo, common_path = "refs/tags", **kwargs): - """ - Returns - git.Tag[] - - For more documentation, please refer to git.base.Ref.find_all - """ - return super(TagRef,cls).find_all(repo, common_path, **kwargs) - - + @classmethod + def find_all(cls, repo, common_path = "refs/tags", **kwargs): + """ + Returns + git.Tag[] + + For more documentation, please refer to git.base.Ref.find_all + """ + return super(TagRef,cls).find_all(repo, common_path, **kwargs) + + # provide an alias Tag = TagRef - + class TagObject(base.Object): - """ - Non-Lightweight tag carrying additional information about an object we are pointing - to. - """ - type = "tag" - __slots__ = ( "object", "tag", "tagger", "tagged_date", "message" ) - - def __init__(self, repo, id, size=None, object=None, tag=None, - tagger=None, tagged_date=None, message=None): - """ - Initialize a tag object with additional data - - ``repo`` - repository this object is located in - - ``id`` - SHA1 or ref suitable for git-rev-parse - - ``size`` - Size of the object's data in bytes - - ``object`` - Object instance of object we are pointing to - - ``tag`` - name of this tag - - ``tagger`` - Actor identifying the tagger - - ``tagged_date`` : (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) - is the DateTime of the tag creation - """ - super(TagObject, self).__init__(repo, id , size) - self.object = object - self.tag = tag - self.tagger = tagger - self.tagged_date = tagged_date - self.message = message - - def __bake__(self): - super(TagObject, self).__bake__() - - output = self.repo.git.cat_file(self.type,self.id) - lines = output.split("\n") - - obj, hexsha = lines[0].split(" ") # object - type_token, type_name = lines[1].split(" ") # type - self.object = base.Object.get_type_by_name(type_name)(self.repo, hexsha) - - self.tag = lines[2][4:] # tag - - tagger_info = lines[3][7:]# tagger - self.tagger, self.tagged_date = commit.Commit._actor(tagger_info) - - # line 4 empty - check git source to figure out purpose - self.message = "\n".join(lines[5:]) - - + """ + Non-Lightweight tag carrying additional information about an object we are pointing + to. + """ + type = "tag" + __slots__ = ( "object", "tag", "tagger", "tagged_date", "message" ) + + def __init__(self, repo, id, size=None, object=None, tag=None, + tagger=None, tagged_date=None, message=None): + """ + Initialize a tag object with additional data + + ``repo`` + repository this object is located in + + ``id`` + SHA1 or ref suitable for git-rev-parse + + ``size`` + Size of the object's data in bytes + + ``object`` + Object instance of object we are pointing to + + ``tag`` + name of this tag + + ``tagger`` + Actor identifying the tagger + + ``tagged_date`` : (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) + is the DateTime of the tag creation + """ + super(TagObject, self).__init__(repo, id , size) + self.object = object + self.tag = tag + self.tagger = tagger + self.tagged_date = tagged_date + self.message = message + + def __bake__(self): + super(TagObject, self).__bake__() + + output = self.repo.git.cat_file(self.type,self.id) + lines = output.split("\n") + + obj, hexsha = lines[0].split(" ") # object + type_token, type_name = lines[1].split(" ") # type + self.object = base.Object.get_type_by_name(type_name)(self.repo, hexsha) + + self.tag = lines[2][4:] # tag + + tagger_info = lines[3][7:]# tagger + self.tagger, self.tagged_date = commit.Commit._actor(tagger_info) + + # line 4 empty - check git source to figure out purpose + self.message = "\n".join(lines[5:]) + + -- cgit v1.2.3 From 15b9129ec639112e94ea96b6a395ad9b149515d1 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 11 Oct 2009 19:07:03 +0200 Subject: lazymixin system now supports per-attribute baking, it is up to the class whether it bakes more. This also leads to more efficient use of memory as values are only cached and set when required - the baking system does not require an own tracking variable anymore, and values are only to be cached once - then python will natively find the cache without involving any additional overhead. This works by using __getattr__ instead of __get_attribute__ which would always be called --- lib/git/tag.py | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'lib/git/tag.py') diff --git a/lib/git/tag.py b/lib/git/tag.py index 4266a7a9..89060ee0 100644 --- a/lib/git/tag.py +++ b/lib/git/tag.py @@ -74,7 +74,7 @@ class TagObject(base.Object): type = "tag" __slots__ = ( "object", "tag", "tagger", "tagged_date", "message" ) - def __init__(self, repo, id, size=None, object=None, tag=None, + def __init__(self, repo, id, object=None, tag=None, tagger=None, tagged_date=None, message=None): """ Initialize a tag object with additional data @@ -85,9 +85,6 @@ class TagObject(base.Object): ``id`` SHA1 or ref suitable for git-rev-parse - ``size`` - Size of the object's data in bytes - ``object`` Object instance of object we are pointing to @@ -100,29 +97,30 @@ class TagObject(base.Object): ``tagged_date`` : (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) is the DateTime of the tag creation """ - super(TagObject, self).__init__(repo, id , size) - self.object = object - self.tag = tag - self.tagger = tagger - self.tagged_date = tagged_date - self.message = message - - def __bake__(self): - super(TagObject, self).__bake__() - - output = self.repo.git.cat_file(self.type,self.id) - lines = output.split("\n") - - obj, hexsha = lines[0].split(" ") # object - type_token, type_name = lines[1].split(" ") # type - self.object = base.Object.get_type_by_name(type_name)(self.repo, hexsha) - - self.tag = lines[2][4:] # tag - - tagger_info = lines[3][7:]# tagger - self.tagger, self.tagged_date = commit.Commit._actor(tagger_info) + super(TagObject, self).__init__(repo, id ) + self._set_self_from_args_(locals()) - # line 4 empty - check git source to figure out purpose - self.message = "\n".join(lines[5:]) + def _set_cache_(self, attr): + """ + Cache all our attributes at once + """ + if attr in self.__slots__: + output = self.repo.git.cat_file(self.type,self.id) + lines = output.split("\n") + + obj, hexsha = lines[0].split(" ") # object + type_token, type_name = lines[1].split(" ") # type + self.object = base.Object.get_type_by_name(type_name)(self.repo, hexsha) + + self.tag = lines[2][4:] # tag + + tagger_info = lines[3][7:]# tagger + self.tagger, self.tagged_date = commit.Commit._actor(tagger_info) + + # line 4 empty - check git source to figure out purpose + self.message = "\n".join(lines[5:]) + # END check our attributes + else: + super(TagObject, self)._set_cache_(attr) -- cgit v1.2.3