From 518143743f60a3fc3864fe2f4a6784cc948c96a2 Mon Sep 17 00:00:00 2001 From: wangweichen Date: Thu, 13 Jul 2017 13:56:12 +0800 Subject: [PATCH 1/3] fix open func encoding error. --- .gitignore | 1 + git/refs/symbolic.py | 8 ++++---- git/repo/fun.py | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d35cddebd..b3bc93624 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ nbproject .DS_Store /*egg-info /.tox +.idea/ \ No newline at end of file diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 90ecb62c6..2d29ebdb6 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -76,7 +76,7 @@ def abspath(self): @classmethod def _get_packed_refs_path(cls, repo): try: - commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt').readlines()[0].strip() + commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() except (OSError, IOError): commondir = '.' repodir = osp.join(repo.git_dir, commondir) @@ -87,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 open(cls._get_packed_refs_path(repo), 'rt', encoding="utf-8") as fp: for line in fp: line = line.strip() if not line: @@ -133,7 +133,7 @@ def _get_ref_info_helper(cls, repo, repodir, ref_path): point to, or None""" tokens = None try: - with open(osp.join(repodir, ref_path), 'rt') as fp: + with 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 @@ -173,7 +173,7 @@ def _get_ref_info(cls, repo, ref_path): return cls._get_ref_info_helper(repo, repo.git_dir, ref_path) except ValueError: try: - commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt').readlines()[0].strip() + commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() except (OSError, IOError): commondir = '.' diff --git a/git/repo/fun.py b/git/repo/fun.py index 6aefd9d66..dc229ba20 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -58,7 +58,7 @@ def find_worktree_git_dir(dotgit): return None try: - lines = open(dotgit, 'r').readlines() + lines = open(dotgit, 'r', encoding="utf-8").readlines() for key, value in [line.strip().split(': ') for line in lines]: if key == 'gitdir': return value @@ -73,7 +73,7 @@ def find_submodule_git_dir(d): return d try: - with open(d) as fp: + with open(d, encoding="utf-8") as fp: content = fp.read().rstrip() except (IOError, OSError): # it's probably not a file From d3e10783342ca24fd945766fac005ea0fedb89ec Mon Sep 17 00:00:00 2001 From: wangweichen Date: Thu, 13 Jul 2017 14:31:26 +0800 Subject: [PATCH 2/3] change codes with open func support encoding="utf-8" --- git/refs/symbolic.py | 10 +++++----- git/repo/fun.py | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 2d29ebdb6..8fb630959 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -1,5 +1,5 @@ import os - +import codecs from git.compat import ( string_types, defenc @@ -76,7 +76,7 @@ def abspath(self): @classmethod def _get_packed_refs_path(cls, repo): try: - commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() + commondir = codecs.open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() except (OSError, IOError): commondir = '.' repodir = osp.join(repo.git_dir, commondir) @@ -87,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', encoding="utf-8") 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: @@ -133,7 +133,7 @@ def _get_ref_info_helper(cls, repo, repodir, ref_path): point to, or None""" tokens = None try: - with open(osp.join(repodir, ref_path), 'rt', encoding="utf-8") 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 @@ -173,7 +173,7 @@ def _get_ref_info(cls, repo, ref_path): return cls._get_ref_info_helper(repo, repo.git_dir, ref_path) except ValueError: try: - commondir = open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() + commondir = codecs.open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() except (OSError, IOError): commondir = '.' diff --git a/git/repo/fun.py b/git/repo/fun.py index dc229ba20..21f1ec00c 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -1,21 +1,21 @@ """Package with general repository related functions""" +import codecs import os +import os.path as osp import stat from string import digits -from git.compat import xrange -from git.exc import WorkTreeRepositoryUnsupported -from git.objects import Object -from git.refs import SymbolicReference -from git.util import hex_to_bin, bin_to_hex, decygpath from gitdb.exc import ( BadObject, BadName, ) -import os.path as osp from git.cmd import Git - +from git.compat import xrange +from git.exc import WorkTreeRepositoryUnsupported +from git.objects import Object +from git.refs import SymbolicReference +from git.util import hex_to_bin, bin_to_hex, decygpath __all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_submodule_git_dir', 'name_to_object', 'short_to_long', 'deref_tag', 'to_commit', 'find_worktree_git_dir') @@ -58,7 +58,7 @@ def find_worktree_git_dir(dotgit): return None try: - lines = open(dotgit, 'r', encoding="utf-8").readlines() + lines = codecs.open(dotgit, 'r', encoding="utf-8").readlines() for key, value in [line.strip().split(': ') for line in lines]: if key == 'gitdir': return value @@ -73,7 +73,7 @@ def find_submodule_git_dir(d): return d try: - with open(d, encoding="utf-8") as fp: + with codecs.open(d, encoding="utf-8") as fp: content = fp.read().rstrip() except (IOError, OSError): # it's probably not a file From 27fcca11fb1191431b98a724b6923c51767df279 Mon Sep 17 00:00:00 2001 From: wangweichen Date: Thu, 13 Jul 2017 14:57:54 +0800 Subject: [PATCH 3/3] fix codecs.open can't have text and binary mode at once --- git/refs/symbolic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 8fb630959..b1f17661e 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -76,7 +76,7 @@ def abspath(self): @classmethod def _get_packed_refs_path(cls, repo): try: - commondir = codecs.open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() + commondir = codecs.open(osp.join(repo.git_dir, 'commondir'), 'r', encoding="utf-8").readlines()[0].strip() except (OSError, IOError): commondir = '.' repodir = osp.join(repo.git_dir, commondir) @@ -87,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 codecs.open(cls._get_packed_refs_path(repo), 'rt', encoding="utf-8") as fp: + with codecs.open(cls._get_packed_refs_path(repo), 'r', encoding="utf-8") as fp: for line in fp: line = line.strip() if not line: @@ -133,7 +133,7 @@ def _get_ref_info_helper(cls, repo, repodir, ref_path): point to, or None""" tokens = None try: - with codecs.open(osp.join(repodir, ref_path), 'rt', encoding="utf-8") as fp: + with codecs.open(osp.join(repodir, ref_path), 'r', 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 @@ -173,7 +173,7 @@ def _get_ref_info(cls, repo, ref_path): return cls._get_ref_info_helper(repo, repo.git_dir, ref_path) except ValueError: try: - commondir = codecs.open(osp.join(repo.git_dir, 'commondir'), 'rt', encoding="utf-8").readlines()[0].strip() + commondir = codecs.open(osp.join(repo.git_dir, 'commondir'), 'r', encoding="utf-8").readlines()[0].strip() except (OSError, IOError): commondir = '.'