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

Add support for main as the default branch #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rastersize
Copy link

@rastersize rastersize commented Mar 10, 2021

GitHub and others are moving to use main as the default branch name. In fact newly created repositories use it by default now. The most recent version/commit of Mint fails to clone tools that don’t have any tags and uses main:

$ mint run rastersize/apollo-codegen
🌱 Finding latest version of apollo-codegen
🌱 Cloning apollo-codegen master
Cloning into 'github.com_rastersize_apollo-codegen'...
warning: Could not find remote branch master to clone.
fatal: Remote branch master not found in upstream origin
🌱 Encountered error during "git clone --depth 1 -b master https://github.com/rastersize/apollo-codegen.git github.com_rastersize_apollo-codegen". Use --verbose to see full output
🌱  Couldn't clone https://github.com/rastersize/apollo-codegen.git master

This PR aims to resolve that by adding support for both main and master. It should in theory support any name for the default branch as it uses git to look up the name of the branch.

With this patch the invocation above succeeds:

$ .build/debug/mint run rastersize/apollo-codegen apollo-codegen --help
🌱 Finding latest version of apollo-codegen
🌱 Cloning apollo-codegen main
🌱 Resolving package
🌱 Building package
🌱 Installed apollo-codegen main
🌱 Running apollo-codegen main...
OVERVIEW: A utility for performing Apollo GraphQL related tasks.

[...]

@rastersize rastersize changed the title Add support for “main” as the default branch Add support for main as the default branch Mar 10, 2021
@rastersize
Copy link
Author

Heya @yonaskolb! Just wanted to check if you had time to look at this, or had any comments/questions about it. Thank you 🙂

Copy link
Owner

@yonaskolb yonaskolb left a comment

Choose a reason for hiding this comment

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

Thanks @rastersize, great change. Note sure it always works though.
Could you also please add a changelog entry

@@ -15,7 +15,7 @@ class PackageCommand: MintfileCommand {
\(description)

The package can be a shorthand for a github repo \"githubName/repo\", or a fully qualified .git path.
An optional version can be specified by appending @version to the repo, otherwise the newest tag will be used (or master if no tags are found.)
An optional version can be specified by appending @version to the repo, otherwise the newest tag will be used (or main, then master if no tags are found.)
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
An optional version can be specified by appending @version to the repo, otherwise the newest tag will be used (or main, then master if no tags are found.)
An optional version can be specified by appending @version to the repo, otherwise the newest tag will be used, or the default branch if untagged

Copy link
Owner

Choose a reason for hiding this comment

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

Could you also update the same message in the Readme, under Package reference

@@ -84,6 +84,11 @@ public class Mint {
return gitRepos
}

func getDefaultGitBranch() throws -> String {
let refsOutput = try Task.capture(bash: "git ls-remote --heads --quiet | cut -f2 | cut -d'/' -f3")
Copy link
Owner

Choose a reason for hiding this comment

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

Are you sure this always works? I tried locally in some repos. Some I got an access error if though they have public remotes:

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

And some I just got a list of the branches in the repo, and not just the default branch. I'm on Git 2.24.3 and zsh 5.8

Choose a reason for hiding this comment

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

I use this in my zsh function, it's a bit different: defaultBranch=$(git ls-remote --symref "$remoteUrl" HEAD | grep "^ref: " | sed 's@^ref: refs/heads/\([^[:space:]]*\).*@\1@')

Choose a reason for hiding this comment

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

Assuming pipefail is on, my version will result in an error in the rare case where the default branch is not set, while the original code will probably not result in an error. (I'm not on my dev machine, so I can't check but I'm 99% sure about this)

related git docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emset-headem

Copy link

Choose a reason for hiding this comment

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

If it's assume both 1) that the repo was cloned, not created locally, and 2) that the first (or only) remote is the one that matters), this should be as good as it seems to be possible to get within the bounds of what's practical (I know the string extraction is a bit tortured; while either sed or the new regex support in Swift 5.7 could do this less painfully, sed is not always dependable between platforms, and 5.7 is a bit of a jump from this project's current minimum supported version of 5.0):

let symref = try Task.capture(bash: "git ls-remote --symref . 'refs/remotes/*/HEAD'")
if symref.starts(with: "ref:") { return symref
    .prefix { !$0.isNewline }    // "ref: refs/remotes/origin/main    refs/remotes/origin/HEAD"
    .dropFirst(17)               // "origin/main    refs/remotes/origin/HEAD"
    .drop { $0 != "/" }          // "/main    refs/remotes/origin/HEAD"
    .dropFirst()                 // "main    refs/remotes/origin/HEAD"
    .prefix { !$0.isWhitespace } // "main"
} else {
    // no remote configured?
    return "HEAD" // fall back to current checked out revision
}

palaniraja added a commit to palaniraja/XcodeMigrate that referenced this pull request Jan 5, 2023
The actual default fails to install as per the current `README.md` this is needed until `mint` team support `main` as alternative to `master` 

Refer: 

XcodeMigrate#29
yonaskolb/Mint#204
imWildCat pushed a commit to XcodeMigrate/XcodeMigrate that referenced this pull request Jan 5, 2023
The actual default fails to install as per the current `README.md` this is needed until `mint` team support `main` as alternative to `master` 

Refer: 

#29
yonaskolb/Mint#204
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.

None yet

4 participants