Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc05ef0e

Browse files
committed
repo: renamed directories to more descriptive identifiers and made them safer to use in case of bare repositories
1 parent1eae9d1 commitc05ef0e

File tree

14 files changed

+112
-63
lines changed

14 files changed

+112
-63
lines changed

‎CHANGES‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ GitCommand
5757
* git.subcommand call scheme now prunes out None from the argument list, allowing
5858
to be called more confortably as None can never be a valid to the git command
5959
if converted to a string.
60+
* Renamed 'git_dir' attribute to 'working_dir' which is exactly how it is used
6061

6162
Commit
6263
------
@@ -135,6 +136,11 @@ Repo
135136
- 'config_reader' method
136137
- 'config_writer' method
137138
- 'bare' property, previously it was a simple attribute that could be written
139+
* Renamed the following attributes
140+
- 'path' is now 'git_dir'
141+
- 'wd' is now 'working_dir'
142+
* Added attribute
143+
- 'working_tree_dir' which may be None in case of bare repositories
138144

139145
Remote
140146
------

‎lib/git/cmd.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Git(object):
4343
of the command to stdout.
4444
Set its value to 'full' to see details about the returned values.
4545
"""
46+
__slots__= ("_working_dir","cat_file_all","cat_file_header")
47+
4648
classAutoInterrupt(object):
4749
"""
4850
Kill/Interrupt the stored process instance once this instance goes out of scope. It is
@@ -92,18 +94,18 @@ def wait(self):
9294

9395

9496

95-
def__init__(self,git_dir=None):
97+
def__init__(self,working_dir=None):
9698
"""
9799
Initialize this instance with:
98100
99-
``git_dir``
101+
``working_dir``
100102
Git directory we should work in. If None, we always work in the current
101103
directory as returned by os.getcwd().
102104
It is meant to be the working tree directory if available, or the
103105
.git directory in case of bare repositories.
104106
"""
105107
super(Git,self).__init__()
106-
self.git_dir=git_dir
108+
self._working_dir=working_dir
107109

108110
# cached command slots
109111
self.cat_file_header=None
@@ -121,12 +123,12 @@ def __getattr__(self, name):
121123
returnlambda*args,**kwargs:self._call_process(name,*args,**kwargs)
122124

123125
@property
124-
defget_dir(self):
126+
defworking_dir(self):
125127
"""
126128
Returns
127129
Git directory we are working on
128130
"""
129-
returnself.git_dir
131+
returnself._working_dir
130132

