Skip to content

Commit

Permalink
Add .gitignore behavior in autoconfig.yaml generation and preview
Browse files Browse the repository at this point in the history
This commit uses the go-gitignore library to parse a .gitignore
file in the target directory should it exist. It then prevents
files from being added to the autoconfig.yaml and, in the case
of preview, the ZIP file that contains the contents of the repo.
  • Loading branch information
Curtis Schlak committed Mar 24, 2024
1 parent 04602e4 commit 89c25e2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 4 deletions.
66 changes: 65 additions & 1 deletion app/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"sort"
"strings"

gitignore "github.com/denormal/go-gitignore"

yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -66,6 +68,60 @@ type ContentFileAttrs struct {
fromHeader bool // fromHeader is set true when the attrs were parsed from the header
}

type emptyGitIgnore struct{}
type alwaysMatch struct {
path string
}

func (am *alwaysMatch) Ignore() bool {
return false
}

func (am *alwaysMatch) Include() bool {
return true
}

func (am *alwaysMatch) String() string {
return am.path
}

func (am *alwaysMatch) Position() gitignore.Position {
return gitignore.Position{am.path, 0, 0, 0}
}

func (gi emptyGitIgnore) Base() string {
return ""
}

func (gi emptyGitIgnore) Match(path string) gitignore.Match {
return &alwaysMatch{path}
}

func (gi emptyGitIgnore) Absolute(path string, something bool) gitignore.Match {
return &alwaysMatch{path}
}

func (gi emptyGitIgnore) Relative(path string, something bool) gitignore.Match {
return &alwaysMatch{path}
}

func (gi emptyGitIgnore) Ignore(path string) bool {
return false
}

func (gi emptyGitIgnore) Include(path string) bool {
return true
}

func NewGitIgnore(repoPath string) gitignore.GitIgnore {
ignore, err := gitignore.NewRepository(repoPath)
if err != nil {
fmt.Fprintf(os.Stdout, "Did not find gitignore in %s\n", repoPath)
ignore = emptyGitIgnore{}
}
return ignore
}

var gitTopLevelCmd = "git rev-parse --show-toplevel"

// only used from publish, just going to send
Expand Down Expand Up @@ -237,6 +293,8 @@ func (cb *ConfigBuilder) newConfigYaml() (ConfigYaml, error) {
}
sort.Strings(unitKeys)

ignore := NewGitIgnore(cb.target)

formattedTargetName := formattedName(cb.target)
for _, unit := range unitKeys {
parts := strings.Split(unit, "/")
Expand Down Expand Up @@ -271,6 +329,10 @@ func (cb *ConfigBuilder) newConfigYaml() (ConfigYaml, error) {
}

for _, contentFile := range unitToContentFileMap[unit] {
match := ignore.Relative(contentFile.Path, false)
if match != nil && match.Ignore() {
continue
}
if anyMatchingPrefix("/"+contentFile.Path, cb.excludePaths) {
continue
}
Expand Down Expand Up @@ -316,7 +378,9 @@ func (cb *ConfigBuilder) newConfigYaml() (ConfigYaml, error) {
}
}
}
config.Standards = append(config.Standards, standard)
if len(standard.ContentFiles) > 0 {
config.Standards = append(config.Standards, standard)
}
}

return config, nil
Expand Down
19 changes: 17 additions & 2 deletions app/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func (p *previewBuilder) compressDirectory(zipTarget string) error {
baseDir = filepath.Base(p.target)
}

ignore := NewGitIgnore(p.target)

// Walk the whole filepath
filepath.Walk(p.target, func(path string, info os.FileInfo, err error) error {
path = filepath.ToSlash(path)
Expand All @@ -189,8 +191,21 @@ func (p *previewBuilder) compressDirectory(zipTarget string) error {

var isConfigFile = strings.Contains(path, "config.yml") || strings.Contains(path, "config.yaml") || strings.Contains(path, "autoconfig.yaml")
ext := filepath.Ext(path)

isGitIgnored := false
if !info.IsDir() {
relPath, relPathErr := filepath.Rel(p.target, path)
if relPathErr == nil {
match := ignore.Relative(relPath, false)
if match != nil && match.Ignore() {
isGitIgnored = true
}
fmt.Printf("%v %s\n", isGitIgnored, relPath)
}
}

// Ignoring all files over 1mb for preivew and warning users if the file is over 20mb that it will be ignored in publish action as well.
if !info.IsDir() && info.Size() > 1000000 && !strings.Contains(path, ".git/") {
if !isGitIgnored && !info.IsDir() && info.Size() > 1000000 && !strings.Contains(path, ".git/") {
if path == "preview-curriculum.zip" { // don't warn on preview-curriculum, it gets read here but still cleaned up
return nil
}
Expand All @@ -202,7 +217,7 @@ func (p *previewBuilder) compressDirectory(zipTarget string) error {
return nil
}

if isConfigFile || fileIsIncluded || (info.IsDir() && !strings.Contains(path, ".git/") && (ext != ".git" && path != "node_modules")) {
if !isGitIgnored && isConfigFile || fileIsIncluded || (info.IsDir() && !strings.Contains(path, ".git/") && (ext != ".git" && path != "node_modules")) {
if err != nil {
return err
}
Expand Down
35 changes: 34 additions & 1 deletion app/cmd/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"testing"
)

const ignoredMDContent = `## Ignored
This file should be ignored.`

const testMDContent = `## Test links
![alt](./image/nested-small.png)
Expand Down Expand Up @@ -59,6 +63,21 @@ func Test_compressDirectory(t *testing.T) {
t.Errorf("There should be paths parsed from the target")
}

err = createTestFile("../../fixtures/test-block-auto-config/ignored.md", ignoredMDContent)
if err != nil {
t.Error("Could not make ignored.md")
}

err = os.MkdirAll("../../fixtures/test-block-auto-config/ignored/", os.FileMode(0777))
if err != nil {
t.Error("Could not make ignored/")
}

err = createTestFile("../../fixtures/test-block-auto-config/ignored/ignored.md", ignoredMDContent)
if err != nil {
t.Error("Could not make ignored/ignored.md")
}

tmpZipFile := "../../fixtures/test-block-auto-config/preview-curriculum.zip"

var challengePaths []string
Expand Down Expand Up @@ -107,6 +126,16 @@ func Test_compressDirectory(t *testing.T) {
}
}

fmt.Printf("%+v", paths)

if _, ok := paths["ignored.md"]; ok {
t.Error("ZIP should not contain ignored.md")
}

if _, ok := paths["ignored/ignored.md"]; ok {
t.Error("ZIP should not contain ignored/ignored.md")
}

os.Remove(tmpZipFile)
}

Expand Down Expand Up @@ -551,7 +580,11 @@ func testFilesExist(t *testing.T, paths []string) {
}

func createTestMD(content string) error {
f, err := os.OpenFile("test.md", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
return createTestFile("test.md", content)
}

func createTestFile(fileName string, content string) error {
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions fixtures/test-block-auto-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignored.md
ignored/
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require (

require (
github.com/VividCortex/ewma v1.1.1 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 h1:0nsrg//Dc7xC74H/TZ5sYR8uk4UQRNjsw8zejqH5a4Q=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817/go.mod h1:C/+sI4IFnEpCn6VQ3GIPEp+FrQnQw+YQP3+n+GdGq7o=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
Expand Down

0 comments on commit 89c25e2

Please sign in to comment.