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

gitCheckout() validates branch, plus unit tests #1315

Merged
merged 9 commits into from
Oct 6, 2020

Conversation

pda
Copy link
Member

@pda pda commented Oct 5, 2020

When constructing the git checkout command, the sometimes-user-specified branch name should never be considered flags/options by git. It's not possible to terminate arg parsing with -- because that has special meaning to git checkout — it indicates the following names are files, not refs. So, the ref must be validated. There's a git check-ref-format subcommand which roughly does that. Rather than shelling out to git, this patch introduces a simple gitCheckRefFormat(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

  • Before: git checkout -f --fooerror: unknown option 'foo'.
  • After: git checkout -f -- --fooerror: pathspec '--foo' did not match any file(s) known to git

Related: #1314


This pull request also introduces unit tests (with a mockShellRunner) for gitCheckout(), gitClone(), gitClean(), gitCleanSubmodules() and gitFetch().

$ go test ./bootstrap -run TestGit -v
=== RUN   TestGitCheckRefFormat
=== RUN   TestGitCheckRefFormat/hello
=== RUN   TestGitCheckRefFormat/--world
--- PASS: TestGitCheckRefFormat (0.00s)
    --- PASS: TestGitCheckRefFormat/hello (0.00s)
    --- PASS: TestGitCheckRefFormat/--world (0.00s)
=== RUN   TestGitCheckoutValidatesRef
--- PASS: TestGitCheckoutValidatesRef (0.00s)
=== RUN   TestGitCheckoutSomething
--- PASS: TestGitCheckoutSomething (0.00s)
=== RUN   TestGitClone
--- PASS: TestGitClone (0.00s)
=== RUN   TestGitClean
--- PASS: TestGitClean (0.00s)
=== RUN   TestGitCleanSubmodules
--- PASS: TestGitCleanSubmodules (0.00s)
=== RUN   TestGitFetch
--- PASS: TestGitFetch (0.00s)
PASS

Comment on lines +39 to +40
if !gitCheckRefFormat(reference) {
return fmt.Errorf("%q is not a valid git ref format", reference)
Copy link
Member Author

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.

sj26
sj26 previously approved these changes Oct 5, 2020
Copy link
Member

@sj26 sj26 left a 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
}
}
}
Copy link
Member

@sj26 sj26 Oct 6, 2020

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] == "-"?

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@sj26 sj26 left a comment

Choose a reason for hiding this comment

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

Nice

@pda pda merged commit 3df8d1a into master Oct 6, 2020
@pda pda deleted the git-checkout-validate-ref branch October 6, 2020 08:24
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.

2 participants