fork/gdu

git clone https://git.julia.blue/fork/gdu

VXNCXNXfix(ignore): anchor the whole ignore-pattern alternation (#623)5782be9

master
7.1 KiB240 linesraw
1// Package common contains commong logic and interfaces used across Gdu
2// nolint: revive //Why: this is common package
3package common
4
5import (
6	"bufio"
7	"os"
8	"path/filepath"
9	"regexp"
10	"strings"
11
12	log "github.com/sirupsen/logrus"
13)
14
15// CreateIgnorePattern creates one pattern from all path patterns
16func CreateIgnorePattern(paths []string) (compiled *regexp.Regexp, err error) {
17	for i, path := range paths {
18		if _, err = regexp.Compile(path); err != nil {
19			return nil, err
20		}
21		if !filepath.IsAbs(path) {
22			absPath, err := filepath.Abs(path)
23			if err == nil {
24				paths = append(paths, absPath)
25			}
26		} else {
27			relPath, err := filepath.Rel("/", path)
28			if err == nil {
29				paths = append(paths, relPath)
30			}
31		}
32		paths[i] = "(" + path + ")"
33	}
34
35	ignore := `^(?:` + strings.Join(paths, "|") + `)$`
36	return regexp.Compile(ignore)
37}
38
39// SetIgnoreDirPaths sets paths to ignore
40func (ui *UI) SetIgnoreDirPaths(paths []string) {
41	log.Printf("Ignoring dirs %s", strings.Join(paths, ", "))
42	ui.IgnoreDirPaths = make(map[string]struct{}, len(paths)*2)
43	for _, path := range paths {
44		ui.IgnoreDirPaths[path] = struct{}{}
45		if !filepath.IsAbs(path) {
46			if absPath, err := filepath.Abs(path); err == nil {
47				ui.IgnoreDirPaths[absPath] = struct{}{}
48			}
49		} else {
50			if relPath, err := filepath.Rel("/", path); err == nil {
51				ui.IgnoreDirPaths[relPath] = struct{}{}
52			}
53		}
54	}
55}
56
57// SetIgnoreDirPatterns sets regular patterns of dirs to ignore
58func (ui *UI) SetIgnoreDirPatterns(paths []string) error {
59	var err error
60	log.Printf("Ignoring dir patterns %s", strings.Join(paths, ", "))
61	ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths)
62	return err
63}
64
65// SetIgnoreFromFile sets regular patterns of dirs to ignore
66func (ui *UI) SetIgnoreFromFile(ignoreFile string) error {
67	var err error
68	var paths []string
69	log.Printf("Reading ignoring dir patterns from file '%s'", ignoreFile)
70
71	file, err := os.Open(ignoreFile)
72	if err != nil {
73		return err
74	}
75	defer file.Close()
76
77	scanner := bufio.NewScanner(file)
78	for scanner.Scan() {
79		paths = append(paths, scanner.Text())
80	}
81
82	if err := scanner.Err(); err != nil {
83		return err
84	}
85
86	ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths)
87	return err
88}
89
90// SetIgnoreTypes sets file types to ignore
91func (ui *UI) SetIgnoreTypes(types []string) {
92	log.Printf("Ignoring file types: %s", strings.Join(types, ", "))
93	ui.IgnoreTypes = types
94}
95
96// SetIncludeTypes sets file types to include (whitelist)
97func (ui *UI) SetIncludeTypes(types []string) {
98	log.Printf("Including only file types: %s", strings.Join(types, ", "))
99	ui.IncludeTypes = types
100}
101
102// SetIgnoreHidden sets flags if hidden dirs should be ignored
103func (ui *UI) SetIgnoreHidden(value bool) {
104	log.Printf("Ignoring hidden dirs")
105	ui.IgnoreHidden = value
106}
107
108// ShouldDirBeIgnored returns true if given path should be ignored
109func (ui *UI) ShouldDirBeIgnored(name, path string) bool {
110	_, shouldIgnore := ui.IgnoreDirPaths[path]
111	if shouldIgnore {
112		log.Printf("Directory %s ignored", path)
113	}
114	return shouldIgnore
115}
116
117// ShouldDirBeIgnoredUsingPattern returns true if given path should be ignored
118func (ui *UI) ShouldDirBeIgnoredUsingPattern(name, path string) bool {
119	shouldIgnore := ui.IgnoreDirPathPatterns.MatchString(path)
120	if shouldIgnore {
121		log.Printf("Directory %s ignored", path)
122	}
123	return shouldIgnore
124}
125
126// IsHiddenDir returns if the dir name begins with dot
127func (ui *UI) IsHiddenDir(name, path string) bool {
128	shouldIgnore := name[0] == '.'
129	if shouldIgnore {
130		log.Printf("Directory %s ignored", path)
131	}
132	return shouldIgnore
133}
134
135// ShouldFileBeIgnoredByType returns true if file should be ignored based on its extension
136func (ui *UI) ShouldFileBeIgnoredByType(name string) bool {
137	if len(ui.IgnoreTypes) == 0 {
138		return false
139	}
140
141	ext := strings.ToLower(filepath.Ext(name))
142	if ext == "" {
143		return false // No extension, don't ignore
144	}
145
146	// Remove leading dot from extension
147	ext = strings.TrimPrefix(ext, ".")
148
149	for _, ignoreType := range ui.IgnoreTypes {
150		// Remove leading dot from ignoreType
151		cleanIgnoreType := strings.TrimPrefix(strings.ToLower(ignoreType), ".")
152		if cleanIgnoreType == ext {
153			log.Printf("File %s ignored by type", name)
154			return true
155		}
156	}
157	return false
158}
159
160// ShouldFileBeIncludedByType returns true if file should be included based on its extension
161func (ui *UI) ShouldFileBeIncludedByType(name string) bool {
162	if len(ui.IncludeTypes) == 0 {
163		return true // No include filter, include all
164	}
165
166	ext := strings.ToLower(filepath.Ext(name))
167	if ext == "" {
168		return false // No extension, don't include if we have include filter
169	}
170
171	// Remove leading dot from extension
172	ext = strings.TrimPrefix(ext, ".")
173
174	for _, includeType := range ui.IncludeTypes {
175		// Remove leading dot from includeType
176		cleanIncludeType := strings.TrimPrefix(strings.ToLower(includeType), ".")
177		if cleanIncludeType == ext {
178			return true
179		}
180	}
181
182	log.Printf("File %s excluded by type filter", name)
183	return false
184}
185
186// CreateIgnoreFunc returns function for detecting if dir should be ignored
187// nolint: gocyclo // Why: This function is a switch statement that is not too complex
188func (ui *UI) CreateIgnoreFunc() ShouldDirBeIgnored {
189	switch {
190	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && !ui.IgnoreHidden:
191		return ui.ShouldDirBeIgnored
192	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden:
193		return func(name, path string) bool {
194			return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path)
195		}
196	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden:
197		return func(name, path string) bool {
198			return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path)
199		}
200	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden:
201		return func(name, path string) bool {
202			return ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path)
203		}
204	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden:
205		return ui.ShouldDirBeIgnoredUsingPattern
206	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden:
207		return ui.IsHiddenDir
208	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden:
209		return func(name, path string) bool {
210			return ui.ShouldDirBeIgnored(name, path) || ui.IsHiddenDir(name, path)
211		}
212	default:
213		return func(name, path string) bool { return false }
214	}
215}
216
217// CreateFileTypeFilter returns function for detecting if file should be ignored based on type
218func (ui *UI) CreateFileTypeFilter() ShouldFileBeIgnored {
219	// If we have include types, use whitelist mode
220	if len(ui.IncludeTypes) > 0 {
221		return func(name string) bool {
222			return !ui.ShouldFileBeIncludedByType(name)
223		}
224	}
225
226	// If we have ignore types, use blacklist mode
227	if len(ui.IgnoreTypes) > 0 {
228		return func(name string) bool {
229			return ui.ShouldFileBeIgnoredByType(name)
230		}
231	}
232
233	// No type filtering - return nil to indicate no filtering is needed
234	return nil
235}
236
237// IsFilteringFiles returns true if we have any file type filters set
238func (ui *UI) IsFilteringFiles() bool {
239	return len(ui.IgnoreTypes) > 0 || len(ui.IncludeTypes) > 0 || ui.FilteringFiles
240}