forked from rapidsai/cuvs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitutils.py
More file actions
286 lines (231 loc) · 9.08 KB
/
gitutils.py
File metadata and controls
286 lines (231 loc) · 9.08 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright (c) 2020-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import subprocess
import os
import re
def isFileEmpty(f):
return os.stat(f).st_size == 0
def __git(*opts):
"""Runs a git command and returns its output"""
cmd = "git " + " ".join(list(opts))
ret = subprocess.check_output(cmd, shell=True)
return ret.decode("UTF-8").rstrip("\n")
def __gitdiff(*opts):
"""Runs a git diff command with no pager set"""
return __git("--no-pager", "diff", *opts)
def branch():
"""Returns the name of the current branch"""
name = __git("rev-parse", "--abbrev-ref", "HEAD")
name = name.rstrip()
return name
def repo_version():
"""
Determines the version of the repo by using `git describe`
Returns
-------
str
The full version of the repo in the format 'v#.#.#{a|b|rc}'
"""
return __git("describe", "--tags", "--abbrev=0")
def repo_version_major_minor():
"""
Determines the version of the repo using `git describe` and returns only
the major and minor portion
Returns
-------
str
The partial version of the repo in the format '{major}.{minor}'
"""
full_repo_version = repo_version()
match = re.match(r"^v?(?P<major>[0-9]+)(?:\.(?P<minor>[0-9]+))?",
full_repo_version)
if (match is None):
print(" [DEBUG] Could not determine repo major minor version. "
f"Full repo version: {full_repo_version}.")
return None
out_version = match.group("major")
if (match.group("minor")):
out_version += "." + match.group("minor")
return out_version
def determine_merge_commit(current_branch="HEAD"):
"""
When running outside of CI, this will estimate the target merge commit hash
of `current_branch` by finding a common ancestor with the remote branch
'branch-{major}.{minor}' where {major} and {minor} are determined from the
repo version.
Parameters
----------
current_branch : str, optional
Which branch to consider as the current branch, by default "HEAD"
Returns
-------
str
The common commit hash ID
"""
try:
# Try to determine the target branch from the most recent tag
head_branch = __git("describe",
"--all",
"--tags",
"--match='branch-*'",
"--abbrev=0")
except subprocess.CalledProcessError:
print(" [DEBUG] Could not determine target branch from most recent "
"tag. Falling back to 'branch-{major}.{minor}.")
head_branch = None
if (head_branch is not None):
# Convert from head to branch name
head_branch = __git("name-rev", "--name-only", head_branch)
else:
# Try and guess the target branch as "branch-<major>.<minor>"
version = repo_version_major_minor()
if (version is None):
return None
head_branch = "branch-{}".format(version)
try:
# Now get the remote tracking branch
remote_branch = __git("rev-parse",
"--abbrev-ref",
"--symbolic-full-name",
head_branch + "@{upstream}")
except subprocess.CalledProcessError:
print(" [DEBUG] Could not remote tracking reference for "
f"branch {head_branch}.")
remote_branch = None
if (remote_branch is None):
return None
print(f" [DEBUG] Determined TARGET_BRANCH as: '{remote_branch}'. "
"Finding common ancestor.")
common_commit = __git("merge-base", remote_branch, current_branch)
return common_commit
def uncommittedFiles():
"""
Returns a list of all changed files that are not yet committed. This
means both untracked/unstaged as well as uncommitted files too.
"""
files = __git("status", "-u", "-s")
ret = []
for f in files.splitlines():
f = f.strip(" ")
f = re.sub(r"\s+", " ", f) # noqa: W605
tmp = f.split(" ", 1)
# only consider staged files or uncommitted files
# in other words, ignore untracked files
if tmp[0] == "M" or tmp[0] == "A":
ret.append(tmp[1])
return ret
def changedFilesBetween(baseName, branchName, commitHash):
"""
Returns a list of files changed between branches baseName and latest commit
of branchName.
"""
current = branch()
# checkout "base" branch
__git("checkout", "--force", baseName)
# checkout branch for comparing
__git("checkout", "--force", branchName)
# checkout latest commit from branch
__git("checkout", "-fq", commitHash)
files = __gitdiff("--name-only",
"--ignore-submodules",
f"{baseName}..{branchName}")
# restore the original branch
__git("checkout", "--force", current)
return files.splitlines()
def changesInFileBetween(file, b1, b2, filter=None):
"""Filters the changed lines to a file between the branches b1 and b2"""
current = branch()
__git("checkout", "--quiet", b1)
__git("checkout", "--quiet", b2)
diffs = __gitdiff("--ignore-submodules",
"-w",
"--minimal",
"-U0",
"%s...%s" % (b1, b2),
"--",
file)
__git("checkout", "--quiet", current)
lines = []
for line in diffs.splitlines():
if filter is None or filter(line):
lines.append(line)
return lines
def modifiedFiles(pathFilter=None):
"""
If inside a CI-env (ie. TARGET_BRANCH and COMMIT_HASH are defined, and
current branch is "current-pr-branch"), then lists out all files modified
between these 2 branches. Locally, TARGET_BRANCH will try to be determined
from the current repo version and finding a corresponding branch named
'branch-{major}.{minor}'. If this fails, this function will list out all
the uncommitted files in the current branch.
Such utility function is helpful while putting checker scripts as part of
cmake, as well as CI process. This way, during development, only the files
touched (but not yet committed) by devs can be checked. But, during the CI
process ALL files modified by the dev, as submiited in the PR, will be
checked. This happens, all the while using the same script.
"""
targetBranch = os.environ.get("TARGET_BRANCH")
commitHash = os.environ.get("COMMIT_HASH")
currentBranch = branch()
print(
f" [DEBUG] TARGET_BRANCH={targetBranch}, COMMIT_HASH={commitHash}, "
f"currentBranch={currentBranch}")
if targetBranch and commitHash and (currentBranch == "current-pr-branch"):
print(" [DEBUG] Assuming a CI environment.")
allFiles = changedFilesBetween(targetBranch, currentBranch, commitHash)
else:
print(" [DEBUG] Did not detect CI environment. "
"Determining TARGET_BRANCH locally.")
common_commit = determine_merge_commit(currentBranch)
if (common_commit is not None):
# Now get the diff. Use --staged to get both diff between
# common_commit..HEAD and any locally staged files
allFiles = __gitdiff("--name-only",
"--ignore-submodules",
"--staged",
f"{common_commit}").splitlines()
else:
# Fallback to just uncommitted files
allFiles = uncommittedFiles()
files = []
for f in allFiles:
if pathFilter is None or pathFilter(f):
files.append(f)
filesToCheckString = "\n\t".join(files) if files else "<None>"
print(f" [DEBUG] Found files to check:\n\t{filesToCheckString}\n")
return files
def listAllFilesInDir(folder):
"""Utility function to list all files/subdirs in the input folder"""
allFiles = []
for root, dirs, files in os.walk(folder):
for name in files:
allFiles.append(os.path.join(root, name))
return allFiles
def listFilesToCheck(filesDirs, filter=None):
"""
Utility function to filter the input list of files/dirs based on the input
filter method and returns all the files that need to be checked
"""
allFiles = []
for f in filesDirs:
if os.path.isfile(f):
if filter is None or filter(f):
allFiles.append(f)
elif os.path.isdir(f):
files = listAllFilesInDir(f)
for f_ in files:
if filter is None or filter(f_):
allFiles.append(f_)
return allFiles