summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-12-03 22:40:25 +0100
committerSebastian Thiel <byronimo@gmail.com>2009-12-03 22:40:25 +0100
commit4e729429631c84c3bd5602edcab3e7c2ab1dcce0 (patch)
tree5adf9421a59f1f136d4b0a8b7450a56692ce7936
parent12276fedec49c855401262f0929f088ebf33d2cf (diff)
downloadgitpython-4e729429631c84c3bd5602edcab3e7c2ab1dcce0.tar.gz
refs.rename: improved handling as it will only raise ( if not forced ) if the destination contains different data. If it contains the same, the move will work as we can do what the user intended
-rw-r--r--lib/git/refs.py9
-rw-r--r--test/git/test_refs.py5
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/git/refs.py b/lib/git/refs.py
index 0b8b2a98..ec70c72b 100644
--- a/lib/git/refs.py
+++ b/lib/git/refs.py
@@ -334,9 +334,15 @@ class SymbolicReference(object):
return self
new_abs_path = os.path.join(self.repo.git_dir, new_path)
+ cur_abs_path = os.path.join(self.repo.git_dir, self.path)
if os.path.isfile(new_abs_path):
if not force:
- raise OSError("File at path %r already exists" % new_abs_path)
+ # if they point to the same file, its not an error
+ if open(new_abs_path,'rb').read() != open(cur_abs_path,'rb').read():
+ 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
+ # END not force handling
os.remove(new_abs_path)
# END handle existing target file
@@ -345,7 +351,6 @@ class SymbolicReference(object):
os.makedirs(dirname)
# END create directory
- cur_abs_path = os.path.join(self.repo.git_dir, self.path)
os.rename(cur_abs_path, new_abs_path)
self.path = new_path
diff --git a/test/git/test_refs.py b/test/git/test_refs.py
index aee9b706..762554dc 100644
--- a/test/git/test_refs.py
+++ b/test/git/test_refs.py
@@ -267,7 +267,10 @@ class TestRefs(TestBase):
# exists, fail unless we force
ex_ref_path = far_away_head.path
self.failUnlessRaises(OSError, ref.rename, ex_ref_path)
- assert ref.rename(ex_ref_path, force=True).path == ex_ref_path and ref.object == orig_obj
+ # if it points to the same commit it works
+ far_away_head.commit = ref.commit
+ ref.rename(ex_ref_path)
+ assert ref.path == ex_ref_path and ref.object == orig_obj
assert ref.rename(ref.path).path == ex_ref_path # rename to same name
# create symbolic refs