Skip to content

Commit 746afa5

Browse files
committed
pre-commit: move logic for applying patch to shell script
1 parent 56a395b commit 746afa5

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

.github/workflows/pre-commit.yml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,4 @@ jobs:
1818
- uses: pre-commit/action@v3.0.1
1919
- run: |
2020
python -m pip install 'ruamel.yaml'
21-
if ! python sort_regressionfiles_yaml.py > regressionfiles.diff; then
22-
echo "difference found:"
23-
cat regressionfiles.diff
24-
if ! patch -p1 -i regressionfiles.diff regressionfiles.yaml; then
25-
echo "patch didn't apply cleanly"
26-
else
27-
echo "patch applied cleanly"
28-
fi
29-
exit 1
30-
fi
21+
./sort_regressionfiles_yaml.sh

sort_regressionfiles_yaml.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/usr/bin/env python
22

3+
"""sort_regressionfiles_yaml.py: Check and see if the entries in
4+
regressionfiles.yaml are sorted by filename, producing the diff to standard
5+
out that should make them sorted.
6+
7+
You should probably call the shell script, which wraps this Python code.
8+
"""
9+
310

411
if __name__ == "__main__":
512
import sys
@@ -16,6 +23,10 @@
1623

1724
contents_sorted = {"regressions": sorted(contents["regressions"], key=lambda x: x["loc_entry"])}
1825

26+
# Since the original regressionfiles.yaml has comments, we need to strip
27+
# those out before generating the diff, since we can't yet generated
28+
# sorted output that contains comments.
29+
1930
with open("regressionfiles_nocomments.yaml", "w", encoding="utf-8") as handle:
2031
yaml.dump(contents, handle)
2132

@@ -24,9 +35,8 @@
2435

2536
result = sp.run(
2637
[
27-
"git",
2838
"diff",
29-
"--no-index",
39+
"-u",
3040
"regressionfiles_nocomments.yaml",
3141
"regressionfiles_sorted.yaml",
3242
]

sort_regressionfiles_yaml.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# sort_regressionfiles_yaml.sh: Check and see if the entries in
6+
# regressionfiles.yaml are sorted by filename, producing a diff file that
7+
# should make them sorted. Then, attempt to apply the diff as a patch that
8+
# would make regressionfiles.yaml sorted, which may fail depending on any
9+
# comments present in the original file.
10+
#
11+
# You should probably call this shell script and not the Python code.
12+
13+
if ! python sort_regressionfiles_yaml.py > regressionfiles.diff; then
14+
echo "difference found:"
15+
cat regressionfiles.diff
16+
if ! patch -p1 -i regressionfiles.diff regressionfiles.yaml; then
17+
echo "patch didn't apply cleanly"
18+
else
19+
echo "patch applied cleanly"
20+
fi
21+
exit 1
22+
fi

0 commit comments

Comments
 (0)