@@ -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
0 commit comments