| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,15 +67,7 @@ Refs | |||
| 67 | 67 | ----- | |
| 68 | 68 | * When adjusting the reference of a symbolic reference, the ref log might need | |
| 69 | 69 | adjustments as well. This is not critical, but would make things totally 'right' | |
| 70 | - * Reference Objects should be able to set the commit they are pointing to, making | ||
| 71 | - the commit property read-write. Tags are a special case of this and would need | ||
| 72 | - to be handled as well ! | ||
| 73 | - * Ability to create new heads and tags in the Repository ( but using the respective | ||
| 74 | - Reference Type ), i.e. Head.create(repo, name, commit = 'HEAD') or | ||
| 75 | - TagReference.create(repo, name | ||
| 76 | - * Ability to rename references and tagsre | ||
| 77 | - * Ability to remove references and tags | ||
| 78 | - * Ability to checkout a reference - | ||
| 70 | + - same with adjusting references directly | ||
| 79 | 71 | * Check whether we are the active reference HEAD.commit == self.commit | |
| 80 | 72 | ||
| 81 | 73 | Remote | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,8 +65,7 @@ def name(self): | |||
| 65 | 65 | ||
| 66 | 66 | return '/'.join(tokens[2:]) | |
| 67 | 67 | ||
| 68 | - @property | ||
| 69 | - def object(self): | ||
| 68 | + def _get_object(self): | ||
| 70 | 69 | """ | |
| 71 | 70 | Returns | |
| 72 | 71 | The object our ref currently refers to. Refs can be cached, they will | |
@@ -76,17 +75,54 @@ def object(self): | |||
| 76 | 75 | # Our path will be resolved to the hexsha which will be used accordingly | |
| 77 | 76 | return Object.new(self.repo, self.path) | |
| 78 | 77 | ||
| 79 | - @property | ||
| 80 | - def commit(self): | ||
| 78 | + def _set_object(self, ref, type=None): | ||
| 81 | 79 | """ | |
| 80 | + Set our reference to point to the given ref. It will be converted | ||
| 81 | + to a specific hexsha. | ||
| 82 | + | ||
| 83 | + ``type`` | ||
| 84 | + If not None, string type of that the object must have, other we raise | ||
| 85 | + a type error. Only used internally | ||
| 86 | + | ||
| 82 | 87 | Returns | |
| 83 | - Commit object the head points to | ||
| 88 | + Object we have set. This is used internally only to reduce the amount | ||
| 89 | + of calls to the git command | ||
| 90 | + """ | ||
| 91 | + obj = Object.new(self.repo, ref) | ||
| 92 | + if type is not None and obj.type != type: | ||
| 93 | + raise TypeError("Reference %r cannot point to object of type %r" % (self,obj.type)) | ||
| 94 | + | ||
| 95 | + full_ref_path = os.path.join(self.repo.path, self.path) | ||
| 96 | + fp = open(full_ref_path, "w") | ||
| 97 | + try: | ||
| 98 | + fp.write(str(obj)) | ||
| 99 | + finally: | ||
| 100 | + fp.close() | ||
| 101 | + return obj | ||
| 102 | + | ||
| 103 | + object = property(_get_object, _set_object, doc="Return the object our ref currently refers to") | ||
| 104 | + | ||
| 105 | + def _set_commit(self, commit): | ||
| 106 | + """ | ||
| 107 | + Set ourselves to point to the given commit. | ||
| 108 | + | ||
| 109 | + Raise | ||
| 110 | + ValueError if commit does not actually point to a commit | ||
| 111 | + """ | ||
| 112 | + self._set_object(commit, type="commit") | ||
| 113 | + | ||
| 114 | + def _get_commit(self): | ||
| 115 | + """ | ||
| 116 | + Returns | ||
| 117 | + Commit object the reference points to | ||
| 84 | 118 | """ | |
| 85 | 119 | commit = self.object | |
| 86 | 120 | if commit.type != "commit": | |
| 87 | 121 | raise TypeError("Object of reference %s did not point to a commit" % self) | |
| 88 | 122 | return commit | |
| 89 | 123 | ||
| 124 | + commit = property(_get_commit, _set_commit, doc="Return Commit object the reference points to") | ||
| 125 | + | ||
| 90 | 126 | @classmethod | |
| 91 | 127 | def iter_items(cls, repo, common_path = None, **kwargs): | |
| 92 | 128 | """ | |
@@ -244,7 +280,7 @@ def _get_reference(self): | |||
| 244 | 280 | try: | |
| 245 | 281 | tokens = fp.readline().rstrip().split(' ') | |
| 246 | 282 | if tokens[0] != 'ref:': | |
| 247 | - raise TypeError("%s is a detached symbolic reference as it points to %r" % tokens[0]) | ||
| 283 | + raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, tokens[0])) | ||
| 248 | 284 | return Reference.from_path(self.repo, tokens[1]) | |
| 249 | 285 | finally: | |
| 250 | 286 | fp.close() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -181,4 +181,20 @@ def test_head_reset(self, rw_repo): | |||
| 181 | 181 | self.failUnlessRaises(AssertionError, getattr, remote, 'refs') | |
| 182 | 182 | # END for each remote | |
| 183 | 183 | ||
| 184 | - self.fail("set commit using ref.commit = that") | ||
| 184 | + # change where the active head points to | ||
| 185 | + if cur_head.is_detached: | ||
| 186 | + cur_head.reference = rw_repo.heads[0] | ||
| 187 | + | ||
| 188 | + head = cur_head.reference | ||
| 189 | + old_commit = head.commit | ||
| 190 | + head.commit = old_commit.parents[0] | ||
| 191 | + assert head.commit == old_commit.parents[0] | ||
| 192 | + assert head.commit == cur_head.commit | ||
| 193 | + head.commit = old_commit | ||
| 194 | + | ||
| 195 | + # setting a non-commit as commit fails, but succeeds as object | ||
| 196 | + head_tree = head.commit.tree | ||
| 197 | + self.failUnlessRaises(TypeError, setattr, head, 'commit', head_tree) | ||
| 198 | + assert head.commit == old_commit # and the ref did not change | ||
| 199 | + head.object = head_tree | ||
| 200 | + assert head.object == head_tree | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments