From 6398b8cf6f29c837162e71d4c7f78dfd0017bc16 Mon Sep 17 00:00:00 2001 From: HumanBojack Date: Mon, 29 Jul 2024 12:58:12 +0200 Subject: [PATCH] feat(sync): add the keep-git-dir flag --- cmd/sync.go | 17 +++++++++++++---- cmd/sync_test.go | 2 +- internal/remotes/git.go | 14 ++++++++------ internal/remotes/git_test.go | 35 +++++++++++++++++++++++++---------- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/cmd/sync.go b/cmd/sync.go index cbf43ea..d25a65a 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -15,9 +15,18 @@ var syncCmd = &cobra.Command{ RunE: syncExecute, } -func syncExecute(_ *cobra.Command, _ []string) error { +func init() { + syncCmd.Flags().Bool("keep-git-dir", false, "Keep the .git directory after cloning") +} + +func syncExecute(ccmd *cobra.Command, _ []string) error { MaybeSetDebug() - if err := sync(); err != nil { + keepGitDir, err := ccmd.Flags().GetBool("keep-git-dir") + if err != nil { + return fmt.Errorf("getting keep-git-dir flag: %w", err) + } + + if err := sync(keepGitDir); err != nil { return fmt.Errorf("executing sync command: %w", err) } return nil @@ -25,7 +34,7 @@ func syncExecute(_ *cobra.Command, _ []string) error { // sync does the heavy lifting to ensure that the local directory tree(s) match // the desired state as defined in the specfile. -func sync() error { +func sync(keepGitDir bool) error { spec, err := vdmspec.GetSpecFromFile(RootFlagValues.SpecFilePath) if err != nil { return fmt.Errorf("getting specs from spec file: %w", err) @@ -58,7 +67,7 @@ SpecLoop: switch remote.Type { case vdmspec.GitType, "": - if err := remotes.SyncGit(remote); err != nil { + if err := remotes.SyncGit(remote, keepGitDir); err != nil { return fmt.Errorf("syncing git remote: %w", err) } case vdmspec.FileType: diff --git a/cmd/sync_test.go b/cmd/sync_test.go index 8708f01..2bee526 100644 --- a/cmd/sync_test.go +++ b/cmd/sync_test.go @@ -21,7 +21,7 @@ func TestSync(t *testing.T) { // Need to override for test RootFlagValues.SpecFilePath = testSpecFilePath - err = sync() + err = sync(false) require.NoError(t, err) // defer t.Cleanup(func() { diff --git a/internal/remotes/git.go b/internal/remotes/git.go index 940a25c..f454f27 100644 --- a/internal/remotes/git.go +++ b/internal/remotes/git.go @@ -12,7 +12,7 @@ import ( ) // SyncGit is the root of the sync operations for "git" remote types. -func SyncGit(remote vdmspec.Remote) error { +func SyncGit(remote vdmspec.Remote, keepGitDir bool) error { err := gitClone(remote) if err != nil { return fmt.Errorf("cloing remote: %w", err) @@ -27,11 +27,13 @@ func SyncGit(remote vdmspec.Remote) error { } } - message.Debugf("removing .git dir for local path '%s'", remote.LocalPath) - dotGitPath := filepath.Join(remote.LocalPath, ".git") - err = os.RemoveAll(dotGitPath) - if err != nil { - return fmt.Errorf("removing directory %s: %w", dotGitPath, err) + if !keepGitDir { + message.Debugf("removing .git dir for local path '%s'", remote.LocalPath) + dotGitPath := filepath.Join(remote.LocalPath, ".git") + err = os.RemoveAll(dotGitPath) + if err != nil { + return fmt.Errorf("removing directory %s: %w", dotGitPath, err) + } } return nil diff --git a/internal/remotes/git_test.go b/internal/remotes/git_test.go index 6b2bde4..8e3323a 100644 --- a/internal/remotes/git_test.go +++ b/internal/remotes/git_test.go @@ -2,6 +2,7 @@ package remotes import ( "os" + "path/filepath" "testing" "github.com/opensourcecorp/vdm/internal/vdmspec" @@ -20,19 +21,33 @@ func getTestGitSpec() vdmspec.Remote { } func TestSyncGit(t *testing.T) { - spec := getTestGitSpec() - err := SyncGit(spec) - require.NoError(t, err) + t.Run("with keepGitDir=false", func(t *testing.T) { + spec := getTestGitSpec() + err := SyncGit(spec, false) + require.NoError(t, err) + defer t.Cleanup(func() { + if cleanupErr := os.RemoveAll(spec.LocalPath); cleanupErr != nil { + t.Fatalf("removing specLocalPath: %v", cleanupErr) + } + }) - defer t.Cleanup(func() { - if cleanupErr := os.RemoveAll(spec.LocalPath); cleanupErr != nil { - t.Fatalf("removing specLocalPath: %v", cleanupErr) - } + _, err = os.Stat(filepath.Join(spec.LocalPath, ".git")) + assert.ErrorIs(t, err, os.ErrNotExist, ".git directory should be removed") }) - t.Run(".git directory was removed", func(t *testing.T) { - _, err := os.Stat("./deps/go-common-tag/.git") - assert.ErrorIs(t, err, os.ErrNotExist) + t.Run("with keepGitDir=true", func(t *testing.T) { + spec := getTestGitSpec() + err := SyncGit(spec, true) + require.NoError(t, err) + defer t.Cleanup(func() { + if cleanupErr := os.RemoveAll(spec.LocalPath); cleanupErr != nil { + t.Fatalf("removing specLocalPath: %v", cleanupErr) + } + }) + + _, err = os.Stat(filepath.Join(spec.LocalPath, ".git")) + assert.NoError(t, err, ".git directory should exist") + assert.DirExists(t, filepath.Join(spec.LocalPath, ".git"), ".git should be a directory") }) }