Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for executable bits that disagree with shebangs #1708

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ jobs:
- name: gix-pack with all features (including wasm)
run: cd gix-pack && cargo build --all-features --target "$TARGET"

check-mode:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Find scripts with mode/shebang mismatch
run: etc/check-mode.sh

check-packetline:
strategy:
fail-fast: false
Expand Down Expand Up @@ -441,6 +449,7 @@ jobs:
- test-32bit-cross
- lint
- cargo-deny
- check-mode
- check-packetline
- check-blocking

Expand Down
40 changes: 40 additions & 0 deletions etc/check-mode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

set -eu -o pipefail

# Go to the worktree's root. (Even if the dir name ends in a newline.)
root_padded="$(git rev-parse --show-toplevel && echo -n .)"
root="${root_padded%$'\n.'}"
cd -- "$root"

symbolic_shebang="$(printf '#!' | od -An -ta)"
status=0

function check () {
local mode="$1" oid="$2" path="$3" symbolic_magic

# Extract the first two bytes (or less if shorter) and put in symbolic form.
symbolic_magic="$(git cat-file blob "$oid" | od -N2 -An -ta)"

# Check for inconsistency between the mode and whether `#!` is present.
if [ "$mode" = 100644 ] && [ "$symbolic_magic" = "$symbolic_shebang" ]; then
printf 'mode -x but has shebang: %q\n' "$path"
elif [ "$mode" = 100755 ] && [ "$symbolic_magic" != "$symbolic_shebang" ]; then
printf 'mode +x but no shebang: %q\n' "$path"
else
return 0
fi

status=1
}

# Check regular files named with a `.sh` suffix.
while read -rd '' mode oid _stage_number path; do
case "$mode" in
100644 | 100755)
check "$mode" "$oid" "$path"
;;
esac
done < <(git ls-files -sz -- '*.sh')

exit "$status"
Empty file modified gix-diff/tests/fixtures/make_diff_for_rewrites_repo.sh
100644 → 100755
Empty file.
Empty file modified gix-merge/tests/fixtures/make_blob_repo.sh
100644 → 100755
Empty file.
Empty file modified gix-merge/tests/fixtures/text-baseline.sh
100644 → 100755
Empty file.
Empty file modified gix-merge/tests/fixtures/tree-baseline.sh
100644 → 100755
Empty file.
Empty file modified gix-revision/tests/fixtures/merge_base_octopus_repos.sh
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ fmt:
find-yanked:
cargo install --debug --locked --no-default-features --features max-pure --path .

# Find shell scripts whose +x/-x bits and magic bytes (e.g. `#!`) disagree
check-mode:
./etc/check-mode.sh

# Delete gix-packetline-blocking/src and regenerate from gix-packetline/src
copy-packetline:
./etc/copy-packetline.sh
Loading