◐ Shell
clean mode source ↗

open func support "utf-8" by nooperpudd · Pull Request #641 · gitpython-developers/GitPython

@@ -1,4 +1,10 @@ import os import os.path as osp import codecs from gitdb.exc import ( BadObject, BadName )
from git.compat import ( string_types, Expand All @@ -13,16 +19,8 @@ hex_to_bin, LockedFD ) from gitdb.exc import ( BadObject, BadName )
import os.path as osp
from .log import RefLog

__all__ = ["SymbolicReference"]

Expand All @@ -35,7 +33,6 @@ def _git_dir(repo, path):

class SymbolicReference(object):
"""Represents a special case of a reference such that this reference is symbolic. It does not point to a specific commit, but to another Head, which itself specifies a commit. Expand Down Expand Up @@ -90,7 +87,7 @@ def _iter_packed_refs(cls, repo): """Returns an iterator yielding pairs of sha1/path pairs (as bytes) for the corresponding refs. :note: The packed refs file will be kept open as long as we iterate""" try: with open(cls._get_packed_refs_path(repo), 'rt') as fp: with codecs.open(cls._get_packed_refs_path(repo), 'rt', encoding="utf-8") as fp: for line in fp: line = line.strip() if not line: Expand All @@ -108,14 +105,14 @@ def _iter_packed_refs(cls, repo): continue
yield tuple(line.split(' ', 1)) # END for each line # END for each line except (OSError, IOError): return # END no packed-refs file handling # NOTE: Had try-finally block around here to close the fp, # but some python version wouldn't allow yields within that. # I believe files are closing themselves on destruction, so it is # alright. # END no packed-refs file handling # NOTE: Had try-finally block around here to close the fp, # but some python version wouldn't allow yields within that. # I believe files are closing themselves on destruction, so it is # alright.
@classmethod def dereference_recursive(cls, repo, ref_path): Expand All @@ -127,7 +124,7 @@ def dereference_recursive(cls, repo, ref_path): hexsha, ref_path = cls._get_ref_info(repo, ref_path) if hexsha is not None: return hexsha # END recursive dereferencing # END recursive dereferencing
@classmethod def _get_ref_info_helper(cls, repo, ref_path): Expand All @@ -137,12 +134,12 @@ def _get_ref_info_helper(cls, repo, ref_path): tokens = None repodir = _git_dir(repo, ref_path) try: with open(osp.join(repodir, ref_path), 'rt') as fp: with codecs.open(osp.join(repodir, ref_path), 'rt', encoding="utf-8") 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() assert(len(tokens) != 0) assert (len(tokens) != 0) except (OSError, IOError): # Probably we are just packed, find our entry in the packed refs file # NOTE: We are not a symbolic ref if we are in a packed file, as these Expand All @@ -153,7 +150,7 @@ def _get_ref_info_helper(cls, repo, ref_path): # sha will be used tokens = sha, path break # END for each packed ref # END for each packed ref # END handle packed refs if tokens is None: raise ValueError("Reference at %r does not exist" % ref_path) Expand Down Expand Up @@ -216,7 +213,7 @@ def set_commit(self, commit, logmsg=None): invalid_type = self.repo.rev_parse(commit).type != Commit.type except (BadObject, BadName): raise ValueError("Invalid object: %s" % commit) # END handle exception # END handle exception # END verify type
if invalid_type: Expand Down Expand Up @@ -294,11 +291,11 @@ def set_reference(self, ref, logmsg=None): write_value = ref.hexsha elif isinstance(ref, string_types): try: obj = self.repo.rev_parse(ref + "^{}") # optionally deref tags obj = self.repo.rev_parse(ref + "^{}") # optionally deref tags write_value = obj.hexsha except (BadObject, BadName): raise ValueError("Could not extract object from %s" % ref) # END end try string # END end try string else: raise ValueError("Unrecognized Value: %r" % ref) # END try commit attribute Expand All @@ -314,7 +311,7 @@ def set_reference(self, ref, logmsg=None): oldbinsha = self.commit.binsha except ValueError: oldbinsha = Commit.NULL_BIN_SHA # END handle non-existing # END handle non-existing # END retrieve old hexsha
fpath = self.abspath Expand Down Expand Up @@ -470,7 +467,7 @@ def delete(cls, repo, path): reflog_path = RefLog.path(cls(repo, full_ref_path)) if osp.isfile(reflog_path): os.remove(reflog_path) # END remove reflog # END remove reflog
@classmethod def _create(cls, repo, path, resolve, reference, force, logmsg=None): Expand Down Expand Up @@ -566,8 +563,8 @@ def rename(self, new_path, force=False): 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 # 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 Expand Down Expand Up @@ -602,14 +599,14 @@ def _iter_items(cls, repo, common_path=None): continue abs_path = to_native_path_linux(join_path(root, f)) rela_paths.add(abs_path.replace(to_native_path_linux(repo.common_dir) + '/', "")) # END for each file in root directory # END for each file in root directory # END for each directory to walk
# read packed refs for sha, rela_path in cls._iter_packed_refs(repo): # @UnusedVariable if rela_path.startswith(common_path): rela_paths.add(rela_path) # END relative path matches common path # END relative path matches common path # END packed refs reading
# return paths in sorted order Expand All @@ -618,7 +615,7 @@ def _iter_items(cls, repo, common_path=None): yield cls.from_path(repo, path) except ValueError: continue # END for each sorted relative refpath # END for each sorted relative refpath
@classmethod def iter_items(cls, repo, common_path=None): Expand Down Expand Up @@ -662,7 +659,7 @@ def from_path(cls, repo, path): return instance except ValueError: pass # END exception handling # END exception handling # END for each type to try raise ValueError("Could not find reference type suitable to handle path %r" % path)
Expand Down