Skip to content

Commit 6a3bb96

Browse files
committed
Use --git-dir to avoid issues with CVE-2022-24765 mitigation
1 parent ca3855b commit 6a3bb96

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/setuptools_scm/git.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from os.path import isfile
66
from os.path import join
77
from os.path import samefile
8+
from shlex import quote
89

910
from .config import Configuration
1011
from .scm_workdir import Workdir
@@ -36,7 +37,11 @@ class GitWorkdir(Workdir):
3637
def from_potential_worktree(cls, wd):
3738
require_command(cls.COMMAND)
3839
wd = os.path.abspath(wd)
39-
real_wd, _, ret = do_ex("git rev-parse --show-prefix", wd)
40+
git_dir = join(wd, ".git")
41+
real_wd, _, ret = do_ex(
42+
f"git --git-dir={quote(git_dir)} rev-parse --show-prefix",
43+
wd,
44+
)
4045
real_wd = real_wd[:-1] # remove the trailing pathsep
4146
if ret:
4247
return
@@ -54,23 +59,35 @@ def from_potential_worktree(cls, wd):
5459

5560
return cls(real_wd)
5661

62+
def __init__(self, *args, **kwargs):
63+
super().__init__(*args, **kwargs)
64+
self._git_dir = join(self.path, ".git")
65+
5766
def is_dirty(self):
58-
out, _, _ = self.do_ex("git status --porcelain --untracked-files=no")
67+
out, _, _ = self.do_ex(
68+
f"git --git-dir={quote(self._git_dir)} "
69+
"status --porcelain --untracked-files=no"
70+
)
5971
return bool(out)
6072

6173
def get_branch(self):
62-
branch, err, ret = self.do_ex("git rev-parse --abbrev-ref HEAD")
74+
branch, err, ret = self.do_ex(
75+
f"git --git-dir={quote(self._git_dir)} rev-parse --abbrev-ref HEAD"
76+
)
6377
if ret:
6478
trace("branch err", branch, err, ret)
65-
branch, err, ret = self.do_ex("git symbolic-ref --short HEAD")
79+
branch, err, ret = self.do_ex(
80+
f"git --git-dir={quote(self._git_dir)} symbolic-ref --short HEAD"
81+
)
6682
if ret:
6783
trace("branch err (symbolic-ref)", branch, err, ret)
6884
branch = None
6985
return branch
7086

7187
def get_head_date(self):
7288
timestamp, err, ret = self.do_ex(
73-
"git -c log.showSignature=false log -n 1 HEAD --format=%cI"
89+
f"git --git-dir={quote(self._git_dir)} -c log.showSignature=false "
90+
"log -n 1 HEAD --format=%cI"
7491
)
7592
if ret:
7693
trace("timestamp err", timestamp, err, ret)
@@ -83,22 +100,26 @@ def get_head_date(self):
83100
return datetime.strptime(date_part, r"%Y-%m-%d").date()
84101

85102
def is_shallow(self):
86-
return isfile(join(self.path, ".git/shallow"))
103+
return isfile(join(self._git_dir, "shallow"))
87104

88105
def fetch_shallow(self):
89-
self.do_ex("git fetch --unshallow")
106+
self.do_ex(f"git --git-dir={quote(self._git_dir)} fetch --unshallow")
90107

91108
def node(self):
92-
node, _, ret = self.do_ex("git rev-parse --verify --quiet HEAD")
109+
node, _, ret = self.do_ex(
110+
f"git --git-dir={quote(self._git_dir)} rev-parse --verify --quiet HEAD"
111+
)
93112
if not ret:
94113
return node[:7]
95114

96115
def count_all_nodes(self):
97-
revs, _, _ = self.do_ex("git rev-list HEAD")
116+
revs, _, _ = self.do_ex(f"git --git-dir={quote(self._git_dir)} rev-list HEAD")
98117
return revs.count("\n") + 1
99118

100119
def default_describe(self):
101-
return self.do_ex(DEFAULT_DESCRIBE)
120+
return self.do_ex(
121+
DEFAULT_DESCRIBE[:1] + ["--git-dir", self._git_dir] + DEFAULT_DESCRIBE[1:]
122+
)
102123

103124

104125
def warn_on_shallow(wd):

0 commit comments

Comments
 (0)