Skip to content

GitGetter: Re-allow ref=COMMIT_ID #345

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

Merged
merged 1 commit into from
Dec 22, 2021
Merged

Conversation

apparentlymart
Copy link
Contributor

@apparentlymart apparentlymart commented Dec 20, 2021

Earlier versions of this code allowed ref to take any value that would be accepted by git checkout as a valid target of a symbolic ref. We inadvertently made a breaking change that broke that as part of introducing a shallow clone optimization, because shallow clone requires selecting a single branch. (#266)

To restore the previous capabilities while retaining the depth argument, here we accept a compromise where ref has the stronger requirement of being a valid named ref in the remote repository if and only if depth is set to a value greater than zero. If depth isn't set or is less than one, we will do the old behavior of just cloning all of the refs in the remote repository in full and then switching to refer to the selected branch, tag, or naked commit ID as a separate step.

This includes a heuristic to generate an additional error message hint if we get an error from git clone and it looks like the user might've been trying to use depth and ref=COMMIT together. We can't recognize that error accurately because it's only reported as human-oriented git command output, but this heuristic should hopefully minimize situations where we show it inappropriately.


Since this regression was found in Terraform v1.1 first, we previously merged a local fork of GitGetter into Terraform to expedite a fix, in hashicorp/terraform#30155.

This set of changes is functionally equivalent to what we implemented and shipped in Terraform v1.1.1. My intent is to switch Terraform back to using the upstream GitGetter in this codebase once we have changes like these included in a tagged release.


This closes #341. This will also contribute to closing the Terraform issue hashicorp/terraform#30170, once something like this is included in a tagged release that we can switch Terraform to.

@apparentlymart
Copy link
Contributor Author

We seem to have some tests that lack a t.Skip for when credentials aren't available, and so the CI checks failed here. Those failures don't seem to relate to what I changed in this PR, so I'm not intending to fix that here.

Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this up properly @apparentlymart! Sorry for the mess.

get_git.go Outdated
Comment on lines 183 to 186
autoBranch := false
if ref == "" {
ref = findRemoteDefaultBranch(u)
autoBranch = true

Choose a reason for hiding this comment

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

i think this is either a very subjective nit or me not understanding (or both), so feel free to ignore:
is autoBranch essentially a way to tell if we're using the default branch (so ref == "")?
if so, i think it might be clearer to either name it a little differently (maybe like onDefaultBranch or something) or use ref == "" directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess the subtlety I was trying to capture with this name is that we're recording whether the branch was explicitly chosen, vs. it being detected automatically. Specifically, the user can potentially write ?ref=main even if main happens to be the default branch name, in which case autoBranch would be false even though we're going to end up on the default branch in the end.

Really the only motivation for the separate variable here is that we just overwrote the original ref on the line above, and so we no longer have the information about whether ref was originally "" or not. I didn't really like it when I wrote it, but was trying to change what was already here as little as possible. With that said, I think it could be clearer for the extra variable to be originalRef := ref (a string) and then test if originalRef == "" below, which I think more directly captures the intended meaning of "did the caller specify a ref?". What do you think? 🤔

Choose a reason for hiding this comment

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

ooooo yeah. i totally missed that we were overwriting ref.
in that case, i think your proposal of adding originalRef captures what you explained really well!

Earlier versions of this code allowed "ref" to take any value that would
be accepted by "git checkout" as a valid target of a symbolic ref. We
inadvertently made a breaking change that broke that as part of
introducing a shallow clone optimization, because shallow clone requires
selecting a single branch.

To restore the previous capabilities while retaining the "depth" argument,
here we accept a compromise where "ref" has the stronger requirement of
being a valid named ref in the remote repository if and only if "depth"
is set to a value greater than zero. If depth isn't set or is less than
one, we will do the old behavior of just cloning all of the refs in the
remote repository in full and then switching to refer to the selected
branch, tag, or naked commit ID as a separate step.

This includes a heuristic to generate an additional error message hint if
we get an error from "git clone" and it looks like the user might've been
trying to use "depth" and "ref=COMMIT" together. We can't recognize that
error accurately because it's only reported as human-oriented git command
output, but this heuristic should hopefully minimize situations where we
show it inappropriately.
@apparentlymart apparentlymart force-pushed the b-git-getter-ref-commit branch from 7c4115b to f1c466b Compare December 22, 2021 22:00
@apparentlymart apparentlymart merged commit 23702d0 into main Dec 22, 2021
@apparentlymart apparentlymart deleted the b-git-getter-ref-commit branch December 22, 2021 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Git "ref" parameter described as taking a ref, but is a tag or branch name only
3 participants