Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ 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
}

// 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)
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion cmd/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 8 additions & 6 deletions internal/remotes/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
35 changes: 25 additions & 10 deletions internal/remotes/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package remotes

import (
"os"
"path/filepath"
"testing"

"github.com/opensourcecorp/vdm/internal/vdmspec"
Expand All @@ -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")
})
}

Expand Down