| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b7a5c05 commit d1bd99c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,8 +64,6 @@ Refs | |||
| 64 | 64 | a symbolic reference anymore. Currently, we cannot handle this that well | |
| 65 | 65 | as we do not check for this case. This should be added though as it is | |
| 66 | 66 | valid to have a detached head in some cases. | |
| 67 | - * Allow to set SymbolicReferences to a specific Head Reference. This is like | ||
| 68 | - a git-reset --soft | ||
| 69 | 67 | ||
| 70 | 68 | Remote | |
| 71 | 69 | ------ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ | |||
| 7 | 7 | Module containing all ref based objects | |
| 8 | 8 | """ | |
| 9 | 9 | import os | |
| 10 | - from objects.base import Object | ||
| 10 | + from objects import Object, Commit | ||
| 11 | 11 | from objects.utils import get_object_type_by_name | |
| 12 | 12 | from utils import LazyMixin, Iterable | |
| 13 | 13 | ||
@@ -214,13 +214,36 @@ def __ne__(self, other): | |||
| 214 | 214 | def __hash__(self): | |
| 215 | 215 | return hash(self.name) | |
| 216 | 216 | ||
| 217 | + def _get_path(self): | ||
| 218 | + return os.path.join(self.repo.path, self.name) | ||
| 219 | + | ||
| 217 | 220 | @property | |
| 218 | - def reference(self): | ||
| 221 | + def commit(self): | ||
| 222 | + """ | ||
| 223 | + Returns: | ||
| 224 | + Commit object we point to, works for detached and non-detached | ||
| 225 | + SymbolicReferences | ||
| 226 | + """ | ||
| 227 | + # we partially reimplement it to prevent unnecessary file access | ||
| 228 | + fp = open(self._get_path(), 'r') | ||
| 229 | + value = fp.read().rstrip() | ||
| 230 | + fp.close() | ||
| 231 | + tokens = value.split(" ") | ||
| 232 | + | ||
| 233 | + # it is a detached reference | ||
| 234 | + if len(tokens) == 1 and len(tokens[0]) == 40: | ||
| 235 | + return Commit(self.repo, tokens[0]) | ||
| 236 | + | ||
| 237 | + # must be a head ! Git does not allow symbol refs to other things than heads | ||
| 238 | + # Otherwise it would have detached it | ||
| 239 | + return Head(self.repo, tokens[1]).commit | ||
| 240 | + | ||
| 241 | + def _get_reference(self): | ||
| 219 | 242 | """ | |
| 220 | 243 | Returns | |
| 221 | 244 | Reference Object we point to | |
| 222 | 245 | """ | |
| 223 | - fp = open(os.path.join(self.repo.path, self.name), 'r') | ||
| 246 | + fp = open(self._get_path(), 'r') | ||
| 224 | 247 | try: | |
| 225 | 248 | tokens = fp.readline().rstrip().split(' ') | |
| 226 | 249 | if tokens[0] != 'ref:': | |
@@ -229,6 +252,41 @@ def reference(self): | |||
| 229 | 252 | finally: | |
| 230 | 253 | fp.close() | |
| 231 | 254 | ||
| 255 | + def _set_reference(self, ref): | ||
| 256 | + """ | ||
| 257 | + Set ourselves to the given ref. It will stay a symbol if the ref is a Head. | ||
| 258 | + Otherwise we try to get a commit from it using our interface. | ||
| 259 | + | ||
| 260 | + Strings are allowed but will be checked to be sure we have a commit | ||
| 261 | + """ | ||
| 262 | + write_value = None | ||
| 263 | + if isinstance(ref, Head): | ||
| 264 | + write_value = "ref: %s" % ref.path | ||
| 265 | + elif isinstance(ref, Commit): | ||
| 266 | + write_value = ref.id | ||
| 267 | + else: | ||
| 268 | + try: | ||
| 269 | + write_value = ref.commit.id | ||
| 270 | + except AttributeError: | ||
| 271 | + sha = str(ref) | ||
| 272 | + try: | ||
| 273 | + obj = Object.new(self.repo, sha) | ||
| 274 | + if obj.type != "commit": | ||
| 275 | + raise TypeError("Invalid object type behind sha: %s" % sha) | ||
| 276 | + write_value = obj.id | ||
| 277 | + except Exception: | ||
| 278 | + raise ValueError("Could not extract object from %s" % ref) | ||
| 279 | + # END end try string | ||
| 280 | + # END try commit attribute | ||
| 281 | + fp = open(self._get_path(), "w") | ||
| 282 | + try: | ||
| 283 | + fp.write(write_value) | ||
| 284 | + finally: | ||
| 285 | + fp.close() | ||
| 286 | + # END writing | ||
| 287 | + | ||
| 288 | + reference = property(_get_reference, _set_reference, doc="Returns the Reference we point to") | ||
| 289 | + | ||
| 232 | 290 | # alias | |
| 233 | 291 | ref = reference | |
| 234 | 292 | ||
@@ -251,11 +309,12 @@ class HEAD(SymbolicReference): | |||
| 251 | 309 | Special case of a Symbolic Reference as it represents the repository's | |
| 252 | 310 | HEAD reference. | |
| 253 | 311 | """ | |
| 312 | + _HEAD_NAME = 'HEAD' | ||
| 254 | 313 | __slots__ = tuple() | |
| 255 | 314 | ||
| 256 | - def __init__(self, repo, name): | ||
| 257 | - if name != 'HEAD': | ||
| 258 | - raise ValueError("HEAD instance must point to 'HEAD', got %s" % name) | ||
| 315 | + def __init__(self, repo, name=_HEAD_NAME): | ||
| 316 | + if name != self._HEAD_NAME: | ||
| 317 | + raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, name)) | ||
| 259 | 318 | super(HEAD, self).__init__(repo, name) | |
| 260 | 319 | ||
| 261 | 320 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,3 +82,29 @@ def test_head_reset(self, rw_repo): | |||
| 82 | 82 | ||
| 83 | 83 | # paths | |
| 84 | 84 | cur_head.reset(new_head_commit, paths = "lib") | |
| 85 | + | ||
| 86 | + | ||
| 87 | + # now that we have a write write repo, change the HEAD reference - its | ||
| 88 | + # like git-reset --soft | ||
| 89 | + heads = rw_repo.heads | ||
| 90 | + assert heads | ||
| 91 | + for head in heads: | ||
| 92 | + cur_head.reference = head | ||
| 93 | + assert cur_head.reference == head | ||
| 94 | + assert cur_head.commit == head.commit | ||
| 95 | + assert not cur_head.is_detached | ||
| 96 | + # END for each head | ||
| 97 | + | ||
| 98 | + # detach | ||
| 99 | + cur_head.reference = heads[0].commit | ||
| 100 | + assert cur_head.commit == heads[0].commit | ||
| 101 | + assert cur_head.is_detached | ||
| 102 | + self.failUnlessRaises(TypeError, getattr, cur_head, "reference") | ||
| 103 | + | ||
| 104 | + some_tag = rw_repo.tags[0] | ||
| 105 | + cur_head.reference = some_tag | ||
| 106 | + assert cur_head.is_detached | ||
| 107 | + assert cur_head.commit == some_tag.commit | ||
| 108 | + | ||
| 109 | + # type check | ||
| 110 | + self.failUnlessRaises(ValueError, setattr, cur_head, "reference", "that") | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments