diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-12-04 10:01:43 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-12-04 10:01:43 +0100 |
commit | e4ed59a86909b142ede105dd9262915c0e2993ac (patch) | |
tree | 1b82a5419233e35e04cfb458a303bbbdcdf4ff14 | |
parent | 4e729429631c84c3bd5602edcab3e7c2ab1dcce0 (diff) | |
download | gitpython-e4ed59a86909b142ede105dd9262915c0e2993ac.tar.gz |
SymbolicReference creation now won't fail if the target already exists and has the same content as the new reference
-rw-r--r-- | lib/git/refs.py | 25 | ||||
-rw-r--r-- | test/git/test_refs.py | 8 |
2 files changed, 24 insertions, 9 deletions
diff --git a/lib/git/refs.py b/lib/git/refs.py index ec70c72b..45e6dcfb 100644 --- a/lib/git/refs.py +++ b/lib/git/refs.py @@ -187,7 +187,7 @@ class SymbolicReference(object): if not os.path.isdir(directory): os.makedirs(directory) - fp = open(path, "w") + fp = open(path, "wb") try: fp.write(write_value) finally: @@ -270,16 +270,24 @@ class SymbolicReference(object): corresponding object and a detached symbolic reference will be created instead""" full_ref_path = cls._to_full_path(repo, path) - abs_ref_path = os.path.join(repo.git_dir, full_ref_path) - if not force and os.path.isfile(abs_ref_path): - raise OSError("Reference at %s does already exist" % full_ref_path) - ref = cls(repo, full_ref_path) + # figure out target data target = reference if resolve: target = Object.new(repo, reference) + + if not force and os.path.isfile(abs_ref_path): + target_data = str(target) + if isinstance(target, SymbolicReference): + target_data = target.path + if not resolve: + target_data = "ref: " + target_data + if open(abs_ref_path, 'rb').read().strip() != target_data: + raise OSError("Reference at %s does already exist" % full_ref_path) + # END no force handling + ref = cls(repo, full_ref_path) ref.reference = target return ref @@ -305,6 +313,9 @@ class SymbolicReference(object): Returns Newly created symbolic Reference + Raises OSError + If a (Symbolic)Reference with the same name but different contents + already exists. Note This does not alter the current HEAD, index or Working Tree """ @@ -327,7 +338,7 @@ class SymbolicReference(object): self Raises OSError: - In case a file at path with that name already exists + In case a file at path but a different contents already exists """ new_path = self._to_full_path(self.repo, new_path) if self.path == new_path: @@ -338,7 +349,7 @@ class SymbolicReference(object): if os.path.isfile(new_abs_path): if not force: # if they point to the same file, its not an error - if open(new_abs_path,'rb').read() != open(cur_abs_path,'rb').read(): + if open(new_abs_path,'rb').read().strip() != open(cur_abs_path,'rb').read().strip(): raise OSError("File at path %r already exists" % new_abs_path) # else: we could remove ourselves and use the otherone, but # but clarity we just continue as usual diff --git a/test/git/test_refs.py b/test/git/test_refs.py index 762554dc..97617c1b 100644 --- a/test/git/test_refs.py +++ b/test/git/test_refs.py @@ -242,7 +242,9 @@ class TestRefs(TestBase): assert ref.path == full_ref assert ref.object == rw_repo.head.commit - self.failUnlessRaises(OSError, Reference.create, rw_repo, full_ref) + self.failUnlessRaises(OSError, Reference.create, rw_repo, full_ref, 'HEAD~20') + # it works if it is at the same spot though and points to the same reference + assert Reference.create(rw_repo, full_ref, 'HEAD').path == full_ref Reference.delete(rw_repo, full_ref) # recreate the reference using a full_ref @@ -279,7 +281,9 @@ class TestRefs(TestBase): assert symref.path == symref_path assert symref.reference == cur_head.reference - self.failUnlessRaises(OSError, SymbolicReference.create, rw_repo, symref_path, cur_head.reference) + self.failUnlessRaises(OSError, SymbolicReference.create, rw_repo, symref_path, cur_head.reference.commit) + # it works if the new ref points to the same reference + SymbolicReference.create(rw_repo, symref.path, symref.reference).path == symref.path SymbolicReference.delete(rw_repo, symref) # would raise if the symref wouldn't have been deletedpbl symref = SymbolicReference.create(rw_repo, symref_path, cur_head.reference) |