Skip to content

Commit 0c626c7

Browse files
committed
add global ignore #32
1 parent 58cc429 commit 0c626c7

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

cmd/chkbit/help.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var helpTips = `
1818
- \\ escape to match the following character
1919
- lines starting with '#' are skipped
2020
- lines starting with '/' are only applied to the current directory
21+
- global ignore file location: <global-ignore-file>
2122
2223
Status codes:
2324
PNC: exception/panic, unable to continue

cmd/chkbit/main.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ var (
6464
termDimFG = lterm.Fg8(240)
6565
)
6666

67+
var configRoot string
68+
6769
type CLI struct {
6870
Check struct {
6971
Paths []string `arg:"" name:"paths" help:"directories to check"`
@@ -113,22 +115,23 @@ type CLI struct {
113115
Version struct {
114116
} `cmd:"" help:"show version information"`
115117

116-
LogDeleted bool `short:"x" help:"log deleted/missing files/directories since the last run" negatable:""`
117-
IncludeDot bool `short:"d" help:"include dot files" negatable:""`
118-
SkipSymlinks bool `short:"S" help:"do not follow symlinks" negatable:""`
119-
NoRecurse bool `short:"R" help:"do not recurse into subdirectories" negatable:""`
120-
NoDirInIndex bool `short:"D" help:"do not track directories in the index" negatable:""`
121-
NoConfig bool `help:"ignore the config file"`
122-
MaxDepth int `default:0 help:"process a directory only if it is N or fewer levels below the specified path(s); 0 for no limit"`
123-
LogFile string `short:"l" help:"write to a logfile if specified"`
124-
LogVerbose bool `help:"verbose logging" negatable:""`
125-
Algo string `default:"blake3" help:"hash algorithm: md5, sha512, blake3"`
126-
IndexName string `default:".chkbit" help:"filename where chkbit stores its hashes, needs to start with '.'"`
127-
IgnoreName string `default:".chkbitignore" help:"filename that chkbit reads its ignore list from, needs to start with '.'"`
128-
Workers int `short:"w" default:"5" help:"number of workers to use. For slow IO (like on a spinning disk) --workers=1 will be faster"`
129-
Plain bool `help:"show plain status instead of being fancy" negatable:""`
130-
Quiet bool `short:"q" help:"quiet, don't show progress/information" negatable:""`
131-
Verbose bool `short:"v" help:"verbose output" negatable:""`
118+
LogDeleted bool `short:"x" help:"log deleted/missing files/directories since the last run" negatable:""`
119+
IncludeDot bool `short:"d" help:"include dot files" negatable:""`
120+
SkipSymlinks bool `short:"S" help:"do not follow symlinks" negatable:""`
121+
NoRecurse bool `short:"R" help:"do not recurse into subdirectories" negatable:""`
122+
NoDirInIndex bool `short:"D" help:"do not track directories in the index" negatable:""`
123+
NoConfig bool `help:"do not load the config file"`
124+
NoGlobalIgnore bool `help:"do not load the global ignore file"`
125+
MaxDepth int `default:0 help:"process a directory only if it is N or fewer levels below the specified path(s); 0 for no limit"`
126+
LogFile string `short:"l" help:"write to a logfile if specified"`
127+
LogVerbose bool `help:"verbose logging" negatable:""`
128+
Algo string `default:"blake3" help:"hash algorithm: md5, sha512, blake3"`
129+
IndexName string `default:".chkbit" help:"filename where chkbit stores its hashes, needs to start with '.'"`
130+
IgnoreName string `default:".chkbitignore" help:"filename that chkbit reads its ignore list from, needs to start with '.'"`
131+
Workers int `short:"w" default:"5" help:"number of workers to use. For slow IO (like on a spinning disk) --workers=1 will be faster"`
132+
Plain bool `help:"show plain status instead of being fancy" negatable:""`
133+
Quiet bool `short:"q" help:"quiet, don't show progress/information" negatable:""`
134+
Verbose bool `short:"v" help:"verbose output" negatable:""`
132135
}
133136

134137
type CLIDedup struct {
@@ -338,6 +341,9 @@ func (m *Main) runCmd(command string, cli CLI) int {
338341
m.context.SkipSubdirectories = cli.NoRecurse
339342
m.context.TrackDirectories = !cli.NoDirInIndex
340343
m.context.MaxDepth = cli.MaxDepth
344+
if !cli.NoGlobalIgnore {
345+
m.context.GlobalIgnorePath = configRoot
346+
}
341347

342348
st, root, err := chkbit.LocateIndex(pathList[0], chkbit.IndexTypeAny, m.context.IndexFilename)
343349
if err != nil {
@@ -461,9 +467,8 @@ func (m *Main) run() int {
461467
}
462468

463469
var configPath = "chkbit-config.json"
464-
configRoot, err := os.UserConfigDir()
465-
if err == nil {
466-
configPath = slpath.Join(configRoot, "chkbit/config.json")
470+
if configRoot != "" {
471+
configPath = slpath.Join(configRoot, "config.json")
467472
}
468473

469474
var cli CLI
@@ -595,7 +600,14 @@ func (m *Main) run() int {
595600
return 0
596601

597602
case cmdTips:
598-
fmt.Println(strings.ReplaceAll(helpTips, "<config-file>", configPath))
603+
globalIgnoreFile := "unsupported"
604+
605+
if configRoot != "" {
606+
globalIgnoreFile = slpath.Join(configRoot, cli.IgnoreName)
607+
}
608+
tips := strings.ReplaceAll(helpTips, "<config-file>", configPath)
609+
tips = strings.ReplaceAll(tips, "<global-ignore-file>", globalIgnoreFile)
610+
fmt.Println(tips)
599611
return 0
600612
case cmdVersion:
601613
fmt.Println("github.com/laktak/chkbit")
@@ -616,6 +628,13 @@ func main() {
616628
}
617629
}()
618630

631+
var err error
632+
if configRoot, err = os.UserConfigDir(); err == nil {
633+
configRoot = slpath.Join(configRoot, "chkbit")
634+
} else {
635+
configRoot = ""
636+
}
637+
619638
termWidth := lterm.GetWidth()
620639
m := &Main{
621640
logger: log.New(io.Discard, "", 0),

context.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Context struct {
2424
SkipSubdirectories bool
2525
IndexFilename string
2626
IgnoreFilename string
27+
GlobalIgnorePath string
2728
MaxDepth int
2829

2930
WorkQueue chan *WorkItem
@@ -142,6 +143,14 @@ func (context *Context) Process(pathList []string) {
142143
context.NumUpd = 0
143144
context.NumDel = 0
144145

146+
var globalIgnore *Ignore = nil
147+
148+
if context.GlobalIgnorePath != "" {
149+
if ignore, err := GetIgnore(context, context.GlobalIgnorePath, nil); err == nil {
150+
globalIgnore = ignore
151+
}
152+
}
153+
145154
err := context.store.Open(!context.UpdateIndex, context.NumWorkers*10)
146155
if err != nil {
147156
context.logErr("index", err)
@@ -159,7 +168,7 @@ func (context *Context) Process(pathList []string) {
159168
}
160169
go func() {
161170
for _, path := range pathList {
162-
context.scanDir(path, nil, 1)
171+
context.scanDir(path, globalIgnore, 1)
163172
}
164173
for i := 0; i < context.NumWorkers; i++ {
165174
context.endWork()

index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (i *Index) calcHashes(ignore *Ignore) {
127127

128128
func (i *Index) showIgnoredOnly(ignore *Ignore) {
129129
for _, name := range i.files {
130-
if ignore.shouldIgnore(name) {
130+
if ignore.shouldIgnore(name) && !ignore.context.isChkbitFile(name) {
131131
i.logFile(StatusIgnore, name)
132132
}
133133
}

scripts/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func runCmd(args ...string) *exec.Cmd {
1919
_, filename, _, _ := runtime.Caller(0)
2020
prjRoot := filepath.Dir(filepath.Dir(filename))
2121
tool := filepath.Join(prjRoot, "chkbit")
22-
args = append([]string{"--no-config"}, args...)
22+
args = append([]string{"--no-config", "--no-global-ignore"}, args...)
2323
return exec.Command(tool, args...)
2424
}
2525

0 commit comments

Comments
 (0)