This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathversioning_systems.py
More file actions
201 lines (164 loc) · 6.33 KB
/
versioning_systems.py
File metadata and controls
201 lines (164 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from itertools import chain
import logging
import re
import subprocess
import typing as t
from pathlib import Path
from shutil import which
from codecov_cli.fallbacks import FallbackFieldEnum
from codecov_cli.helpers.folder_searcher import search_files
from codecov_cli.helpers.git import parse_git_service, parse_slug
from abc import ABC, abstractmethod
logger = logging.getLogger("codecovcli")
IGNORE_DIRS = [
"*.egg-info",
".DS_Store",
".circleci",
".env",
".envs",
".git",
".gitignore",
".mypy_cache",
".nvmrc",
".nyc_output",
".ruff_cache",
".venv",
".venvns",
".virtualenv",
".virtualenvs",
"__pycache__",
"bower_components",
"build/lib/",
"jspm_packages",
"node_modules",
"vendor",
"virtualenv",
"virtualenvs",
]
IGNORE_PATHS = [
"*.gif",
"*.jpeg",
"*.jpg",
"*.md",
"*.png",
"shunit2*",
]
class VersioningSystemInterface(ABC):
def __repr__(self) -> str:
return str(type(self))
@abstractmethod
def get_fallback_value(self, fallback_field: FallbackFieldEnum) -> t.Optional[str]:
pass
@abstractmethod
def get_network_root(self) -> t.Optional[Path]:
pass
@abstractmethod
def list_relevant_files(
self, directory: t.Optional[Path] = None, recurse_submodules: bool = False
) -> t.Optional[t.List[str]]:
pass
def get_versioning_system() -> t.Optional[VersioningSystemInterface]:
for klass in [GitVersioningSystem, NoVersioningSystem]:
if klass.is_available():
logger.debug(f"versioning system found: {klass}")
return klass()
class GitVersioningSystem(VersioningSystemInterface):
@classmethod
def is_available(cls):
if which("git") is not None:
p = subprocess.run(
["git", "rev-parse", "--show-toplevel"], capture_output=True
)
if p.stdout:
return True
return False
def get_fallback_value(self, fallback_field: FallbackFieldEnum):
if fallback_field == FallbackFieldEnum.commit_sha:
# here we will get the commit SHA of the latest commit
# that is NOT a merge commit
p = subprocess.run(
# List current commit parent's SHA
["git", "rev-parse", "HEAD^@"],
capture_output=True,
)
parents_hash = p.stdout.decode().strip().splitlines()
if len(parents_hash) == 2:
# IFF the current commit is a merge commit it will have 2 parents
# We return the 2nd one - The commit that came from the branch merged into ours
return parents_hash[1]
# At this point we know the current commit is not a merge commit
# so we get it's SHA and return that
p = subprocess.run(["git", "log", "-1", "--format=%H"], capture_output=True)
if p.stdout:
return p.stdout.decode().strip()
if fallback_field == FallbackFieldEnum.branch:
p = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True
)
if p.stdout:
branch_name = p.stdout.decode().strip()
# branch_name will be 'HEAD' if we are in 'detached HEAD' state
return branch_name if branch_name != "HEAD" else None
if fallback_field == FallbackFieldEnum.slug:
# if there are multiple remotes, we will prioritize using the one called 'origin' if it exists, else we will use the first one in 'git remote' list
p = subprocess.run(["git", "remote"], capture_output=True)
if not p.stdout:
return None
remotes = p.stdout.decode().strip().splitlines()
remote_name = "origin" if "origin" in remotes else remotes[0]
p = subprocess.run(
["git", "ls-remote", "--get-url", remote_name], capture_output=True
)
if not p.stdout:
return None
remote_url = p.stdout.decode().strip()
return parse_slug(remote_url)
if fallback_field == FallbackFieldEnum.git_service:
# if there are multiple remotes, we will prioritize using the one called 'origin' if it exists, else we will use the first one in 'git remote' list
p = subprocess.run(["git", "remote"], capture_output=True)
if not p.stdout:
return None
remotes = p.stdout.decode().strip().splitlines()
remote_name = "origin" if "origin" in remotes else remotes[0]
p = subprocess.run(
["git", "ls-remote", "--get-url", remote_name], capture_output=True
)
if not p.stdout:
return None
remote_url = p.stdout.decode().strip()
return parse_git_service(remote_url)
return None
def get_network_root(self):
p = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True)
if p.stdout:
return Path(p.stdout.decode().rstrip())
return None
def list_relevant_files(
self, directory: t.Optional[Path] = None, recurse_submodules: bool = False
) -> t.List[str]:
dir_to_use = directory or self.get_network_root()
if dir_to_use is None:
raise ValueError("Can't determine root folder")
cmd = ["git", "-C", str(dir_to_use), "ls-files", "-z"]
if recurse_submodules:
cmd.append("--recurse-submodules")
res = subprocess.run(cmd, capture_output=True)
return res.stdout.decode().split("\0")
class NoVersioningSystem(VersioningSystemInterface):
@classmethod
def is_available(cls):
return True
def get_network_root(self):
return Path.cwd()
def get_fallback_value(self, fallback_field: FallbackFieldEnum):
return None
def list_relevant_files(
self, directory: t.Optional[Path] = None, recurse_submodules: bool = False
) -> t.List[str]:
dir_to_use = directory or self.get_network_root()
if dir_to_use is None:
raise ValueError("Can't determine root folder")
files = search_files(
dir_to_use, folders_to_ignore=[], filename_include_regex=re.compile("")
)
return [f.relative_to(dir_to_use).as_posix() for f in files]