From 7ddc0cdb42bb285898bc2dc55dc854fee9cb7727 Mon Sep 17 00:00:00 2001 From: Kent Date: Tue, 24 Oct 2023 13:07:13 -0400 Subject: [PATCH] clean commit branches more thoroughly Signed-off-by: Kent --- branches.go | 32 +++++++++++++++--------- branches_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/branches.go b/branches.go index c4708be..6a0c8af 100644 --- a/branches.go +++ b/branches.go @@ -144,19 +144,29 @@ func switchToCommitBranch(rc requestContext) (string, error) { } } - // Clean existing output paths - for appName, appConfig := range rc.target.branchConfig.AppConfigs { - var outputDir string - if appConfig.OutputPath != "" { - outputDir = filepath.Join(rc.repo.WorkingDir(), appConfig.OutputPath) - } else { - outputDir = filepath.Join(rc.repo.WorkingDir(), appName) - } - if err := os.RemoveAll(outputDir); err != nil { - return "", errors.Wrapf(err, "error deleting %q", outputDir) - } + // Clean the branch so we can replace its contents wholesale + if err := cleanCommitBranch(rc.repo.WorkingDir()); err != nil { + return "", errors.Wrap(err, "error cleaning commit branch") } logger.Debug("cleaned commit branch") return commitBranch, nil } + +// cleanCommitBranch deletes the entire contents of the specified directory +// EXCEPT for the .git and .kargo-render subdirectories. +func cleanCommitBranch(dir string) error { + dirEntries, err := os.ReadDir(dir) + if err != nil { + return err + } + for _, dirEntry := range dirEntries { + if dirEntry.Name() == ".git" || dirEntry.Name() == ".kargo-render" { + continue + } + if err = os.RemoveAll(filepath.Join(dir, dirEntry.Name())); err != nil { + return err + } + } + return nil +} diff --git a/branches_test.go b/branches_test.go index 626d701..f38c47b 100644 --- a/branches_test.go +++ b/branches_test.go @@ -1,6 +1,7 @@ package render import ( + "fmt" "os" "path/filepath" "testing" @@ -93,3 +94,65 @@ func TestWriteBranchMetadata(t *testing.T) { require.NoError(t, err) require.True(t, exists) } + +func TestCleanCommitBranch(t *testing.T) { + const subdirCount = 50 + const fileCount = 50 + // Create dummy repo dir + dir, err := createDummyCommitBranchDir(subdirCount, fileCount) + defer os.RemoveAll(dir) + require.NoError(t, err) + // Double-check the setup + dirEntries, err := os.ReadDir(dir) + require.NoError(t, err) + require.Len(t, dirEntries, subdirCount+fileCount+2) + // Delete + err = cleanCommitBranch(dir) + require.NoError(t, err) + // .git should not have been deleted + _, err = os.Stat(filepath.Join(dir, ".git")) + require.NoError(t, err) + // .kargo-render should not have been deleted + _, err = os.Stat(filepath.Join(dir, ".kargo-render")) + require.NoError(t, err) + // Everything else should be deleted + dirEntries, err = os.ReadDir(dir) + require.NoError(t, err) + require.Len(t, dirEntries, 2) +} + +func createDummyCommitBranchDir(dirCount, fileCount int) (string, error) { + // Create a directory + dir, err := os.MkdirTemp("", "") + if err != nil { + return dir, err + } + // Add a dummy .git/ subdir + if err = os.Mkdir(filepath.Join(dir, ".git"), 0755); err != nil { + return dir, err + } + // Add a dummy .kargo-render/ subdir + if err = os.Mkdir(filepath.Join(dir, ".kargo-render"), 0755); err != nil { + return dir, err + } + // Add some other dummy dirs + for i := 0; i < dirCount; i++ { + if err = os.Mkdir( + filepath.Join(dir, fmt.Sprintf("dir-%d", i)), + 0755, + ); err != nil { + return dir, err + } + } + // Add some dummy files + for i := 0; i < fileCount; i++ { + file, err := os.Create(filepath.Join(dir, fmt.Sprintf("file-%d", i))) + if err != nil { + return dir, err + } + if err = file.Close(); err != nil { + return dir, err + } + } + return dir, nil +}