Skip to content

Commit

Permalink
fix: don't make cli default to "main" branch (dagger#7688)
Browse files Browse the repository at this point in the history
Previously, we didn't use to have a way to get the "default" branch -
however, now we do, we can just do `.Head()` on a git repository.

So we don't need to assume "main" at any point.

Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Jun 18, 2024
1 parent 79a8001 commit dceb4de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
23 changes: 16 additions & 7 deletions cmd/dagger/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,14 @@ func (v *directoryValue) Get(ctx context.Context, dag *dagger.Client, modSrc *da
if authSock, ok := os.LookupEnv("SSH_AUTH_SOCK"); ok {
gitOpts.SSHAuthSocket = dag.Host().UnixSocket(authSock)
}
gitDir := dag.Git(parsedGit.Remote, gitOpts).Branch(parsedGit.Fragment.Ref).Tree()
git := dag.Git(parsedGit.Remote, gitOpts)
var gitRef *dagger.GitRef
if parsedGit.Fragment.Ref == "" {
gitRef = git.Head()
} else {
gitRef = git.Branch(parsedGit.Fragment.Ref)
}
gitDir := gitRef.Tree()
if subdir := parsedGit.Fragment.Subdir; subdir != "" {
gitDir = gitDir.Directory(subdir)
}
Expand Down Expand Up @@ -259,11 +266,6 @@ func parseGit(urlStr string) (*gitutil.GitURL, error) {
if u.Fragment == nil {
u.Fragment = &gitutil.GitURLFragment{}
}
if u.Fragment.Ref == "" {
// FIXME: default branch can be remotely looked up, but that would
// require 1) a context, 2) a way to return an error, 3) more time than I have :)
u.Fragment.Ref = "main"
}
return u, nil
}

Expand Down Expand Up @@ -303,7 +305,14 @@ func (v *fileValue) Get(_ context.Context, dag *dagger.Client, _ *dagger.ModuleS
if authSock, ok := os.LookupEnv("SSH_AUTH_SOCK"); ok {
gitOpts.SSHAuthSocket = dag.Host().UnixSocket(authSock)
}
gitDir := dag.Git(parsedGit.Remote, gitOpts).Branch(parsedGit.Fragment.Ref).Tree()
git := dag.Git(parsedGit.Remote, gitOpts)
var gitRef *dagger.GitRef
if parsedGit.Fragment.Ref == "" {
gitRef = git.Head()
} else {
gitRef = git.Branch(parsedGit.Fragment.Ref)
}
gitDir := gitRef.Tree()
path := parsedGit.Fragment.Subdir
if path == "" {
return nil, fmt.Errorf("expected path selection for git repo")
Expand Down
8 changes: 4 additions & 4 deletions cmd/dagger/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestParseGit(t *testing.T) {
Host: "github.com",
Path: "/shykes/daggerverse",
Fragment: &gitutil.GitURLFragment{
Ref: "main",
Ref: "",
},
Remote: "ssh://git@github.com/shykes/daggerverse",
},
Expand All @@ -72,7 +72,7 @@ func TestParseGit(t *testing.T) {
Host: "github.com",
Path: "/shykes/daggerverse.git",
Fragment: &gitutil.GitURLFragment{
Ref: "main",
Ref: "",
},
Remote: "ssh://git@github.com/shykes/daggerverse.git",
},
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestParseGit(t *testing.T) {
Host: "github.com",
Path: "sipsma/daggerverse",
Fragment: &gitutil.GitURLFragment{
Ref: "main",
Ref: "",
},
Remote: "git@github.com:sipsma/daggerverse",
},
Expand All @@ -152,7 +152,7 @@ func TestParseGit(t *testing.T) {
Host: "github.com",
Path: "sipsma/daggerverse.git",
Fragment: &gitutil.GitURLFragment{
Ref: "main",
Ref: "",
},
Remote: "git@github.com:sipsma/daggerverse.git",
},
Expand Down

0 comments on commit dceb4de

Please sign in to comment.