-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathversioning_systems.py
147 lines (114 loc) · 5.13 KB
/
versioning_systems.py
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
import logging
import subprocess
import typing as t
from pathlib import Path
from shutil import which
from codecov_cli.fallbacks import FallbackFieldEnum
from codecov_cli.helpers.git import parse_git_service, parse_slug
logger = logging.getLogger("codecovcli")
class VersioningSystemInterface(object):
def __repr__(self) -> str:
return str(type(self))
def get_fallback_value(self, fallback_field: FallbackFieldEnum) -> t.Optional[str]:
pass
def get_network_root(self) -> t.Optional[Path]:
pass
def list_relevant_files(self, directory: t.Optional[Path] = None) -> 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, root_folder: t.Optional[Path] = None) -> t.List[str]:
dir_to_use = root_folder or self.get_network_root()
if dir_to_use is None:
raise ValueError("Can't determine root folder")
res = subprocess.run(
["git", "-C", str(dir_to_use), "ls-files"], capture_output=True
)
return [
(
filename[1:-1]
if filename.startswith('"') and filename.endswith('"')
else filename
)
for filename in res.stdout.decode("unicode_escape").strip().split("\n")
]
class NoVersioningSystem(VersioningSystemInterface):
@classmethod
def is_available(cls):
return True
def get_network_root(self):
return Path.cwd()