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

[carry] Automatically detect default git branch #2228

Merged
merged 4 commits into from
Jul 7, 2021

Conversation

tonistiigi
Copy link
Member

@tonistiigi tonistiigi commented Jul 6, 2021

LeviHarrison and others added 4 commits July 6, 2021 15:37
Instead of just assuming that the default branch is master, use ls-remote to find out. Also removed tests that didn't specifiy a branch but required authentication, because those will fail now that the repo is actually checked.

Signed-off-by: Levi Harrison <[email protected]>
It is my suspecion that the tests were failing on previous commits because of the lack of authentication and other stuff like that available in gitidentifier as compared to gitsource

Signed-off-by: Levi Harrison <[email protected]>
Unfortunately, further test cases will have to be removed because gitindentifier will now leave the branch blank instead of filling it in

Signed-off-by: Levi Harrison <[email protected]>
@tonistiigi tonistiigi added this to the v0.9.0 milestone Jul 6, 2021
@tonistiigi tonistiigi requested review from coryb and AkihiroSuda July 6, 2021 23:40
func getDefaultBranch(ctx context.Context, gitDir, workDir, sshAuthSock, knownHosts string, auth []string, remoteURL string) (string, error) {
buf, err := gitWithinDir(ctx, gitDir, workDir, sshAuthSock, knownHosts, auth, "ls-remote", "--symref", remoteURL, "HEAD")
if err != nil {
return "", errors.Wrapf(err, "error fetching default branch for repository %s", redactCredentials(remoteURL))
Copy link
Member

Choose a reason for hiding this comment

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

Can we fallback to "master" on error?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you know a case that wouldn't work? The problem is that this is the first network request via git so there is actually a good chance that this will legitimately fail and I wouldn't want to hide the error.

Copy link
Member

@AkihiroSuda AkihiroSuda Jul 7, 2021

Choose a reason for hiding this comment

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

When was ls-remote introduced? (EDIT: no later than 2007: git/git@7c2c6ee)

Some git server implementation may not support ls-remote?

Copy link
Member Author

Choose a reason for hiding this comment

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

We already had a dependency on ls-remote before to get the sha for the ref.

@AkihiroSuda AkihiroSuda merged commit 6076d93 into moby:master Jul 7, 2021
// getDefaultBranch gets the default branch of a repository using ls-remote
func getDefaultBranch(ctx context.Context, gitDir, workDir, sshAuthSock, knownHosts string, auth []string, remoteURL string) (string, error) {
buf, err := gitWithinDir(ctx, gitDir, workDir, sshAuthSock, knownHosts, auth, "ls-remote", "--symref", remoteURL, "HEAD")
if err != nil {
Copy link

Choose a reason for hiding this comment

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

I think it would be helpful to include a hint on how you would set the branch being used (add #branchname to URL). If greenlit, I can make a PR for the hint in error messages.

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.

Builder: make building from remote git repository work with alternative default branches
4 participants