Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parameter substitution instead of dirname/basename, where safe to do so #1926

Merged
merged 2 commits into from
Sep 18, 2021

Conversation

gaelicWizard
Copy link
Contributor

@gaelicWizard gaelicWizard commented Sep 7, 2021

Description

Reduce subshells and external commands by using Bash's string processing features instead of dirname and basename. This basically uses $PWD, $BASH_SOURCE, and "${pathname%*/}" and similar constructs to avoid subshells for $(pwd), dirname, and basename.

Motivation and Context

This is part of a set of patches to reduce usage of subshells and external binaries in Bash It.

The two commits here were part of #1914, but that PR went further than this one and some edge cases were identified which could lead to unintended behavior so those uses were left as-is. Both commits here operate specifically on code which specifies the full path within the same function and thus won't run in to possible issues with user-supplied strings. Merging this PR resolves #1914.

The two edge-cases are:

  1. too many /, and
  2. not enough /.

Since parameter substitution is string manipulation, it lacks logic to insert ./ for an empty directory name, &c.

How Has This Been Tested?

Tested locally, reviewed in part in #1914, and all tests pass.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the code style of this project.
  • If my change requires a change to the documentation, I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • If I have added a new file, I also added it to clean_files.txt and formatted it using lint_clean_files.sh.
  • I have added tests to cover my changes, and all the new and existing tests pass.

Copy link
Member

@NoahGorny NoahGorny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @davidpfarrell can you take a look as well?

If its only when its safe, I see no problem with merging this

@cornfeedhobo
Copy link
Member

I'd like to take a day to test this, but at first glance, this looks great.

@NoahGorny
Copy link
Member

Hi @cornfeedhobo, did you have a time to test this?

@gaelicWizard
Copy link
Contributor Author

@NoahGorny, the Mac OS X test runners are so slow that $SECONDS changed during one of the tests...

@gaelicWizard gaelicWizard force-pushed the basenamed branch 2 times, most recently from 0d1a8e1 to d76398d Compare September 12, 2021 06:50
Convert `var=${dirname $filename)` to `var="${filename%/*}` in cases where there is no ambiguity.

Make sure that the path in `$BASH_IT` is absolute because this path gets embedded in the template `.bash_profile` file if selected by the user.
Convert `var=$(basename $file)` to `var="${file##*/}"`
@gaelicWizard
Copy link
Contributor Author

::: Not me checking this PR twice a day hoping @cornfeedhobo found some time to look through this, nope. :::

@cornfeedhobo
Copy link
Member

cornfeedhobo commented Sep 18, 2021

Sorry y'all. Rough week. Taking a test drive with this today. Will report back shortly.

@gaelicWizard @NoahGorny if you have any other PRs that need review and/or testing, this is a good weekend for me to power through them. Ping me in the comments. Thanks!!

Copy link
Member

@cornfeedhobo cornfeedhobo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything I tested works as expected. Great work here, and my apologies again for the delay in review!

@NoahGorny
Copy link
Member

Everything I tested works as expected. Great work here, and my apologies again for the delay in review!

No worries, thanks for the review @cornfeedhobo ❤️

@NoahGorny NoahGorny merged commit 0dbf1d5 into Bash-it:master Sep 18, 2021
@gaelicWizard gaelicWizard deleted the basenamed branch September 18, 2021 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants