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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/MintCLI/Commands/PackageCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
if let parameterDescription = parameterDescription {
longDescription += "\n\n\(parameterDescription)"
Expand Down
9 changes: 7 additions & 2 deletions Sources/MintKit/Mint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

return refsOutput.stdout
}

func getLinkedExecutables() -> [Path] {
guard linkPath.exists,
let packages = try? linkPath.children() else {
Expand Down Expand Up @@ -164,14 +169,14 @@ public class Mint {

let tagReferences = tagOutput.stdout
if tagReferences.isEmpty {
package.version = "master"
package.version = try getDefaultGitBranch()
} else {
let tags = tagReferences.split(separator: "\n").map { String($0.split(separator: "\t").last!.split(separator: "/").last!) }
let versions = convertTagsToVersionMap(tags)
if let latestVersion = versions.keys.sorted().last, let tag = versions[latestVersion] {
package.version = tag
} else {
package.version = "master"
package.version = try getDefaultGitBranch()
}
}
} catch {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MintKit/PackageReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class PackageReference {

public var versionCouldBeSHA: Bool {
switch version {
case "master", "develop":
case "main", "master", "develop":
return false
default:
let characterSet = CharacterSet.letters.union(.decimalDigits)
Expand Down
1 change: 1 addition & 0 deletions Tests/MintTests/PackageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class PackageTests: XCTestCase {

XCTAssertFalse(PackageReference(repo: "", version: "my_branch").versionCouldBeSHA)
XCTAssertFalse(PackageReference(repo: "", version: "develop").versionCouldBeSHA)
XCTAssertFalse(PackageReference(repo: "", version: "main").versionCouldBeSHA)
XCTAssertFalse(PackageReference(repo: "", version: "master").versionCouldBeSHA)
XCTAssertFalse(PackageReference(repo: "", version: "1.0").versionCouldBeSHA)
XCTAssertFalse(PackageReference(repo: "", version: "fgvb45g_").versionCouldBeSHA)
Expand Down