From 8f9bdf69adab0c58ddca0ad8b74bc7523a740f83 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Thu, 1 Dec 2022 11:08:20 -0800 Subject: [PATCH] Make sure scripts are valid when updating DB --- internal/repos/pull.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internal/repos/pull.go b/internal/repos/pull.go index cb3af86..39842a5 100644 --- a/internal/repos/pull.go +++ b/internal/repos/pull.go @@ -14,6 +14,7 @@ import ( "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/format/diff" "github.com/pelletier/go-toml/v2" "go.arsenm.dev/logger/log" "go.arsenm.dev/lure/distro" @@ -174,6 +175,11 @@ func processRepoChanges(ctx context.Context, repo types.Repo, r *git.Repository, var actions []action for _, fp := range patch.FilePatches() { from, to := fp.Files() + + if !isValid(from, to) { + continue + } + if to == nil { actions = append(actions, action{ Type: actionDelete, @@ -274,6 +280,21 @@ func processRepoChanges(ctx context.Context, repo types.Repo, r *git.Repository, return nil } +// isValid makes sure the path of the file being updated is valid. +// It checks to make sure the file is not within a nested directory +// and that it is called lure.sh. +func isValid(from, to diff.File) bool { + var path string + if from != nil { + path = from.Path() + } + if to != nil { + path = to.Path() + } + + return strings.Count(path, "/") == 1 && strings.HasSuffix(path, "lure.sh") +} + func processRepoFull(ctx context.Context, repo types.Repo, repoDir string, gdb *genji.DB) error { glob := filepath.Join(repoDir, "/*/lure.sh") matches, err := filepath.Glob(glob)