9
9
import os
10
10
import re
11
11
import sys
12
+ import warnings
12
13
13
14
from git .cmd import (
14
15
Git ,
@@ -86,7 +87,7 @@ class Repo(object):
86
87
# Subclasses may easily bring in their own custom types by placing a constructor or type here
87
88
GitCommandWrapperType = Git
88
89
89
- def __init__ (self , path = None , odbt = DefaultDBType , search_parent_directories = False ):
90
+ def __init__ (self , path = None , odbt = DefaultDBType , search_parent_directories = False , expand_vars = True ):
90
91
"""Create a new Repo instance
91
92
92
93
:param path:
@@ -112,12 +113,18 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
112
113
:raise InvalidGitRepositoryError:
113
114
:raise NoSuchPathError:
114
115
:return: git.Repo """
116
+
115
117
epath = path or os .getenv ('GIT_DIR' )
116
118
if not epath :
117
119
epath = os .getcwd ()
118
120
if Git .is_cygwin ():
119
121
epath = decygpath (epath )
120
- epath = expand_path (epath or path or os .getcwd ())
122
+
123
+ epath = epath or path or os .getcwd ()
124
+ if expand_vars and ("%" in epath or "$" in epath ):
125
+ warnings .warn ("The use of environment variables in paths is deprecated" +
126
+ "\n for security reasons and may be removed in the future!!" )
127
+ epath = expand_path (epath , expand_vars )
121
128
if not os .path .exists (epath ):
122
129
raise NoSuchPathError (epath )
123
130
@@ -144,7 +151,7 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
144
151
sm_gitpath = find_worktree_git_dir (dotgit )
145
152
146
153
if sm_gitpath is not None :
147
- self .git_dir = expand_path (sm_gitpath )
154
+ self .git_dir = expand_path (sm_gitpath , expand_vars )
148
155
self ._working_tree_dir = curpath
149
156
break
150
157
@@ -840,7 +847,7 @@ def blame(self, rev, file, incremental=False, **kwargs):
840
847
return blames
841
848
842
849
@classmethod
843
- def init (cls , path = None , mkdir = True , odbt = DefaultDBType , ** kwargs ):
850
+ def init (cls , path = None , mkdir = True , odbt = DefaultDBType , expand_vars = True , ** kwargs ):
844
851
"""Initialize a git repository at the given path if specified
845
852
846
853
:param path:
@@ -858,12 +865,17 @@ def init(cls, path=None, mkdir=True, odbt=DefaultDBType, **kwargs):
858
865
the directory containing the database objects, i.e. .git/objects.
859
866
It will be used to access all object data
860
867
868
+ :param expand_vars:
869
+ if specified, environment variables will not be escaped. This
870
+ can lead to information disclosure, allowing attackers to
871
+ access the contents of environment variables
872
+
861
873
:parm kwargs:
862
874
keyword arguments serving as additional options to the git-init command
863
875
864
876
:return: ``git.Repo`` (the newly created repo)"""
865
877
if path :
866
- path = expand_path (path )
878
+ path = expand_path (path , expand_vars )
867
879
if mkdir and path and not osp .exists (path ):
868
880
os .makedirs (path , 0o755 )
869
881
0 commit comments