spacedentist / spr

Submit pull requests for individual, amendable, rebaseable commits to GitHub
https://getcord.github.io/spr/
MIT License
376 stars 33 forks source link

Add refname rule checking to branch prefix input dialogue #153

Closed cadolphs closed 1 year ago

cadolphs commented 1 year ago

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.