ci: check-shebangs-and-filemodesワークフローを追加#1080
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a GitHub Actions workflow to enforce consistency between shebang usage and file executability across the repository.
- Introduces a new workflow to check file permissions for files with shebangs and shell scripts.
- Checks that shebanged files are executable, shell scripts are executable, and executables have a shebang.
| first_line=$(head -n 1 "$filename") | ||
| shebang_line=$(grep '^#!/' <<< "$first_line" || true) | ||
|
|
||
| has-shebang() { |
There was a problem hiding this comment.
Function names in bash should not contain hyphens as they can cause syntax errors; please rename 'has-shebang' to 'has_shebang'.
| has-shebang() { | |
| has_shebang() { |
| [ -n "$shebang_line" ] | ||
| } | ||
|
|
||
| is-shellscript() { |
There was a problem hiding this comment.
Function names in bash should not contain hyphens; please rename 'is-shellscript' to 'is_shellscript'.
| is-shellscript() { | |
| is_shellscript() { |
| is-executable() { | ||
| [ "$filemode" = 100755 ] | ||
| } | ||
|
|
||
| echo -n "$filename ($filemode): " | ||
|
|
||
| if has-shebang && ! is-executable; then | ||
| # shellcheck disable=SC2016 | ||
| echo 'A shebanged file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | ||
| exit 1 | ||
| fi | ||
|
|
||
| if is-shellscript && ! is-executable; then | ||
| # shellcheck disable=SC2016 | ||
| echo 'A shell script file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | ||
| exit 1 | ||
| fi | ||
|
|
||
| if is-executable && ! has-shebang; then |
There was a problem hiding this comment.
Function names in bash should not contain hyphens; please rename 'is-executable' to 'is_executable'.
| is-executable() { | |
| [ "$filemode" = 100755 ] | |
| } | |
| echo -n "$filename ($filemode): " | |
| if has-shebang && ! is-executable; then | |
| # shellcheck disable=SC2016 | |
| echo 'A shebanged file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | |
| exit 1 | |
| fi | |
| if is-shellscript && ! is-executable; then | |
| # shellcheck disable=SC2016 | |
| echo 'A shell script file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | |
| exit 1 | |
| fi | |
| if is-executable && ! has-shebang; then | |
| is_executable() { | |
| [ "$filemode" = 100755 ] | |
| } | |
| echo -n "$filename ($filemode): " | |
| if has-shebang && ! is_executable; then | |
| # shellcheck disable=SC2016 | |
| echo 'A shebanged file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | |
| exit 1 | |
| fi | |
| if is-shellscript && ! is_executable; then | |
| # shellcheck disable=SC2016 | |
| echo 'A shell script file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | |
| exit 1 | |
| fi | |
| if is_executable && ! has-shebang; then |
Hiroshiba
left a comment
There was a problem hiding this comment.
LGTM!!
関数をケバブケースかスネークケースにするかですが、個人的には統一されていればどちらでもいいと思います!
たぶんbashに慣れていない人は混乱すると思うので、倒すとしたらスネークケースの方が良さそうではある。
ちなみにPOSIX標準?だと-は関数名に認められてない・・・ってChatGPT君が言ってました!!
https://chatgpt.com/share/6817815e-e204-8008-a0fa-bfa424397c2f
| - name: Check shebangs and filemodes | ||
| run: | | ||
| blobs=$(git ls-files -s | grep '^100') | ||
|
|
||
| while read -r blob; do | ||
| filemode=$(awk '{ print $1 }' <<< "$blob") | ||
| filename=$(awk '{ print $4 }' <<< "$blob") | ||
|
|
||
| first_line=$(head -n 1 "$filename") | ||
| shebang_line=$(grep '^#!/' <<< "$first_line" || true) | ||
|
|
||
| has-shebang() { | ||
| [ -n "$shebang_line" ] | ||
| } | ||
|
|
||
| is-shellscript() { | ||
| [[ "$filename" =~ \.(ba)?sh$ ]] | ||
| } | ||
|
|
||
| is-executable() { | ||
| [ "$filemode" = 100755 ] | ||
| } | ||
|
|
||
| echo -n "$filename ($filemode): " | ||
|
|
||
| if has-shebang && ! is-executable; then | ||
| # shellcheck disable=SC2016 | ||
| echo 'A shebanged file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | ||
| exit 1 | ||
| fi | ||
|
|
||
| if is-shellscript && ! is-executable; then | ||
| # shellcheck disable=SC2016 | ||
| echo 'A shell script file must be executable (note: if you are using Windows, please change the filemode with `git update-index --chmod+x`)' | ||
| exit 1 | ||
| fi | ||
|
|
||
| if is-executable && ! has-shebang; then | ||
| echo 'An executable blob must have a shebang' | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo OK | ||
| done <<< "$blobs" |
There was a problem hiding this comment.
(ただのコメントです)
build_utilディレクトリにシェルスクリプト置いて、それをここから叩くようにしても良いかもですね!
ネストが減って読みやすい・ちゃんとシンタックスハイライトがつく・ローカルからも実行デバッグできる利点がありそう。
その代わりファイル数が増えちゃいそう。
たまに.github/workflowsディレクトリ内にbash置いて叩いてるプロジェクトもちらほら見かけます。
そういうのもありかも(と思いつつちゃんと考えれてませんが)。
|
|
内容
#1077 の続き。
このリポジトリ内のすべてのファイルに対し、以下のチェックを義務付ける。
関連 Issue
その他