diff options
Diffstat (limited to 'git/refs/symbolic.py')
-rw-r--r-- | git/refs/symbolic.py | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index ec2944c6..894b26d5 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -134,9 +134,8 @@ class SymbolicReference(object): point to, or None""" tokens = None try: - fp = open(join(repo.git_dir, ref_path), 'rt') - value = fp.read().rstrip() - fp.close() + with open(join(repo.git_dir, ref_path), 'rt') as fp: + value = fp.read().rstrip() # Don't only split on spaces, but on whitespace, which allows to parse lines like # 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo tokens = value.split() @@ -313,13 +312,17 @@ class SymbolicReference(object): lfd = LockedFD(fpath) fd = lfd.open(write=True, stream=True) - fd.write(write_value.encode('ascii') + b'\n') - lfd.commit() - + ok = True + try: + fd.write(write_value.encode('ascii') + b'\n') + lfd.commit() + ok = True + finally: + if not ok: + lfd.rollback() # Adjust the reflog if logmsg is not None: self.log_append(oldbinsha, logmsg) - # END handle reflog return self @@ -422,40 +425,36 @@ class SymbolicReference(object): # check packed refs pack_file_path = cls._get_packed_refs_path(repo) try: - reader = open(pack_file_path, 'rb') - except (OSError, IOError): - pass # it didnt exist at all - else: - new_lines = list() - made_change = False - dropped_last_line = False - for line in reader: - # keep line if it is a comment or if the ref to delete is not - # in the line - # If we deleted the last line and this one is a tag-reference object, - # we drop it as well - line = line.decode(defenc) - if (line.startswith('#') or full_ref_path not in line) and \ - (not dropped_last_line or dropped_last_line and not line.startswith('^')): - new_lines.append(line) - dropped_last_line = False - continue - # END skip comments and lines without our path - - # drop this line - made_change = True - dropped_last_line = True - # END for each line in packed refs - reader.close() + with open(pack_file_path, 'rb') as reader: + new_lines = list() + made_change = False + dropped_last_line = False + for line in reader: + # keep line if it is a comment or if the ref to delete is not + # in the line + # If we deleted the last line and this one is a tag-reference object, + # we drop it as well + line = line.decode(defenc) + if (line.startswith('#') or full_ref_path not in line) and \ + (not dropped_last_line or dropped_last_line and not line.startswith('^')): + new_lines.append(line) + dropped_last_line = False + continue + # END skip comments and lines without our path + + # drop this line + made_change = True + dropped_last_line = True # write the new lines if made_change: # write-binary is required, otherwise windows will # open the file in text mode and change LF to CRLF ! - open(pack_file_path, 'wb').writelines(l.encode(defenc) for l in new_lines) - # END write out file - # END open exception handling - # END handle deletion + with open(pack_file_path, 'wb') as fd: + fd.writelines(l.encode(defenc) for l in new_lines) + + except (OSError, IOError): + pass # it didnt exist at all # delete the reflog reflog_path = RefLog.path(cls(repo, full_ref_path)) @@ -484,7 +483,8 @@ class SymbolicReference(object): target_data = target.path if not resolve: target_data = "ref: " + target_data - existing_data = open(abs_ref_path, 'rb').read().decode(defenc).strip() + with open(abs_ref_path, 'rb') as fd: + existing_data = fd.read().decode(defenc).strip() if existing_data != target_data: raise OSError("Reference at %r does already exist, pointing to %r, requested was %r" % (full_ref_path, existing_data, target_data)) @@ -549,7 +549,11 @@ class SymbolicReference(object): if 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().strip() != open(cur_abs_path, 'rb').read().strip(): + with open(new_abs_path, 'rb') as fd1: + f1 = fd1.read().strip() + with open(cur_abs_path, 'rb') as fd2: + f2 = fd2.read().strip() + if f1 != f2: 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 |