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

Add refname rule checking to branch prefix input dialogue #153

Merged

Conversation

cadolphs
Copy link
Contributor

Fixes #152

This PR addresses the potential invalid refnames resulting from a bad input to the branchPrefix question during spr init. I debated between two approaches.

  1. We could just construct a refname based off the branch prefix and run a git command to check if it's a valid refname. I thought that was too hacky and I didn't want to deal with spawning an extra command during the setup.
  2. We could just implement the relevant rules directly. Of course if future git versions change those rules, we'll be out of sync with them. I consider that somewhat unlikely though.

In setting up the code for the rules, I opted for a more verbose approach. We could certainly mash together everything into one big regex with lots of branches. Due to all those escaped special chars, the regex is already ugly enough.

One caveat: Because the refname rules disallow multiple consecutive forward slashes /, our branch prefix must not start with even a single /.

Testing

Unit tests make sure the rules are actually implemented. They should be comprehensive, but they are not exhaustive. For example, there isn't a single test for each and every control character. I trust that the is_control method on char works as advertised...

I also manually tested that the dialogue repeats until an acceptable refname is added.

Copy link
Contributor

@sven-of-cord sven-of-cord left a comment

Choose a reason for hiding this comment

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

Looks good. Sorry for the late response. One question in the inline comment.


fn validate_branch_prefix(branch_prefix: &str) -> Result<()> {
// They can include slash / for hierarchical (directory) grouping, but no slash-separated component can begin with a dot . or end with the sequence .lock.
if branch_prefix.contains("/.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to check branch_prefix.starts_with("."), too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I missed the fact that due to the prefix, we get a "/" at the beginning anyway and thus it's not enough to just check for "/..

I amended the commit.

Copy link
Owner

@spacedentist spacedentist left a comment

Choose a reason for hiding this comment

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

Nice work! Thanks for contributing!

@spacedentist spacedentist merged commit 4bc5837 into spacedentist:master Apr 23, 2023
clarityflowers pushed a commit to clarityflowers/spr that referenced this pull request Mar 19, 2024
Fixes spacedentist#152

This PR addresses the potential invalid refnames resulting from a bad
input to the `branchPrefix` question during `spr init`. I debated
between two approaches.

1. We could just construct a refname based off the branch prefix and run
a git command to check if it's a valid refname. I thought that was too
hacky and I didn't want to deal with spawning an extra command during
the setup.
2. We could just implement the relevant rules directly. Of course if
future git versions change those rules, we'll be out of sync with them.
I consider that somewhat unlikely though.

In setting up the code for the rules, I opted for a more verbose
approach. We could certainly mash together everything into one big regex
with lots of branches. Due to all those escaped special chars, the regex
is already ugly enough.

One caveat: Because the refname rules disallow multiple consecutive
forward slashes `/`, our branch prefix must not start with even a single
`/`.

# Testing
Unit tests make sure the rules are actually implemented. They should be
comprehensive, but they are not exhaustive. For example, there isn't a
single test for each and every control character. I trust that the
`is_control` method on `char` works as advertised...

I also manually tested that the dialogue repeats until an acceptable
refname is added.

Reviewers: spacedentist
    
Reviewed By: spacedentist
    
Pull Request: spacedentist#153
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.

spr init should validate the provided branchPrefix
3 participants