Skip to content

Commit

Permalink
Merge pull request #36 from A2-ai/feature/disallow-external-files
Browse files Browse the repository at this point in the history
Ensure add files are in git repository
  • Loading branch information
andriygm authored Aug 15, 2023
2 parents f3bf0b1 + d8df1d6 commit 8ea2474
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 38 deletions.
100 changes: 64 additions & 36 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -50,48 +51,75 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
var filesToAdd []string
for _, path := range args {
globMatches, err := filepath.Glob(path)
// If the path is a glob pattern, add all matches
// Otherwise, add the path itself
if err == nil {
if !slices.Contains(globMatches, path) {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(path), "\n")

if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(path), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: fmt.Sprintf("Skipping invalid path %s", path),
Location: path,
})
continue
}

if !slices.Contains(globMatches, path) {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(path), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: fmt.Sprintf("Skipping invalid path %s", path),
Location: path,
})
}

for _, match := range globMatches {
// Ensure file is inside of the git repo
absPath, err := filepath.Abs(match)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: fmt.Sprintf("Skipping invalid path %s", path),
Location: path,
Message: "skipped invalid path",
Location: match,
})
continue
}
if strings.TrimPrefix(absPath, gitDir) == absPath {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping file outside of git repository", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped file outside of git repository",
Location: match,
})
continue
}

// Check if file exists
fileStat, err := os.Stat(match)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped invalid path",
Location: match,
})

continue
}

// Skip directories
if fileStat.IsDir() {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping directory", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped directory",
Location: match,
})

for _, match := range globMatches {
fileStat, err := os.Stat(match)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped invalid path",
Location: match,
})

continue
}

// Skip directories
if fileStat.IsDir() {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping directory", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped directory",
Location: match,
})

continue
}

// Add the file to the queue
filesToAdd = append(filesToAdd, match)
continue
}
} else {
filesToAdd = append(filesToAdd, path)

// Add the file to the queue
filesToAdd = append(filesToAdd, match)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/storage/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Add(localPath string, storageDir string, gitDir string, message string, dry
Path: localPath,
})
} else {
log.Print(log.ColorBold(log.ColorYellow(" !")), "File already exists, not copying")
log.Print(" Copying...", log.ColorGreen("✔ file already up to date"))
}

// Get file size
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Get(localPath string, storageDir string, gitDir string, dry bool) error {
log.OverwritePreviousLine()
log.Print(" Cleaning up...", log.ColorGreen("✔\n"))
} else {
log.Print(log.ColorGreen(" ✔"), "File already up to date\n")
log.Print(" Copying...", log.ColorGreen("✔ file already up to date\n"))
}

return nil
Expand Down

0 comments on commit 8ea2474

Please sign in to comment.