Commit b870a4cf authored by Michal Berger's avatar Michal Berger Committed by Tomasz Zawadzki
Browse files

scripts/check_format: Apply shfmt patch and diff it with git



git diff yields more concise summary which helps while traversing
potential changes across multiple, bigger files.

Change-Id: I435ecaa60e086c16b8b70b936ba41917ccc8b71e
Signed-off-by: default avatarMichal Berger <michal.berger@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/18863


Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
parent 8cfa8ac4
Loading
Loading
Loading
Loading
+30 −11
Original line number Diff line number Diff line
@@ -497,24 +497,33 @@ function check_bash_style() {
			if ! SHFMT_NO_EDITORCONFIG=true "$shfmt" "${shfmt_cmdline[@]}" "${sh_files[@]}" > "$diff"; then
				# In case shfmt detects an actual syntax error it will write out a proper message on
				# its stderr, hence the diff file should remain empty.
				rc=1
				if [[ -s $diff ]]; then
					diff_out=$(< "$diff")
				fi
					if patch --merge -p0 < "$diff"; then
						diff_out=$(git diff)

						if [[ -n $diff_out ]]; then
							cat <<- ERROR_SHFMT

								* Errors in style formatting have been detected.
					${diff_out:+* Please, review the generated patch at $diff
								  Please, review the generated patch at $diff

								# _START_OF_THE_DIFF

					${diff_out:-ERROR}
								$diff_out

								# _END_OF_THE_DIFF
					}

							ERROR_SHFMT
				rc=1
						else
							# Empty diff? This likely means that we reverted to a clean state
							printf '* Patch reverted, please review your changes and %s\n' "$diff"
						fi
					else
						printf '* Failed to apply %s\n' "$diff"

					fi
				fi
			else
				rm -f "$diff"
				printf ' OK\n'
@@ -524,6 +533,16 @@ function check_bash_style() {
		echo "Supported version of shfmt not detected, Bash style formatting check is skipped"
	fi

	# Cleanup potential .orig files that shfmt creates
	local orig_f

	mapfile -t orig_f < <(git diff --name-only)
	orig_f=("${orig_f[@]/%/.orig}")

	if ((${#orig_f[@]} > 0)); then
		git clean -f "${orig_f[@]}"
	fi

	return $rc
}