Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Conversation

@vdye
Copy link
Collaborator

@vdye vdye commented Mar 20, 2023

This pull request does two main things:

  1. Extend the existing positional args in argparse with a distinction between "required" and "optional". If a positional arg is required but not specified, return a usage error; if a positional arg is optional, do not assign it a value. (commits 1-3)
  2. Make route an optional positional arg in git-bundle-server init; if it is unspecified/empty, try to derive it from the given URL. (commit 4)

Part of #35

vdye added 3 commits March 20, 2023 08:54
If a positional (string) arg is missing, the 'Arg()' function will return an
empty string without indicating that the value is missing. Since (for the
moment) positional args are always required, add an explicit check that the
arg is present and, if not, exit with usage.

Signed-off-by: Victoria Dye <[email protected]>
Add 'optional' property to 'argparse.positionalArg'. This leads to the
following behavior if an arg isn't specified on the command line:

- If 'required' is true, exit with the appropriate usage message.
- If 'required' is false, stop processing positional arguments. The arg
  values are never set, leaving them with the default values with which they
  were originally specified.

For now, mark all existing positional arguments as required to avoid changes
to behavior.

Signed-off-by: Victoria Dye <[email protected]>
Add a printout of positional args (name, description, '(optional)' or not)
in the 'Usage()' string.

Signed-off-by: Victoria Dye <[email protected]>
@vdye vdye added this to the v1.0 milestone Mar 20, 2023
@vdye vdye requested a review from derrickstolee as a code owner March 20, 2023 17:30
@vdye vdye self-assigned this Mar 20, 2023
@vdye vdye requested a review from ldennington as a code owner March 20, 2023 17:30
Copy link
Collaborator

@derrickstolee derrickstolee left a comment

Choose a reason for hiding this comment

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

I'm incapable of parsing regexes. Much like Perl, it's a "write only" language (understandable only by the author). This is somewhat of a personal failure, but it just means I rely on tests instead of my own understanding.

I have some suggestions for unit tests on the regex that will ease my concerns.


// Set route value, if needed
if *route == "" {
urlMatcher := regexp.MustCompile(`^.*(?:/|:)([\w\.-]+)/([\w\.-]+).git$`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting, so you require the .git naming convention? It's not necessary, and I usually omit it when I type Git URLs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is also a great candidate for a unit test, because regexes are hard to understand and are poorly tested.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Example cases to check (consider adding .git as a second case for each):

  • SSH URLs. [email protected]:git/git
  • URLs without both org/repo. https://github.com/git
  • URLs with more than org/repo. https://dev.azure.com/gvfs/ci/_git/ForTests
  • What about file://? file://my/local/path/is/strange

(I can see some of these being worthy to consider in the future, such as ADO URLs, but having the tests now will help make it explicit that it's not the design of the parser at the moment.)

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 split out the matching routine into its own function, and created different regexes for SSH, HTTP, and filesystem routes. I know more regexes probably isn't what you want 😁 but hopefully the added unit tests (and slightly better commenting) will help clarify the expected behavior to readers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The only regex I trust is a well-tested one. Thanks for beefing them up.

Make the '<route>' positional arg optional in 'git-bundle-server init'. If
it is not specified (or is an empty string), extract a route from the
supplied clone URI.

Called by 'init', 'core.GetRouteFromUrl()' attempts to match the URL against
SSH, HTTP(S), and filesystem ('file://') URLs. If the URL cannot be matched,
'init' will exit with an error and usage printout.

Also include unit tests of the route matching function to demonstrate its
behavior.

Signed-off-by: Victoria Dye <[email protected]>
@vdye vdye force-pushed the vdye/derived-route branch from e4b7bf2 to 088f4ac Compare March 20, 2023 19:56
Copy link
Collaborator

@derrickstolee derrickstolee left a comment

Choose a reason for hiding this comment

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

Excellent tests, on both sides of the pattern match.

false,
},
{
"HTTPS://SCREAM/INTOTHEVOID",
Copy link
Collaborator

Choose a reason for hiding this comment

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

HAHAHAAHAHAHA

@vdye vdye merged commit ae0f7b4 into main Mar 20, 2023
@vdye vdye deleted the vdye/derived-route branch March 20, 2023 20:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants