-
Notifications
You must be signed in to change notification settings - Fork 301
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
gitCheckout() validates branch, plus unit tests #1315
Conversation
if !gitCheckRefFormat(reference) { | ||
return fmt.Errorf("%q is not a valid git ref format", reference) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be the only functional change, the rest is testability refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems legit 👍
bootstrap/git.go
Outdated
return false | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a convoluted way to do strings.HasPrefix(ref, "-")
? Or ref[0] == "-"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah… I went through various implementations as I got lured by unit testing into implementing more and more of the spec. I was hoping to implement the whole function as a single pass over ref
, rather than lots of strings.Whatever
calls that each process the whole string. Especially as I was thinking there would be various disallowed prefix/suffix characters… turns out at this point there's only one of each. I should probably simplify it back down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should probably just rewrite the whole thing as one regexp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
When constructing the
git checkout
command, the sometimes-user-specified branch name should never be considered flags/options bygit
. It's not possible to terminate arg parsing with--
because that has special meaning togit checkout
— it indicates the following names are files, not refs. So, the ref must be validated. There's agit check-ref-format
subcommand which roughly does that. Rather than shelling out to git, this patch introduces a simplegitCheckRefFormat(ref)
implementation to verify the ref doesn't start with a hyphen. This could be replaced with shell-out or a more thorough implementation if that feels worth doing.With branch set to
--foo
…git checkout -f --foo
→error: unknown option 'foo'
.git checkout -f -- --foo
→error: pathspec '--foo' did not match any file(s) known to git
Related: #1314
This pull request also introduces unit tests (with a
mockShellRunner
) forgitCheckout()
,gitClone()
,gitClean()
,gitCleanSubmodules()
andgitFetch()
.