Skip to content

Commit

Permalink
feat: initialize git repository during quickstart (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
adaam2 committed Sep 16, 2024
1 parent 13049fc commit 54272c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/quickstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"github.com/pkg/browser"
"github.com/speakeasy-api/speakeasy/internal/charm/styles"
"github.com/speakeasy-api/speakeasy/internal/config"
"github.com/speakeasy-api/speakeasy/internal/git"
"github.com/speakeasy-api/speakeasy/internal/interactivity"
"github.com/speakeasy-api/speakeasy/internal/log"
"github.com/speakeasy-api/speakeasy/internal/model/flag"
"github.com/speakeasy-api/speakeasy/internal/studio"

gitc "github.com/go-git/go-git/v5"
"github.com/speakeasy-api/huh"
"github.com/speakeasy-api/speakeasy/internal/model"

Expand Down Expand Up @@ -82,6 +84,14 @@ func quickstartExec(ctx context.Context, flags QuickstartFlags) error {
return err
}

_, err = git.InitLocalRepository(workingDir)

if err != nil && !errors.Is(err, gitc.ErrRepositoryAlreadyExists) {
log.From(ctx).Warnf("Encountered issue initializing git repository: %s", err.Error())
} else if err == nil {
log.From(ctx).Infof("Initialized git repository in %s", workingDir)
}

if workflowFile, _, _ := workflow.Load(workingDir); workflowFile != nil {
return ErrWorkflowExists
}
Expand Down
15 changes: 15 additions & 0 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Repository struct {
repo *gitc.Repository
}

// NewLocalRepository will attempt to open a pre-existing git repository in the given directory
// If no repository is found, it will return an empty Repository
func NewLocalRepository(dir string) (*Repository, error) {
repo, err := gitc.PlainOpenWithOptions(dir, &gitc.PlainOpenOptions{
DetectDotGit: true,
Expand All @@ -25,6 +27,19 @@ func NewLocalRepository(dir string) (*Repository, error) {
return &Repository{repo: repo}, nil
}

// InitLocalRepository will initialize a new git repository in the given directory
func InitLocalRepository(dir string) (*Repository, error) {
repo, err := gitc.PlainInitWithOptions(dir, &gitc.PlainInitOptions{
Bare: false,
})

if err != nil {
return &Repository{}, err
}

return &Repository{repo: repo}, nil
}

func (r *Repository) IsNil() bool {
return r.repo == nil
}
Expand Down

0 comments on commit 54272c3

Please sign in to comment.