From de6e22f909946f7d17047f4aeab41e581546b674 Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Fri, 8 Apr 2022 07:44:07 -0700 Subject: [PATCH] fix: remove comments from whole file instead of line by line for performance (#1198) --- .tool-versions | 2 +- lib/utils.bash | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.tool-versions b/.tool-versions index a7615d27b..ff98df585 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,3 +1,3 @@ -bats v1.3.0 +bats 1.3.0 shellcheck 0.7.2 shfmt 3.3.0 diff --git a/lib/utils.bash b/lib/utils.bash index df0c9430d..84f2a2c44 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -637,16 +637,14 @@ shim_plugins() { strip_tool_version_comments() { local tool_version_path="$1" - - while IFS= read -r tool_line || [ -n "$tool_line" ]; do - # Remove whitespace before pound sign, the pound sign, and everything after it - new_line="$(cut -f1 -d"#" <<<"$tool_line" | sed -e 's/[[:space:]]*$//')" - - # Only print the line if it is not empty - if [[ -n "$new_line" ]]; then - printf "%s\\n" "$new_line" - fi - done <"$tool_version_path" + # Use sed to strip comments from the tool version file + # Breakdown of sed command: + # This command represents 3 steps, seperated by a semi-colon (;), that run on each line. + # 1. Delete line if it starts with any blankspace and a #. + # 2. Find a # and delete it and everything after the #. + # 3. Remove any whitespace from the end of the line. + # Finally, the command will print the lines that are not empty. + sed '/^[[:blank:]]*#/d;s/#.*//;s/[[:blank:]]*$//' "$tool_version_path" } asdf_run_hook() {