From f0c4ee4047f3b70241bacab712642437e37fad2f Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Wed, 24 Sep 2025 20:47:22 +0900 Subject: [PATCH] make lint: Perform bash script linting --- .tool-versions | 3 +- Makefile | 18 ++++------- shell/completion.bash | 4 ++- shell/completion.zsh | 2 +- shell/key-bindings.bash | 4 ++- shell/key-bindings.zsh | 2 +- shell/update-common.sh | 31 ------------------- shell/update.sh | 67 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 83 insertions(+), 48 deletions(-) delete mode 100755 shell/update-common.sh create mode 100755 shell/update.sh diff --git a/.tool-versions b/.tool-versions index 224bfbf2..a5bf7abc 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,3 @@ golang 1.23 -ruby 3.4 +ruby 3.4 +shfmt 3.12 diff --git a/Makefile b/Makefile index efd1aeb6..61261cbe 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,9 @@ BASH_SCRIPTS := $(ROOT_DIR)/bin/fzf-preview.sh \ $(ROOT_DIR)/install \ $(ROOT_DIR)/uninstall \ $(ROOT_DIR)/shell/common.sh \ - $(ROOT_DIR)/shell/update-common.sh -BASH_SCRIPTS_INDENT_LEFT := $(ROOT_DIR)/shell/completion.bash \ - $(ROOT_DIR)/shell/key-bindings.bash + $(ROOT_DIR)/shell/update.sh \ + $(ROOT_DIR)/shell/completion.bash \ + $(ROOT_DIR)/shell/key-bindings.bash ifdef FZF_VERSION VERSION := $(FZF_VERSION) @@ -100,17 +100,11 @@ bench: lint: $(SOURCES) test/*.rb test/lib/*.rb ${BASH_SCRIPTS} [ -z "$$(gofmt -s -d src)" ] || (gofmt -s -d src; exit 1) bundle exec rubocop -a --require rubocop-minitest --require rubocop-performance - [ -z "$$(shfmt -d $(BASH_SCRIPTS))" ] || (echo "format bash files by (make fmt)"; exit 1) + shell/update.sh --check ${BASH_SCRIPTS} -fmt: $(SOURCES) $(BASH_SCRIPTS) $(BASH_SCRIPTS_INDENT_LEFT) +fmt: $(SOURCES) $(BASH_SCRIPTS) gofmt -s -w src - shfmt -w $(BASH_SCRIPTS) - # shift left because we have an outermost if block for historical reasons. - for f in $(BASH_SCRIPTS_INDENT_LEFT); do \ - shfmt -w "$$f"; \ - tmp=$$(mktemp); \ - sed 's/^ //' "$$f" > "$$tmp" && mv "$$tmp" "$$f"; \ - done + shell/update.sh ${BASH_SCRIPTS} install: bin/fzf diff --git a/shell/completion.bash b/shell/completion.bash index 965bdb40..e41feb9f 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -31,9 +31,10 @@ if [[ $- =~ i ]]; then ########################################################### +#----BEGIN shfmt #----BEGIN INCLUDE common.sh # NOTE: Do not directly edit this section, which is copied from "common.sh". -# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# To modify it, one can edit "common.sh" and run "./update.sh" to apply # the changes. See code comments in "common.sh" for the implementation details. __fzf_defaults() { @@ -683,5 +684,6 @@ _fzf_setup_completion() { esac done } +#----END shfmt fi diff --git a/shell/completion.zsh b/shell/completion.zsh index 1df53f55..39e72db2 100644 --- a/shell/completion.zsh +++ b/shell/completion.zsh @@ -98,7 +98,7 @@ if [[ -o interactive ]]; then #----BEGIN INCLUDE common.sh # NOTE: Do not directly edit this section, which is copied from "common.sh". -# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# To modify it, one can edit "common.sh" and run "./update.sh" to apply # the changes. See code comments in "common.sh" for the implementation details. __fzf_defaults() { diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index b83d26a0..500591bc 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -17,9 +17,10 @@ if [[ $- =~ i ]]; then # Key bindings # ------------ +#----BEGIN shfmt #----BEGIN INCLUDE common.sh # NOTE: Do not directly edit this section, which is copied from "common.sh". -# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# To modify it, one can edit "common.sh" and run "./update.sh" to apply # the changes. See code comments in "common.sh" for the implementation details. __fzf_defaults() { @@ -154,5 +155,6 @@ if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then bind -m vi-command '"\ec": "\C-z\ec\C-z"' bind -m vi-insert '"\ec": "\C-z\ec\C-z"' fi +#----END shfmt fi diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index 46b3c2b0..bc2c8968 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -40,7 +40,7 @@ if [[ -o interactive ]]; then #----BEGIN INCLUDE common.sh # NOTE: Do not directly edit this section, which is copied from "common.sh". -# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# To modify it, one can edit "common.sh" and run "./update.sh" to apply # the changes. See code comments in "common.sh" for the implementation details. __fzf_defaults() { diff --git a/shell/update-common.sh b/shell/update-common.sh deleted file mode 100755 index 55905043..00000000 --- a/shell/update-common.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh - -# This script applies the contents of "common.sh" to the other files. - -set -e - -# Go to the directory that contains this script -dir=${0%"${0##*/}"} -if [ -n "$dir" ]; then - cd "$dir" -fi - -update() { - { - sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1" - cat < "$1.part" - - mv -f "$1.part" "$1" -} - -update completion.bash -update completion.zsh -update key-bindings.bash -update key-bindings.zsh diff --git a/shell/update.sh b/shell/update.sh new file mode 100755 index 00000000..4b6f63ae --- /dev/null +++ b/shell/update.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +# This script applies the contents of "common.sh" to the other files. + +set -e + +dir=${0%"${0##*/}"} + +update() { + { + sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1" + cat < "$1.part" + + mv -f "$1.part" "$1" +} + +update "$dir/completion.bash" +update "$dir/completion.zsh" +update "$dir/key-bindings.bash" +update "$dir/key-bindings.zsh" + +# Check if --check is in ARGV +check=0 +rest=() +for arg in "$@"; do + case $arg in + --check) check=1 ;; + *) rest+=("$arg") ;; + esac +done + +fmt() { + if ! grep -q "^#----BEGIN shfmt" "$1"; then + if [[ $check == 1 ]]; then + shfmt -d "$1" + return $? + else + shfmt -w "$1" + fi + else + { + sed -n '1,/^#----BEGIN shfmt/p' "$1" | sed '$d' + sed -n '/^#----BEGIN shfmt/,/^#----END shfmt/p' "$1" | shfmt --filename "$1" + sed -n '/^#----END shfmt/,$p' "$1" | sed '1d' + } > "$1.part" + + if [[ $check == 1 ]]; then + diff -q "$1" "$1.part" + ret=$? + rm -f "$1.part" + return $ret + fi + + mv -f "$1.part" "$1" + fi +} + +for file in "${rest[@]}"; do + fmt "$file" || exit $? +done