Skip to content

Commit 068c87e

Browse files
committed
try PurePath.relative_to to exclude files outside the repository
1 parent 4e9a355 commit 068c87e

File tree

1 file changed

+6
-2
lines changed
  • onlinejudge_verify/languages

1 file changed

+6
-2
lines changed

onlinejudge_verify/languages/rust.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,13 @@ def _related_source_files(basedir: pathlib.Path, metadata: Dict[str, Any]) -> Di
187187
s += ' '
188188
s += next(it)
189189
path = pathlib.Path(metadata['workspace_root'], s)
190-
# Ignores paths like `/dev/null` or `/usr/share/foo/bar` (if any).
191-
if any(path.parts[:i + 1] == basedir.parts for i, _ in enumerate(path.parts)):
190+
# Ignores paths that don't start with the `basedir`. (e.g. `/dev/null`, `/usr/local/share/foo/bar`)
191+
try:
192+
# `PurePath.is_relative_to` is since Python 3.9.
193+
_ = path.relative_to(basedir)
192194
paths.append(path)
195+
except ValueError:
196+
pass
193197
if paths[:1] == [pathlib.Path(target['src_path'])]:
194198
ret[paths[0]] = frozenset(paths[1:])
195199
break

0 commit comments

Comments
 (0)