diff --git a/cmd/sync/snapshot_test.go b/cmd/sync/snapshot_test.go index 6e307cb8..ef945008 100644 --- a/cmd/sync/snapshot_test.go +++ b/cmd/sync/snapshot_test.go @@ -22,8 +22,7 @@ func TestDiff(t *testing.T) { assert.NoError(t, err) defer f2.Close() - fileSet, err := git.NewFileSet(projectDir, false) - assert.NoError(t, err) + fileSet := git.NewFileSet(projectDir) files, err := fileSet.All() assert.NoError(t, err) state := Snapshot{ diff --git a/git/fileset.go b/git/fileset.go index 90493f68..a3734cf7 100644 --- a/git/fileset.go +++ b/git/fileset.go @@ -9,7 +9,6 @@ import ( "time" ignore "github.com/sabhiram/go-gitignore" - "golang.org/x/exp/slices" ) type File struct { @@ -37,39 +36,29 @@ type FileSet struct { ignore *ignore.GitIgnore } -// Retuns FileSet for the repository located at `root` -func NewFileSet(root string, isProjectRoot bool) (*FileSet, error) { - gitIgnoreMatchers := []string{} - gitIgnorePath := fmt.Sprintf("%s/.gitignore", root) - rawIgnore, err := os.ReadFile(gitIgnorePath) +// GetFileSet retrieves FileSet from Git repository checkout root +// or panics if no root is detected. +func GetFileSet() (FileSet, error) { + root, err := Root() + return NewFileSet(root), err +} +// Retuns FileSet for the repository located at `root` +func NewFileSet(root string) FileSet { + lines := []string{".git", ".bricks"} + rawIgnore, err := os.ReadFile(fmt.Sprintf("%s/.gitignore", root)) if err == nil { // add entries from .gitignore if the file exists (did read correctly) for _, line := range strings.Split(string(rawIgnore), "\n") { // underlying library doesn't behave well with Rule 5 of .gitignore, // hence this workaround - gitIgnoreMatchers = append(gitIgnoreMatchers, strings.Trim(line, "/")) + lines = append(lines, strings.Trim(line, "/")) } } - - if isProjectRoot && !slices.Contains(gitIgnoreMatchers, ".databricks") { - f, err := os.OpenFile(gitIgnorePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) - if err != nil { - return nil, err - } - _, err = f.WriteString("\n/.databricks/") - if err != nil { - return nil, err - } - gitIgnoreMatchers = append(gitIgnoreMatchers, ".databricks") - } - - syncIgnoreMatchers := []string{".git"} - - return &FileSet{ + return FileSet{ root: root, - ignore: ignore.CompileIgnoreLines(append(gitIgnoreMatchers, syncIgnoreMatchers...)...), - }, nil + ignore: ignore.CompileIgnoreLines(lines...), + } } // Return root for fileset. diff --git a/git/fileset_test.go b/git/fileset_test.go index 80610ec4..b35e827e 100644 --- a/git/fileset_test.go +++ b/git/fileset_test.go @@ -1,7 +1,6 @@ package git import ( - "fmt" "os" "path/filepath" "strings" @@ -27,8 +26,7 @@ func TestRecusiveListFile(t *testing.T) { // config file is returned // .gitignore is not because we explictly ignore it in .gitignore - fileSet, err := NewFileSet(projectDir, false) - assert.NoError(t, err) + fileSet := NewFileSet(projectDir) files, err := fileSet.RecursiveListFiles(projectDir) assert.NoError(t, err) assert.Len(t, files, 1) @@ -65,8 +63,7 @@ func TestFileSetNonCleanRoot(t *testing.T) { // Path simplification is done by most filepath functions. root := "./../git" require.NotEqual(t, root, filepath.Clean(root)) - fs, err := NewFileSet(root, false) - assert.NoError(t, err) + fs := NewFileSet(root) files, err := fs.All() require.NoError(t, err) diff --git a/go.mod b/go.mod index 2914c540..7dea5c30 100644 --- a/go.mod +++ b/go.mod @@ -13,14 +13,11 @@ require ( github.com/spf13/cobra v1.5.0 // Apache 2.0 github.com/stretchr/testify v1.8.0 // MIT github.com/whilp/git-urls v1.0.0 // MIT - golang.org/x/mod v0.6.0 // BSD-3-Clause + golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // BSD-3-Clause gopkg.in/ini.v1 v1.67.0 // Apache 2.0 ) -require ( - golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 - golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 -) +require golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 require ( cloud.google.com/go/compute v1.6.1 // indirect @@ -36,7 +33,7 @@ require ( go.opencensus.io v0.23.0 // indirect golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect golang.org/x/oauth2 v0.0.0-20220628200809-02e64fa58f26 // indirect - golang.org/x/sys v0.1.0 // indirect + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect google.golang.org/api v0.82.0 // indirect diff --git a/go.sum b/go.sum index 5bebab36..31e98125 100644 --- a/go.sum +++ b/go.sum @@ -252,8 +252,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE= -golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -279,8 +277,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I= -golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -415,9 +413,8 @@ golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/project/project.go b/project/project.go index 19cbd4b9..9b7747af 100644 --- a/project/project.go +++ b/project/project.go @@ -67,10 +67,7 @@ func Initialize(ctx context.Context, root, env string) (context.Context, error) return nil, fmt.Errorf("environment [%s] not defined", env) } - fileSet, err := git.NewFileSet(root, true) - if err != nil { - return ctx, err - } + fileSet := git.NewFileSet(root) p := project{ root: root, @@ -78,7 +75,7 @@ func Initialize(ctx context.Context, root, env string) (context.Context, error) config: &config, environment: &environment, - fileSet: fileSet, + fileSet: &fileSet, } p.initializeWorkspacesClient(ctx) diff --git a/project/project_test.go b/project/project_test.go index 2ecff5d8..3bee240b 100644 --- a/project/project_test.go +++ b/project/project_test.go @@ -4,7 +4,6 @@ import ( "context" "os" "path/filepath" - "strings" "testing" "github.com/stretchr/testify/assert" @@ -17,7 +16,7 @@ func TestProjectInitialize(t *testing.T) { assert.Equal(t, Get(ctx).config.Name, "dev") } -func TestProjectInitializationCreatesGitIgnoreIfAbsent(t *testing.T) { +func TestProjectCacheDirErrorsIfNoGitIgnoreFile(t *testing.T) { // create project root with databricks.yml projectDir := t.TempDir() f1, err := os.Create(filepath.Join(projectDir, "databricks.yml")) @@ -27,60 +26,29 @@ func TestProjectInitializationCreatesGitIgnoreIfAbsent(t *testing.T) { ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment) assert.NoError(t, err) - gitIgnorePath := filepath.Join(projectDir, ".gitignore") - assert.FileExists(t, gitIgnorePath) - fileBytes, err := os.ReadFile(gitIgnorePath) - assert.NoError(t, err) - assert.Contains(t, string(fileBytes), ".databricks") - prj := Get(ctx) _, err = prj.CacheDir() - assert.NoError(t, err) + assert.Error(t, err, "please add /.databricks/ to .gitignore") } -func TestProjectInitializationAddsCacheDirToGitIgnore(t *testing.T) { +func TestProjectCacheDirErrorsIfGitIgnoreEntryAbsent(t *testing.T) { // create project root with databricks.yml projectDir := t.TempDir() f1, err := os.Create(filepath.Join(projectDir, "databricks.yml")) assert.NoError(t, err) defer f1.Close() - gitIgnorePath := filepath.Join(projectDir, ".gitignore") - f2, err := os.Create(gitIgnorePath) + // create empty .gitignore + f2, err := os.Create(filepath.Join(projectDir, ".gitignore")) assert.NoError(t, err) defer f2.Close() ctx, err := Initialize(context.Background(), projectDir, DefaultEnvironment) assert.NoError(t, err) - fileBytes, err := os.ReadFile(gitIgnorePath) - assert.NoError(t, err) - assert.Contains(t, string(fileBytes), ".databricks") - prj := Get(ctx) _, err = prj.CacheDir() - assert.NoError(t, err) -} - -func TestProjectInitializationDoesNotAddCacheDirToGitIgnoreIfAlreadyPresent(t *testing.T) { - // create project root with databricks.yml - projectDir := t.TempDir() - f1, err := os.Create(filepath.Join(projectDir, "databricks.yml")) - assert.NoError(t, err) - defer f1.Close() - - gitIgnorePath := filepath.Join(projectDir, ".gitignore") - - err = os.WriteFile(gitIgnorePath, []byte("/.databricks/"), 0o644) - assert.NoError(t, err) - - _, err = Initialize(context.Background(), projectDir, DefaultEnvironment) - assert.NoError(t, err) - - fileBytes, err := os.ReadFile(gitIgnorePath) - assert.NoError(t, err) - - assert.Equal(t, 1, strings.Count(string(fileBytes), ".databricks")) + assert.Error(t, err, "please add /.databricks/ to .gitignore") } func TestProjectCacheDir(t *testing.T) { diff --git a/project/testdata/.gitignore b/project/testdata/.gitignore deleted file mode 100644 index d4f22fe0..00000000 --- a/project/testdata/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/.databricks/ -/.databricks/ -/.databricks/ -/.databricks/ -/.databricks/ \ No newline at end of file