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

fix: Improve Bash hygiene #1056

Merged
merged 4 commits into from Aug 18, 2023
Merged

Conversation

hyperupcall
Copy link
Collaborator

@hyperupcall hyperupcall commented Jul 19, 2023

Closes #1055

@@ -280,7 +280,8 @@ commitList() {
for (( i=0; i<"${_tags_list_keys_length}"; i++ )); do
local __curr_tag="${tags_list_keys[$i]}"
local __prev_tag="${tags_list_keys[$i+1]:-null}"
local __curr_date="$(_valueForKeyFakeAssocArray "${__curr_tag}" "${tags_list[*]}")"
local __curr_date=
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
local __curr_date=
local __curr_date

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suppose this is a stylistic preference (the small behavior difference isn't relevant to every Bash project), so I went ahead and removed the extra =.

@@ -216,9 +216,9 @@ print_summary_by_line() {
print_summary() {
if [ "$OUTPUT_STYLE" == "tabular" ]; then
tabular_headers="# Repo $SP Age $SP Last active $SP Active on $SP Commits $SP Uncommitted $SP Branch"
echo -e "$tabular_headers\n$project $SP $(repository_age) $SP $(last_active) $SP $(active_days $commit) days $SP $(commit_count $commit) $SP $(uncommitted_changes_count) $SP $(current_branch_name)" | column -t -s "$COLUMN_CMD_DELIMTER"
echo -e "$tabular_headers\n$project $SP $(repository_age) $SP $(last_active) $SP $(active_days "$commit") days $SP $(commit_count "$commit") $SP $(uncommitted_changes_count) $SP $(current_branch_name)" | column -t -s "$COLUMN_CMD_DELIMTER"
Copy link
Collaborator

Choose a reason for hiding this comment

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

The quote is missing intently. We have shellcheck disable=SC2086 mark in active_days and other similar functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To me it doesn't look like the shellcheck disable=SC2086 should even be in active_days. And actually, looking at all the ShellCheck disable directives, the ones that deal with $commit are incorrect. So to me it seems we are dealing with a larger issue that has been surpressed.

From the top, git-summary has a synposis of git-summary [--line] [--dedup-by-email] [<committish>], and accepts a single committish (even if it accepted multiple committishes, it wouldn't make sense anyways since the underlying git subcommands acccept only a single commitish). The $commit variable is either assigned HEAD or $* (which corresponds to <committish>. The $* looks fishy itself, but doesn't need to be changed.

Concerning the active_days function, the only argument passed to it is $commit, which is eventually passed to git log. In other functions, $commit is passed to git shortlog. All these git subcommands have the commonality of accepting a single [<revision range>].

[<revision range>] is defined in gitrevisions(7), and some valid revisions include things that contain spaces, like HEAD^{/fix nasty bug}, HEAD@{5 minutes ago}. So by not quoting $commit, it word splits on committishes that include spaces.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

However, there are still cases in which $commit may be empty and word splitting is intended - so those of course will have to be handled as well.

Edit: I have fixed that case by moving commit=HEAD up.

[ $# -ne 0 ] && commit=$*
fi
project=${PWD##*/}

#
# get date for the given <commit>
#
date() {
commit_date() {
# the $1 can be empty
# shellcheck disable=SC2086
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can remove the shellcheck now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed one extra one, but the rest cannot be removed because of MERGES_ARG.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems we can quote MERGES_ARG as we don't expect any whitespace in it.
What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately not because that would mean an empty argument will be passed to git. It's funtionally similar to doing this:

$ git log '' --oneline HEAD
fatal: ambiguous argument '': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In other words it shouldn't be quoted so the whitespace can be skipped by the shell during word splitting.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. Thanks for clarifying that.

@spacewander spacewander merged commit 47e9402 into tj:master Aug 18, 2023
4 checks passed
@hyperupcall hyperupcall deleted the hyperupcall-improve-hygiene branch August 18, 2023 08:21
vanpipy pushed a commit to vanpipy/git-extras that referenced this pull request Sep 14, 2023
* fix: Improve Bash hygiene

* Update quoting to account for new bugs

* quotes

* fix: Remove extra shellcheck comment
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.

bad substitution when trying to use git effort
2 participants