Skip to content

Commit

Permalink
Merge pull request #664 from Horgix/path_expansion
Browse files Browse the repository at this point in the history
util: move expand_path from repo/base and use it in Git class init
  • Loading branch information
Byron committed Sep 28, 2017
2 parents 2dca537 + a2d6787 commit 3eb497b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Contributors are:
-Timothy B. Hartman <tbhartman _at_ gmail.com>
-Konstantin Popov <konstantin.popov.89 _at_ yandex.ru>
-Peter Jones <pjones _at_ redhat.com>
-Alexis Horgix Chotard

Portions derived from other open source works and are clearly marked.
4 changes: 2 additions & 2 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
from git.exc import CommandError
from git.odict import OrderedDict
from git.util import is_cygwin_git, cygpath
from git.util import is_cygwin_git, cygpath, expand_path

from .exc import (
GitCommandError,
Expand Down Expand Up @@ -405,7 +405,7 @@ def __init__(self, working_dir=None):
It is meant to be the working tree directory if available, or the
.git directory in case of bare repositories."""
super(Git, self).__init__()
self._working_dir = working_dir
self._working_dir = expand_path(working_dir)
self._git_options = ()
self._persistent_git_options = []

Expand Down
12 changes: 4 additions & 8 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from git.objects import Submodule, RootModule, Commit
from git.refs import HEAD, Head, Reference, TagReference
from git.remote import Remote, add_progress, to_progress_instance
from git.util import Actor, finalize_process, decygpath, hex_to_bin
from git.util import Actor, finalize_process, decygpath, hex_to_bin, expand_path
import os.path as osp

from .fun import rev_parse, is_git_dir, find_submodule_git_dir, touch, find_worktree_git_dir
Expand All @@ -50,10 +50,6 @@
__all__ = ('Repo',)


def _expand_path(p):
return osp.normpath(osp.abspath(osp.expandvars(osp.expanduser(p))))


class Repo(object):
"""Represents a git repository and allows you to query references,
gather commit information, generate diffs, create and clone repositories query
Expand Down Expand Up @@ -121,7 +117,7 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
epath = os.getcwd()
if Git.is_cygwin():
epath = decygpath(epath)
epath = _expand_path(epath or path or os.getcwd())
epath = expand_path(epath or path or os.getcwd())
if not os.path.exists(epath):
raise NoSuchPathError(epath)

Expand All @@ -148,7 +144,7 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
sm_gitpath = find_worktree_git_dir(dotgit)

if sm_gitpath is not None:
self.git_dir = _expand_path(sm_gitpath)
self.git_dir = expand_path(sm_gitpath)
self._working_tree_dir = curpath
break

Expand Down Expand Up @@ -867,7 +863,7 @@ def init(cls, path=None, mkdir=True, odbt=DefaultDBType, **kwargs):
:return: ``git.Repo`` (the newly created repo)"""
if path:
path = _expand_path(path)
path = expand_path(path)
if mkdir and path and not osp.exists(path):
os.makedirs(path, 0o755)

Expand Down
7 changes: 7 additions & 0 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ def finalize_process(proc, **kwargs):
## TODO: No close proc-streams??
proc.wait(**kwargs)


def expand_path(p):
try:
return osp.normpath(osp.abspath(osp.expandvars(osp.expanduser(p))))
except:
return None

#} END utilities

#{ Classes
Expand Down

0 comments on commit 3eb497b

Please sign in to comment.