Skip to content

Commit 82a3692

Browse files
committed
shellcheck: Do not call git apply if there is no diff output
`git apply` fails if there isn't a valid patch on stdin, empty stdin is not a valid patch. Git errors out with `error: unrecognized input` message. This will only call git apply if stdin is *not* empty so we can avoid this error scenario. Signed-off-by: Manuel Mendez <[email protected]>
1 parent b9dbb2a commit 82a3692

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ lint: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/l
3131
.PHONY: fix
3232
fix: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
3333
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run --fix
34-
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh") -f diff | git apply -p2 -
34+
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
3535

3636
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck:
3737
mkdir -p out/linters

lint-install.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,12 @@ func shellLintCmd(_ string, level string, fix bool) string {
266266
suffix := ""
267267

268268
if fix {
269-
// patch(1) doesn't support patching from stdin on all platforms, so we use git apply instead
270-
suffix = " -f diff | git apply -p2 -"
269+
// Use git apply instead of patch(1) because it doesn't support patching from stdin on all platforms.
270+
// Everything after the first | is so we don't call git apply if there's nothing to apply, which would cause git apply to return an error.
271+
// This is the cross platform way to do what gnu xargs does with its --no-run-if-empty switch.
272+
// read -t 1 will read the first line from stdin but timeout after 1 second if empty
273+
// if its not empty it will run the rest of the line after && which echoes the line and uses cat to pipe the rest to git
274+
suffix = ` -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }`
271275
} else if level == "warn" {
272276
suffix = " || true"
273277
}

0 commit comments

Comments
 (0)