Skip to content

Commit e885f2e

Browse files
committed
use -C for git commands in autoupdate
1 parent 148df0d commit e885f2e

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

pre_commit/commands/autoupdate.py

+12-23
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,33 @@ def from_config(cls, config: dict[str, Any]) -> RevInfo:
3434
return cls(config['repo'], config['rev'], None)
3535

3636
def update(self, tags_only: bool, freeze: bool) -> RevInfo:
37-
git_cmd = ('git', *git.NO_FS_MONITOR)
37+
with tempfile.TemporaryDirectory() as tmp:
38+
_git = ('git', *git.NO_FS_MONITOR, '-C', tmp)
3839

39-
if tags_only:
40-
tag_cmd = (
41-
*git_cmd, 'describe',
42-
'FETCH_HEAD', '--tags', '--abbrev=0',
43-
)
44-
else:
45-
tag_cmd = (
46-
*git_cmd, 'describe',
47-
'FETCH_HEAD', '--tags', '--exact',
48-
)
40+
if tags_only:
41+
tag_opt = '--abbrev=0'
42+
else:
43+
tag_opt = '--exact'
44+
tag_cmd = (*_git, 'describe', 'FETCH_HEAD', '--tags', tag_opt)
4945

50-
with tempfile.TemporaryDirectory() as tmp:
5146
git.init_repo(tmp, self.repo)
47+
cmd_output_b(*_git, 'config', 'extensions.partialClone', 'true')
5248
cmd_output_b(
53-
*git_cmd, 'config', 'extensions.partialClone', 'true',
54-
cwd=tmp,
55-
)
56-
cmd_output_b(
57-
*git_cmd, 'fetch', 'origin', 'HEAD',
49+
*_git, 'fetch', 'origin', 'HEAD',
5850
'--quiet', '--filter=blob:none', '--tags',
59-
cwd=tmp,
6051
)
6152

6253
try:
63-
rev = cmd_output(*tag_cmd, cwd=tmp)[1].strip()
54+
rev = cmd_output(*tag_cmd)[1].strip()
6455
except CalledProcessError:
65-
cmd = (*git_cmd, 'rev-parse', 'FETCH_HEAD')
66-
rev = cmd_output(*cmd, cwd=tmp)[1].strip()
56+
rev = cmd_output(*_git, 'rev-parse', 'FETCH_HEAD')[1].strip()
6757
else:
6858
if tags_only:
6959
rev = git.get_best_candidate_tag(rev, tmp)
7060

7161
frozen = None
7262
if freeze:
73-
exact_rev_cmd = (*git_cmd, 'rev-parse', rev)
74-
exact = cmd_output(*exact_rev_cmd, cwd=tmp)[1].strip()
63+
exact = cmd_output(*_git, 'rev-parse', rev)[1].strip()
7564
if exact != rev:
7665
rev, frozen = exact, rev
7766
return self._replace(rev=rev, frozen=frozen)

0 commit comments

Comments
 (0)