Skip to content

Commit 34efe03

Browse files
authored
Merge pull request #1708 from EliahKagan/run-ci/mode
Check for executable bits that disagree with shebangs
2 parents 1d73d00 + 82cbc0a commit 34efe03

File tree

8 files changed

+53
-0
lines changed

8 files changed

+53
-0
lines changed

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ jobs:
359359
- name: gix-pack with all features (including wasm)
360360
run: cd gix-pack && cargo build --all-features --target "$TARGET"
361361

362+
check-mode:
363+
runs-on: ubuntu-latest
364+
365+
steps:
366+
- uses: actions/checkout@v4
367+
- name: Find scripts with mode/shebang mismatch
368+
run: etc/check-mode.sh
369+
362370
check-packetline:
363371
strategy:
364372
fail-fast: false
@@ -441,6 +449,7 @@ jobs:
441449
- test-32bit-cross
442450
- lint
443451
- cargo-deny
452+
- check-mode
444453
- check-packetline
445454
- check-blocking
446455

etc/check-mode.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu -o pipefail
4+
5+
# Go to the worktree's root. (Even if the dir name ends in a newline.)
6+
root_padded="$(git rev-parse --show-toplevel && echo -n .)"
7+
root="${root_padded%$'\n.'}"
8+
cd -- "$root"
9+
10+
symbolic_shebang="$(printf '#!' | od -An -ta)"
11+
status=0
12+
13+
function check () {
14+
local mode="$1" oid="$2" path="$3" symbolic_magic
15+
16+
# Extract the first two bytes (or less if shorter) and put in symbolic form.
17+
symbolic_magic="$(git cat-file blob "$oid" | od -N2 -An -ta)"
18+
19+
# Check for inconsistency between the mode and whether `#!` is present.
20+
if [ "$mode" = 100644 ] && [ "$symbolic_magic" = "$symbolic_shebang" ]; then
21+
printf 'mode -x but has shebang: %q\n' "$path"
22+
elif [ "$mode" = 100755 ] && [ "$symbolic_magic" != "$symbolic_shebang" ]; then
23+
printf 'mode +x but no shebang: %q\n' "$path"
24+
else
25+
return 0
26+
fi
27+
28+
status=1
29+
}
30+
31+
# Check regular files named with a `.sh` suffix.
32+
while read -rd '' mode oid _stage_number path; do
33+
case "$mode" in
34+
100644 | 100755)
35+
check "$mode" "$oid" "$path"
36+
;;
37+
esac
38+
done < <(git ls-files -sz -- '*.sh')
39+
40+
exit "$status"

gix-diff/tests/fixtures/make_diff_for_rewrites_repo.sh

100644100755
File mode changed.

gix-merge/tests/fixtures/make_blob_repo.sh

100644100755
File mode changed.

gix-merge/tests/fixtures/text-baseline.sh

100644100755
File mode changed.

gix-merge/tests/fixtures/tree-baseline.sh

100644100755
File mode changed.

gix-revision/tests/fixtures/merge_base_octopus_repos.sh

100644100755
File mode changed.

justfile

+4
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ fmt:
255255
find-yanked:
256256
cargo install --debug --locked --no-default-features --features max-pure --path .
257257

258+
# Find shell scripts whose +x/-x bits and magic bytes (e.g. `#!`) disagree
259+
check-mode:
260+
./etc/check-mode.sh
261+
258262
# Delete gix-packetline-blocking/src and regenerate from gix-packetline/src
259263
copy-packetline:
260264
./etc/copy-packetline.sh

0 commit comments

Comments
 (0)