131133
defexecute(self,command,
132134
istream=None,
@@ -150,8 +152,8 @@ def execute(self, command,
150152
151153
``with_keep_cwd``
152154
Whether to use the current working directory from os.getcwd().
153-
GitPython uses get_work_tree() as itsworking directory by
154-
default and get_git_dir() for bare repositories.
155+
The cmd otherwise uses itsown working_dir that it has been initialized
156+
with if possible.
155157
156158
``with_extended_output``
157159
Whether to return a (status, stdout, stderr) tuple.
@@ -198,10 +200,10 @@ def execute(self, command,
198200
print' '.join(command)
199201

200202
# Allow the user to have the command executed in their working dir.
201-
ifwith_keep_cwdorself.git_dirisNone:
203+
ifwith_keep_cwdorself._working_dirisNone:
202204
cwd=os.getcwd()
203205
else:
204-
cwd=self.git_dir
206+
cwd=self._working_dir
205207

206208
# Start the process
207209
proc=subprocess.Popen(command,

‎lib/git/index.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def _set_cache_(self, attr):
325325
super(IndexFile,self)._set_cache_(attr)
326326

327327
def_index_path(self):
328-
returnjoin_path_native(self.repo.path,"index")
328+
returnjoin_path_native(self.repo.git_dir,"index")
329329

330330

331331
@property
@@ -539,15 +539,15 @@ def from_tree(cls, repo, *treeish, **kwargs):
539539

540540
# tmp file created in git home directory to be sure renaming
541541
# works - /tmp/ dirs could be on another device
542-
tmp_index=tempfile.mktemp('','',repo.path)
542+
tmp_index=tempfile.mktemp('','',repo.git_dir)
543543
arg_list.append("--index-output=%s"%tmp_index)
544544
arg_list.extend(treeish)
545545

546546
# move current index out of the way - otherwise the merge may fail
547547
# as it considers existing entries. moving it essentially clears the index.
548548
# Unfortunately there is no 'soft' way to do it.
549549
# The _TemporaryFileSwap assure the original file get put back
550-
index_handler=_TemporaryFileSwap(join_path_native(repo.path,'index'))
550+
index_handler=_TemporaryFileSwap(join_path_native(repo.git_dir,'index'))
551551
try:
552552
repo.git.read_tree(*arg_list,**kwargs)
553553
index=cls(repo,tmp_index)
@@ -579,7 +579,7 @@ def _index_mode_to_tree_index_mode(cls, index_mode):
579579
returnret
580580

581581

582-
# UTILITIES
582+
# UTILITIES
583583
def_iter_expand_paths(self,paths):
584584
"""Expand the directories in list of paths to the corresponding paths accordingly,
585585
@@ -588,7 +588,7 @@ def _iter_expand_paths(self, paths):
588588
times - we respect that and do not prune"""
589589
defraise_exc(e):
590590
raisee
591-
r=self.repo.git.git_dir
591+
r=self.repo.working_tree_dir
592592
rs=r+'/'
593593
forpathinpaths:
594594
abs_path=path
@@ -798,9 +798,9 @@ def _to_relative_path(self, path):
798798
"""
799799
ifnotos.path.isabs(path):
800800
returnpath
801-
relative_path=path.replace(self.repo.git.git_dir+os.sep,"")
801+
relative_path=path.replace(self.repo.working_tree_dir+os.sep,"")
802802
ifrelative_path==path:
803-
raiseValueError("Absolute path %r is not in git repository at %r"% (path,self.repo.git.git_dir))
803+
raiseValueError("Absolute path %r is not in git repository at %r"% (path,self.repo.working_tree_dir))
804804
returnrelative_path
805805

806806
def_preprocess_add_items(self,items):

‎lib/git/objects/base.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,5 @@ def abspath(self):
223223
224224
The returned path will be native to the system and contains '\' on windows.
225225
"""
226-
returnjoin_path_native(self.repo.git.git_dir,self.path)
226+
returnjoin_path_native(self.repo.working_tree_dir,self.path)
227227

‎lib/git/refs.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ def name(self):
5252
returnself.path
5353

5454
def_get_path(self):
55-
returnjoin_path_native(self.repo.path,self.path)
55+
returnjoin_path_native(self.repo.git_dir,self.path)
5656

5757
@classmethod
5858
def_iter_packed_refs(cls,repo):
5959
"""Returns an iterator yielding pairs of sha1/path pairs for the corresponding
6060
refs.
6161
NOTE: The packed refs file will be kept open as long as we iterate"""
6262
try:
63-
fp=open(os.path.join(repo.path,'packed-refs'),'r')
63+
fp=open(os.path.join(repo.git_dir,'packed-refs'),'r')
6464
forlineinfp:
6565
line=line.strip()
6666
ifnotline:
@@ -258,7 +258,7 @@ def delete(cls, repo, path):
258258
Alternatively the symbolic reference to be deleted
259259
"""
260260
full_ref_path=cls._to_full_path(repo,path)
261-
abs_path=os.path.join(repo.path,full_ref_path)
261+
abs_path=os.path.join(repo.git_dir,full_ref_path)
262262
ifos.path.exists(abs_path):
263263
os.remove(abs_path)
264264

@@ -271,7 +271,7 @@ def _create(cls, repo, path, resolve, reference, force):
271271
instead"""
272272
full_ref_path=cls._to_full_path(repo,path)
273273

274-
abs_ref_path=os.path.join(repo.path,full_ref_path)
274+
abs_ref_path=os.path.join(repo.git_dir,full_ref_path)
275275
ifnotforceandos.path.isfile(abs_ref_path):
276276
raiseOSError("Reference at %s does already exist"%full_ref_path)
277277

@@ -401,10 +401,10 @@ def iter_items(cls, repo, common_path = None, **kwargs):
401401

402402
# walk loose refs
403403
# Currently we do not follow links
404-
forroot,dirs,filesinos.walk(join_path_native(repo.path,common_path)):
404+
forroot,dirs,filesinos.walk(join_path_native(repo.git_dir,common_path)):
405405
forfinfiles:
406406
abs_path=to_native_path_linux(join_path(root,f))
407-
rela_paths.add(abs_path.replace(to_native_path_linux(repo.path)+'/',""))
407+
rela_paths.add(abs_path.replace(to_native_path_linux(repo.git_dir)+'/',""))
408408
# END for each file in root directory
409409
# END for each directory to walk
410410

‎lib/git/remote.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def _get_fetch_info_from_stderr(self, stderr):
575575
err_info=stderr.splitlines()[1:]
576576

577577
# read head information
578-
fp=open(os.path.join(self.repo.path,'FETCH_HEAD'),'r')
578+
fp=open(os.path.join(self.repo.git_dir,'FETCH_HEAD'),'r')
579579
fetch_head_info=fp.readlines()
580580
fp.close()
581581

‎lib/git/repo.py‎

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ class Repo(object):
4141
Represents a git repository and allows you to query references,
4242
gather commit information, generate diffs, create and clone repositories query
4343
the log.
44+
45+
The following attributes are worth using:
46+
47+
'working_dir' is the working directory of the git command, wich is the working tree
48+
directory if available or the .git directory in case of bare repositories
49+
50+
'working_tree_dir' is the working tree directory, but will raise AssertionError
51+
if we are a bare repository.
52+
53+
'git_dir' is the .git repository directoy, which is always set.
4454
"""
4555
DAEMON_EXPORT_FILE='git-daemon-export-ok'
46-
__slots__= ("wd","path","_bare","git" )
56+
__slots__= ("working_dir","_working_tree_dir","git_dir","_bare","git" )
4757

4858
# precompiled regex
4959
re_whitespace=re.compile(r'\s+')
@@ -81,26 +91,28 @@ def __init__(self, path=None):
8191
ifnotos.path.exists(epath):
8292
raiseNoSuchPathError(epath)
8393

84-
self.path=None
94+
self.working_dir=None
95+
self._working_tree_dir=None
96+
self.git_dir=None
8597
curpath=epath
8698

8799
# walk up the path to find the .git dir
88100
whilecurpath:
89101
ifis_git_dir(curpath):
90-
self.path=curpath
91-
self.wd=os.path.dirname(curpath)
102+
self.git_dir=curpath
103+
self._working_tree_dir=os.path.dirname(curpath)
92104
break
93105
gitpath=os.path.join(curpath,'.git')
94106
ifis_git_dir(gitpath):
95-
self.path=gitpath
96-
self.wd=curpath
107+
self.git_dir=gitpath
108+
self._working_tree_dir=curpath
97109
break
98110
curpath,dummy=os.path.split(curpath)
99111
ifnotdummy:
100112
break
101113
# END while curpath
102114

103-
ifself.pathisNone:
115+
ifself.git_dirisNone:
104116
raiseInvalidGitRepositoryError(epath)
105117

106118
self._bare=False
@@ -113,24 +125,38 @@ def __init__(self, path=None):
113125
# adjust the wd in case we are actually bare - we didn't know that
114126
# in the first place
115127
ifself._bare:
116-
self.wd=self.path
117-
118-
self.git=Git(self.wd)
128+
self._working_tree_dir=None
129+
# END working dir handling
130+
131+
self.working_dir=self._working_tree_dirorself.git_dir
132+
self.git=Git(self.working_dir)
119133

120134
# Description property
121135
def_get_description(self):
122-
filename=os.path.join(self.path,'description')
136+
filename=os.path.join(self.git_dir,'description')
123137
returnfile(filename).read().rstrip()
124138

125139
def_set_description(self,descr):
126-
filename=os.path.join(self.path,'description')
140+
filename=os.path.join(self.git_dir,'description')
127141
file(filename,'w').write(descr+'\n')
128142

129143
description=property(_get_description,_set_description,
130144
doc="the project's description")
131145
del_get_description
132146
del_set_description
133147

148+
@property
149+
defworking_tree_dir(self):
150+
"""
151+
Returns
152+
The working tree directory of our git repository
153+
154+
Raises AssertionError
155+
If we are a bare repository
156+
"""
157+
ifself._working_tree_dirisNone:
158+
raiseAssertionError("Repository at %r is bare and does not have a working tree directory"%self.git_dir )
159+
returnself._working_tree_dir
134160

135161
@property
136162
defbare(self):
@@ -286,7 +312,7 @@ def _get_config_path(self, config_level ):
286312
elifconfig_level=="global":
287313
returnos.path.expanduser("~/.gitconfig")
288314
elifconfig_level=="repository":
289-
return"%s/config"%self.path
315+
return"%s/config"%self.git_dir
290316

291317
raiseValueError("Invalid configuration level: %r"%config_level )
292318

@@ -413,11 +439,11 @@ def iter_commits(self, rev=None, paths='', **kwargs):
413439
returnCommit.iter_items(self,rev,paths,**kwargs)
414440

415441
def_get_daemon_export(self):
416-
filename=os.path.join(self.path,self.DAEMON_EXPORT_FILE)
442+
filename=os.path.join(self.git_dir,self.DAEMON_EXPORT_FILE)
417443
returnos.path.exists(filename)
418444

419445
def_set_daemon_export(self,value):
420-
filename=os.path.join(self.path,self.DAEMON_EXPORT_FILE)
446+
filename=os.path.join(self.git_dir,self.DAEMON_EXPORT_FILE)
421447
fileexists=os.path.exists(filename)
422448
ifvalueandnotfileexists:
423449
touch(filename)
@@ -436,7 +462,7 @@ def _get_alternates(self):
436462
Returns
437463
list of strings being pathnames of alternates
438464
"""
439-
alternates_path=os.path.join(self.path,'objects','info','alternates')
465+
alternates_path=os.path.join(self.git_dir,'objects','info','alternates')
440466

441467
ifos.path.exists(alternates_path):
442468
try:
@@ -466,7 +492,7 @@ def _set_alternates(self, alts):
466492
Returns
467493
None
468494
"""
469-
alternates_path=os.path.join(self.path,'objects','info','alternates')
495+
alternates_path=os.path.join(self.git_dir,'objects','info','alternates')
470496
ifnotalts:
471497
ifos.path.isfile(alternates_path):
472498
os.remove(alternates_path)
@@ -706,7 +732,7 @@ def clone(self, path, **kwargs):
706732
# END windows handling
707733

708734
try:
709-
self.git.clone(self.path,path,**kwargs)
735+
self.git.clone(self.git_dir,path,**kwargs)
710736
finally:
711737
ifprev_cwdisnotNone:
712738
os.chdir(prev_cwd)
@@ -754,4 +780,4 @@ def archive(self, ostream, treeish=None, prefix=None, **kwargs):
754780
returnself
755781

756782
def__repr__(self):
757-
return'<git.Repo "%s">'%self.path
783+
return'<git.Repo "%s">'%self.git_dir

‎lib/git/utils.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,18 @@ def iter_items(cls, repo, *args, **kwargs):
362362
"""
363363
raiseNotImplementedError("To be implemented by Subclass")
364364

365+
defneeds_working_tree(func):
366+
"""
367+
Decorator assuring the wrapped method may only run if the repository has a
368+
working tree, hence it is not bare.
369+
"""
370+
defcheck_default_index(self,*args,**kwargs):
371+
ifself.repo.working_tree_dirisNone:
372+
raiseAssertionError("Cannot call %r bare git repositories"%func.__name__ )
373+
returnfunc(self,*args,**kwargs)
374+
# END wrpaper method
375+
376+
check_default_index.__name__=func.__name__
377+
returncheck_default_index
378+
379+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp