Skip to content

Commit

Permalink
feat: add --skip-git flag to skip git repository initialization (#3402
Browse files Browse the repository at this point in the history
)

* chore: change scaffold command to use `--path` as app directory

When `--path` is specified is used as the final app directory. The
blockchain name is used to create a directory by default otherwise
(previous behaviour).

* feat: add `--skip-git` flag to skip git repository initialization

* feat(pkg/xgit): add function to check if a path is a Git repository

* chore: update changelog

---------

Co-authored-by: Alex Johnson <[email protected]>
  • Loading branch information
jeronimoalbi and Alex Johnson authored Feb 7, 2023
1 parent bba9d53 commit 571eb43
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [#2999](https://github.com/ignite/cli/pull/2999/) Add `ignite network request remove-account` command.
- [#2458](https://github.com/ignite/cli/issues/2458) New `chain serve` command UI.
- [#2992](https://github.com/ignite/cli/issues/2992) Add `ignite chain debug` command.
- [#2736](https://github.com/ignite/cli/issues/2736) Add `--skip-git` flag to skip git repository initialization.

### Changes

Expand Down
6 changes: 5 additions & 1 deletion ignite/cmd/scaffold_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

const (
flagNoDefaultModule = "no-module"
flagSkipGit = "skip-git"

tplScaffoldChainSuccess = `
⭐️ Successfully created a new blockchain '%[1]v'.
Expand Down Expand Up @@ -74,8 +75,9 @@ about Cosmos SDK on https://docs.cosmos.network

flagSetClearCache(c)
c.Flags().AddFlagSet(flagSetAccountPrefixes())
c.Flags().StringP(flagPath, "p", ".", "create a project in a specific path")
c.Flags().StringP(flagPath, "p", "", "create a project in a specific path")
c.Flags().Bool(flagNoDefaultModule, false, "create a project without a default module")
c.Flags().Bool(flagSkipGit, false, "skip Git repository initialization")

return c
}
Expand All @@ -89,6 +91,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
addressPrefix = getAddressPrefix(cmd)
appPath = flagGetPath(cmd)
noDefaultModule, _ = cmd.Flags().GetBool(flagNoDefaultModule)
skipGit, _ = cmd.Flags().GetBool(flagSkipGit)
)

cacheStorage, err := newCache(cmd)
Expand All @@ -104,6 +107,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error {
name,
addressPrefix,
noDefaultModule,
skipGit,
)
if err != nil {
return err
Expand Down
20 changes: 15 additions & 5 deletions ignite/pkg/xgit/xgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var (
commitMsg = "Initialized with Ignite CLI"
devXAuthor = &object.Signature{
commitMsg = "Initialized with Ignite CLI"
defaultOpenOpts = git.PlainOpenOptions{DetectDotGit: true}
devXAuthor = &object.Signature{
Name: "Developer Experience team at Ignite",
Email: "[email protected]",
When: time.Now(),
Expand All @@ -26,9 +27,7 @@ var (
// InitAndCommit creates a git repo in path if path isn't already inside a git
// repository, then commits path content.
func InitAndCommit(path string) error {
repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{
DetectDotGit: true,
})
repo, err := git.PlainOpenWithOptions(path, &defaultOpenOpts)
if err != nil {
if !errors.Is(err, git.ErrRepositoryNotExists) {
return fmt.Errorf("open git repo %s: %w", path, err)
Expand Down Expand Up @@ -144,3 +143,14 @@ func Clone(ctx context.Context, urlRef, dir string) error {
Hash: *h,
})
}

// IsRepository checks if a path contains a Git repository.
func IsRepository(path string) (bool, error) {
if _, err := git.PlainOpenWithOptions(path, &defaultOpenOpts); err != nil {
if errors.Is(err, git.ErrRepositoryNotExists) {
return false, nil
}
return false, err
}
return true, nil
}
53 changes: 53 additions & 0 deletions ignite/pkg/xgit/xgit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,56 @@ func TestClone(t *testing.T) {
})
}
}

func TestIsRepository(t *testing.T) {
tests := []struct {
name string
dirFunc func(*testing.T) string
shouldFail bool
expected bool
}{
{
name: "path is a repository",
dirFunc: func(t *testing.T) string {
dir := t.TempDir()
_, err := git.PlainInit(dir, false)
require.NoError(t, err)
return dir
},
expected: true,
},
{
name: "path is not a repository",
dirFunc: func(t *testing.T) string {
return t.TempDir()
},
expected: false,
},
{
name: "repository error",
dirFunc: func(t *testing.T) string {
dir := t.TempDir()
err := os.Chmod(dir, 0)
require.NoError(t, err)
return dir
},
shouldFail: true,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Act
exists, err := xgit.IsRepository(tt.dirFunc(t))

// Assert
require.Equal(t, tt.expected, exists)

if tt.shouldFail {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
30 changes: 22 additions & 8 deletions ignite/services/scaffolder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@ import (
)

// Init initializes a new app with name and given options.
func Init(ctx context.Context, cacheStorage cache.Storage, tracer *placeholder.Tracer, root, name, addressPrefix string, noDefaultModule bool) (path string, err error) {
if root, err = filepath.Abs(root); err != nil {
func Init(
ctx context.Context,
cacheStorage cache.Storage,
tracer *placeholder.Tracer,
root, name, addressPrefix string,
noDefaultModule, skipGit bool,
) (path string, err error) {
pathInfo, err := gomodulepath.Parse(name)
if err != nil {
return "", err
}

pathInfo, err := gomodulepath.Parse(name)
if err != nil {
// Create a new folder named as the blockchain when a custom path is not specified
var appFolder string
if root == "" {
appFolder = pathInfo.Root
}

if root, err = filepath.Abs(root); err != nil {
return "", err
}

path = filepath.Join(root, pathInfo.Root)
path = filepath.Join(root, appFolder)

// create the project
if err := generate(ctx, tracer, pathInfo, addressPrefix, path, noDefaultModule); err != nil {
Expand All @@ -41,9 +53,11 @@ func Init(ctx context.Context, cacheStorage cache.Storage, tracer *placeholder.T
return "", err
}

// initialize git repository and perform the first commit
if err := xgit.InitAndCommit(path); err != nil {
return "", err
if !skipGit {
// Initialize git repository and perform the first commit
if err := xgit.InitAndCommit(path); err != nil {
return "", err
}
}

return path, nil
Expand Down

0 comments on commit 571eb43

Please sign in to comment.