Skip to content

Commit 22cd7fd

Browse files
committed
tools/codeformat: Fix compatibility running from subfolder in repo.
1 parent ab1e623 commit 22cd7fd

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

tools/codeformat.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,23 @@ def cmd_result_set(cmd):
116116
return set()
117117
return {f.strip() for f in ret.split("\n")}
118118

119+
def rel_paths(files, root):
120+
return {os.path.relpath(os.path.join(root, f.strip()), ".") for f in files}
121+
119122
try:
120123
ret = set()
121124

125+
# get path to root of repository
126+
root_dir = (
127+
subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True)
128+
.stdout.strip()
129+
.decode()
130+
)
131+
122132
# Check locally modified files
123-
dirty = cmd_result_set(["git", "status", "--porcelain"])
124-
ret = {line.split(" ", 1)[-1] for line in dirty}
133+
status = cmd_result_set(["git", "status", "--porcelain"])
134+
dirty_files = rel_paths({line.split(" ", 1)[-1] for line in status}, root_dir)
135+
ret |= dirty_files
125136

126137
# Current commit and branch
127138
current_commit = (
@@ -144,7 +155,10 @@ def cmd_result_set(cmd):
144155
# List the files modified on current branch
145156
if verbose:
146157
print("Scanning changes from current branch and any local changes")
147-
ret |= cmd_result_set(["git", "diff", "--relative", "--name-only", BASE_BRANCH])
158+
files_on_branch = rel_paths(
159+
cmd_result_set(["git", "diff", "--name-only", BASE_BRANCH]), root_dir
160+
)
161+
ret |= files_on_branch
148162
return ret
149163
except:
150164
# Git not available, run on entire repo

0 commit comments

Comments
 (0)