Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean commit branches more thoroughly #208

Merged
merged 1 commit into from
Oct 24, 2023
Merged
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
32 changes: 21 additions & 11 deletions branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
63 changes: 63 additions & 0 deletions branches_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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
}