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

helpers.sh: Robustify functions and don't exit 1 #985

Closed
wants to merge 1 commit into from

Conversation

ermo
Copy link
Contributor

@ermo ermo commented Dec 6, 2023

Summary
Shellcheck recklessly introduced a bug that will make functions exit 1 which causes the shell to exit, if the function in question doesn not complete successfully.

This commit fixes this "small" oversight.

In addition, this commit also makes the gotopkg and goroot functions more robust and yields hopefully useful error messages as necessary.

Test Plan

ermo@solbox:~
$ goroot
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
If you're not somewhere in the solus packages git dir, use gotosoluspkgs instead.
ermo@solbox:~/
$ gotosoluspkgs
ermo@solbox:~/repos/getsolus/solpkgs
$ cd
ermo@solbox:~
$ gotopkg
No package specified, going to the root of the solus packages git dir instead.
ermo@solbox:~/repos/getsolus/solpkgs
$ cd
ermo@solbox:~
$ gotopkg grub2
ermo@solbox:~/repos/getsolus/solpkgs/packages/g/grub2
$

@ermo ermo requested review from silkeh and sheepman4267 December 6, 2023 23:04
@ermo ermo added Bug Something isn't working Topic: Plumbing Core components Confirmed Issue can be reproduced labels Dec 6, 2023
silkeh
silkeh previously approved these changes Dec 6, 2023
Copy link
Member

@silkeh silkeh left a comment

Choose a reason for hiding this comment

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

One minor question that you're free to ignore, but LGTM!

common/Scripts/helpers.sh Show resolved Hide resolved
@ReillyBrogan
Copy link
Contributor

boring

I say let the users shell die that's fun!

**Summary**
Shellcheck recklessly introduced a bug that will make functions `exit 1`
which causes the shell to exit, if the function in question doesn not
complete successfully.

This commit fixes this "small" oversight.

In addition, this commit also makes the `gotopkg` and `goroot` functions
more robust and yields hopefully useful error messages as necessary.

**Test Plan**
```
ermo@solbox:~
$ goroot
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
If you're not somewhere in the solus packages git dir, use gotosoluspkgs instead.
ermo@solbox:~/
$ gotosoluspkgs
ermo@solbox:~/repos/getsolus/solpkgs
$ cd
ermo@solbox:~
$ gotopkg
No package specified, going to the root of the solus packages git dir instead.
ermo@solbox:~/repos/getsolus/solpkgs
$ cd
ermo@solbox:~
$ gotopkg grub2
ermo@solbox:~/repos/getsolus/solpkgs/packages/g/grub2
$
```

Signed-off-by: Rune Morling <[email protected]>
@ermo ermo force-pushed the fix-helpers.sh-script-killing-the-shell branch from c1b4509 to fc811fb Compare December 7, 2023 00:28
@ermo ermo changed the title helpers.sh: Stop killing the user's shell helpers.sh: Robustify functions and don't exit 1 Dec 7, 2023
Copy link
Member

@sheepman4267 sheepman4267 left a comment

Choose a reason for hiding this comment

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

I see a couple issues here:

  1. whatuses and whatprovides now push you to the root of the repo, even if you're somewhere inside the repo already. That feels like it would be very, very frustrating.
  2. Talking of that, do whatuses and whatprovides need to change the directory at all? I think not. Maybe we should:
    • Move these functions into a separate script, so they don't affect the user's shell at all, or
    • Store the current directory, then change back when we've finished running our code? The former makes more sense to me, but I'm not great at shell scripts.
  3. Before, all functions other than gotosoluspkgs would work just fine on a separate clone of the repo (as in, not the one which this script is sourced from). I find that useful. Maybe I'm just bad at git, but I like being able to have multiple clones, so I can (for example) have multiple branches checked out at once, and do comparisons between them manually. Having gotopkg always force me into my "main" clone of the repo would be a nuisance.

@TraceyC77
Copy link
Contributor

I agree with @sheepman4267 in his review. I would use git -C to solve point num 2, no need to store or change directories at all.

}

# What provides a lib
function whatprovides() {
gotosoluspkgs
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @sheepman4267 that forcing the shell into a certain directory when it didn't before is disruptive to the user. This is not necessary if you use git -C.

Generally, if I need code to figure something out, I try to do it in a way that doesn't affect ther user environment if I can.

@@ -1,30 +1,49 @@
#!/usr/bin/env bash

# shellcheck wants to add '|| exit 1' willy nilly, so let's
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be reworded. Any comment starting with #shellcheck is interpreted by shellcheck as a directive.

}

# Goes to the root directory of the git repository
function goroot() {
cd "$(git rev-parse --show-toplevel)" || exit 1
local pkgroot="$(git rev-parse --show-toplevel)"
Copy link
Contributor

Choose a reason for hiding this comment

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

In helpers.sh line 21:
    local pkgroot="$(git rev-parse --show-toplevel)"
          ^-----^ SC2155 (warning): Declare and assign separately to avoid masking return values.

https://www.shellcheck.net/wiki/SC2155

@ermo
Copy link
Contributor Author

ermo commented Dec 7, 2023

Sounds like a (way) smarter/better approach is needed here, so closing.

The good thing is that the shell will still exit 1, so will make interested parties more likely to come up with a set of fixes with nice properties.

Best of luck.

@ermo ermo closed this Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Confirmed Issue can be reproduced Topic: Plumbing Core components
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants