diff --git a/internal/git/commands.go b/internal/git/commands.go index 29206f4..502c0dc 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -199,9 +199,7 @@ func CloneTmp(user string, gist string, gistTmpId string, email string) error { } // remove every file (and not the .git directory!) - cmd = exec.Command("find", ".", "-maxdepth", "1", "-type", "f", "-delete") - cmd.Dir = tmpRepositoryPath - if err = cmd.Run(); err != nil { + if err = removeFilesExceptGit(tmpRepositoryPath); err != nil { return err } @@ -407,6 +405,21 @@ func createDotGitHookFile(repositoryPath string, hook string, content string) er return nil } +func removeFilesExceptGit(dir string) error { + return filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() && filepath.Base(path) == ".git" { + return filepath.SkipDir + } + if !d.IsDir() { + return os.Remove(path) + } + return nil + }) +} + const preReceive = `#!/bin/sh disallowed_files=""