Skip to content

Commit 1f85598

Browse files
authored
Merge pull request #24 from boyter/hashignore
working on hashignore support and other exclusions
2 parents 8e149c2 + 220796a commit 1f85598

42 files changed

Lines changed: 3939 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/boyter/hashit
33
go 1.25.0
44

55
require (
6+
github.com/boyter/gocodewalker v1.5.1
67
github.com/cespare/xxhash/v2 v2.3.0
78
github.com/djherbis/times v1.6.0
89
github.com/gosuri/uiprogress v0.0.1
@@ -15,6 +16,7 @@ require (
1516
)
1617

1718
require (
19+
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
1820
github.com/dustin/go-humanize v1.0.1 // indirect
1921
github.com/google/uuid v1.6.0 // indirect
2022
github.com/gosuri/uilive v0.0.4 // indirect
@@ -25,6 +27,7 @@ require (
2527
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
2628
github.com/spf13/pflag v1.0.5 // indirect
2729
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
30+
golang.org/x/sync v0.16.0 // indirect
2831
golang.org/x/sys v0.36.0 // indirect
2932
modernc.org/libc v1.66.10 // indirect
3033
modernc.org/mathutil v1.7.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
github.com/boyter/gocodewalker v1.5.1 h1:0YeK2QAkd+ymW5MsagMZapIXD3v9/vrZl0HkFSLpKsw=
2+
github.com/boyter/gocodewalker v1.5.1/go.mod h1:9k+yM6+fIx61F0xI9ChXEGE5DYoLhggw8AxSOtW+kKo=
13
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
24
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
35
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
6+
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
7+
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
48
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
59
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
610
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=

main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,44 @@ func main() {
145145
"input file of newline seperated file locations to process",
146146
)
147147

148+
flags.BoolVar(
149+
&processor.GitIgnore,
150+
"gitignore",
151+
false,
152+
"enable .gitignore file logic",
153+
)
154+
flags.BoolVar(
155+
&processor.GitModuleIgnore,
156+
"gitmodule",
157+
false,
158+
"enable .gitmodules file logic",
159+
)
160+
flags.StringSliceVar(
161+
&processor.PathDenyList,
162+
"exclude-dir",
163+
[]string{},
164+
"directories to exclude",
165+
)
166+
flags.BoolVar(
167+
&processor.Ignore,
168+
"ignore",
169+
false,
170+
"enable .ignore file logic",
171+
)
172+
flags.BoolVar(
173+
&processor.HashIgnore,
174+
"hashignore",
175+
false,
176+
"enable .hashignore file logic",
177+
)
178+
flags.StringArrayVarP(
179+
&processor.Exclude,
180+
"not-match",
181+
`M`,
182+
[]string{},
183+
"ignore files and directories matching regular expression",
184+
)
185+
148186
if err := rootCmd.Execute(); err != nil {
149187
os.Exit(1)
150188
}

processor/file.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ package processor
44

55
import (
66
"fmt"
7+
"github.com/boyter/gocodewalker"
78
"os"
89
"path/filepath"
10+
"regexp"
911
)
1012

1113
func walkDirectory(toWalk string, output chan string) {
@@ -28,3 +30,53 @@ func walkDirectory(toWalk string, output chan string) {
2830
}
2931
}
3032
}
33+
34+
func walkDirectoryWithIgnore(toWalk string, output chan string) {
35+
fileListQueue := make(chan *gocodewalker.File, 1000)
36+
var fileWalker *gocodewalker.FileWalker
37+
if NoThreads != 1 {
38+
fileWalker = gocodewalker.NewParallelFileWalker([]string{toWalk}, fileListQueue)
39+
} else {
40+
fileWalker = gocodewalker.NewFileWalker(toWalk, fileListQueue)
41+
}
42+
43+
// The user flags are to enable processing, while gocodewalker is to disable
44+
// so we need to invert the values.
45+
fileWalker.IgnoreGitIgnore = !GitIgnore
46+
fileWalker.IgnoreIgnoreFile = !Ignore
47+
fileWalker.IgnoreGitModules = !GitModuleIgnore
48+
fileWalker.IncludeHidden = true
49+
fileWalker.ExcludeDirectory = PathDenyList
50+
51+
if HashIgnore {
52+
fileWalker.CustomIgnore = []string{".hashignore"}
53+
}
54+
55+
// handle the errors by printing them out and then ignore
56+
errorHandler := func(err error) bool {
57+
printError(err.Error())
58+
return true
59+
}
60+
fileWalker.SetErrorHandler(errorHandler)
61+
62+
for _, exclude := range Exclude {
63+
regexpResult, err := regexp.Compile(exclude)
64+
if err == nil {
65+
fileWalker.ExcludeFilenameRegex = append(fileWalker.ExcludeFilenameRegex, regexpResult)
66+
fileWalker.ExcludeDirectoryRegex = append(fileWalker.ExcludeDirectoryRegex, regexpResult)
67+
} else {
68+
printError(err.Error())
69+
}
70+
}
71+
72+
go func() {
73+
err := fileWalker.Start()
74+
if err != nil {
75+
printError(err.Error())
76+
}
77+
}()
78+
79+
for f := range fileListQueue {
80+
output <- f.Location
81+
}
82+
}

processor/processor.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ var StreamSize int64 = 1_000_000
7171
// FileInput indicates we have a file passed in which consists of a
7272
var FileInput = ""
7373

74+
// GitIgnore set false to enable .gitignore checks
75+
var GitIgnore = true
76+
77+
// GitModuleIgnore set false to enable .gitmodules checks
78+
var GitModuleIgnore = true
79+
80+
// Ignore set false to enable ignore file checks
81+
var Ignore = true
82+
83+
// HashIgnore set true to enable hashignore file checks
84+
var HashIgnore = false
85+
86+
// PathDenyList sets the paths that should be skipped
87+
var PathDenyList = []string{}
88+
89+
// Exclude is a regular expression which is used to exclude files from being processed
90+
var Exclude = []string{}
91+
7492
var NoThreads = runtime.NumCPU()
7593

7694
// String mapping for hash names
@@ -156,13 +174,12 @@ func Process() {
156174
} else {
157175
if fi.IsDir() {
158176
if Recursive {
159-
walkDirectory(fp, fileListQueue)
177+
walkDirectoryWithIgnore(fp, fileListQueue)
160178
}
161179
} else {
162180
fileListQueue <- fp
163181
}
164182
}
165-
166183
}
167184
close(fileListQueue)
168185
}()

vendor/github.com/boyter/gocodewalker/.gitignore

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/boyter/gocodewalker/.goreleaser.yml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/boyter/gocodewalker/.ignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/boyter/gocodewalker/LICENSE

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/boyter/gocodewalker/Makefile

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)