Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4ed59a

Browse files
committed
SymbolicReference creation now won't fail if the target already exists and has the same content as the new reference
1 parent 4e72942 commit e4ed59a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/git/refs.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _set_reference(self, ref):
187187
if not os.path.isdir(directory):
188188
os.makedirs(directory)
189189

190-
fp = open(path, "w")
190+
fp = open(path, "wb")
191191
try:
192192
fp.write(write_value)
193193
finally:
@@ -270,16 +270,24 @@ def _create(cls, repo, path, resolve, reference, force):
270270
corresponding object and a detached symbolic reference will be created
271271
instead"""
272272
full_ref_path = cls._to_full_path(repo, path)
273-
274273
abs_ref_path = os.path.join(repo.git_dir, full_ref_path)
275-
if not force and os.path.isfile(abs_ref_path):
276-
raise OSError("Reference at %s does already exist" % full_ref_path)
277274

278-
ref = cls(repo, full_ref_path)
275+
# figure out target data
279276
target = reference
280277
if resolve:
281278
target = Object.new(repo, reference)
279+
280+
if not force and os.path.isfile(abs_ref_path):
281+
target_data = str(target)
282+
if isinstance(target, SymbolicReference):
283+
target_data = target.path
284+
if not resolve:
285+
target_data = "ref: " + target_data
286+
if open(abs_ref_path, 'rb').read().strip() != target_data:
287+
raise OSError("Reference at %s does already exist" % full_ref_path)
288+
# END no force handling
282289

290+
ref = cls(repo, full_ref_path)
283291
ref.reference = target
284292
return ref
285293

@@ -305,6 +313,9 @@ def create(cls, repo, path, reference='HEAD', force=False ):
305313
Returns
306314
Newly created symbolic Reference
307315
316+
Raises OSError
317+
If a (Symbolic)Reference with the same name but different contents
318+
already exists.
308319
Note
309320
This does not alter the current HEAD, index or Working Tree
310321
"""
@@ -327,7 +338,7 @@ def rename(self, new_path, force=False):
327338
self
328339
329340
Raises OSError:
330-
In case a file at path with that name already exists
341+
In case a file at path but a different contents already exists
331342
"""
332343
new_path = self._to_full_path(self.repo, new_path)
333344
if self.path == new_path:
@@ -338,7 +349,7 @@ def rename(self, new_path, force=False):
338349
if os.path.isfile(new_abs_path):
339350
if not force:
340351
# if they point to the same file, its not an error
341-
if open(new_abs_path,'rb').read() != open(cur_abs_path,'rb').read():
352+
if open(new_abs_path,'rb').read().strip() != open(cur_abs_path,'rb').read().strip():
342353
raise OSError("File at path %r already exists" % new_abs_path)
343354
# else: we could remove ourselves and use the otherone, but
344355
# but clarity we just continue as usual

test/git/test_refs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ def test_head_reset(self, rw_repo):
242242
assert ref.path == full_ref
243243
assert ref.object == rw_repo.head.commit
244244

245-
self.failUnlessRaises(OSError, Reference.create, rw_repo, full_ref)
245+
self.failUnlessRaises(OSError, Reference.create, rw_repo, full_ref, 'HEAD~20')
246+
# it works if it is at the same spot though and points to the same reference
247+
assert Reference.create(rw_repo, full_ref, 'HEAD').path == full_ref
246248
Reference.delete(rw_repo, full_ref)
247249

248250
# recreate the reference using a full_ref
@@ -279,7 +281,9 @@ def test_head_reset(self, rw_repo):
279281
assert symref.path == symref_path
280282
assert symref.reference == cur_head.reference
281283

282-
self.failUnlessRaises(OSError, SymbolicReference.create, rw_repo, symref_path, cur_head.reference)
284+
self.failUnlessRaises(OSError, SymbolicReference.create, rw_repo, symref_path, cur_head.reference.commit)
285+
# it works if the new ref points to the same reference
286+
SymbolicReference.create(rw_repo, symref.path, symref.reference).path == symref.path
283287
SymbolicReference.delete(rw_repo, symref)
284288
# would raise if the symref wouldn't have been deletedpbl
285289
symref = SymbolicReference.create(rw_repo, symref_path, cur_head.reference)

0 commit comments

Comments
 (0)