Skip to content

Commit 1a2dbfa

Browse files
committed
fix patterns
1 parent c70c17b commit 1a2dbfa

File tree

9 files changed

+71
-54
lines changed

9 files changed

+71
-54
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,17 @@ $ gocatcli index /some/other/path myStorageName
155155
[gocatcli](https://github.com/deadc0de6/gocatcli) uses the *basename* of the
156156
path to index as the storage name unless you specify the name when indexing.
157157

158+
### ignore pattern
159+
158160
The below example ignores any file ending with `.go` or `.md` and anything in the `.git` directory:
159161
```bash
160-
$ gocatcli index ../gocatcli --ignore="*.go" --ignore="*.md" --ignore="*.git/*"
162+
$ gocatcli index ../gocatcli --ignore="\.go" --ignore="\.md" --ignore="\.git/*"
163+
```
164+
165+
To ignore any hidden file for example, you would use `\.+` to ensure
166+
that at least a single dot (`.`) is matched.
167+
```bash
168+
$ gocatcli index ../gocatcli --ignore='\.+'
161169
```
162170

163171
## Reindex and update

internal/commands/find.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"gocatcli/internal/node"
1212
"gocatcli/internal/stringer"
1313
"gocatcli/internal/tree"
14-
"gocatcli/internal/utilities"
1514
"regexp"
1615
"strings"
1716
"time"
@@ -83,7 +82,7 @@ func find(_ *cobra.Command, args []string) error {
8382

8483
for _, arg := range args {
8584
// patch pattern
86-
arg = utilities.PatchPattern(arg)
85+
arg = patchFindPattern(arg)
8786
// get the pattern to search for
8887
patt := arg
8988
re, err := regexp.Compile(patt)
@@ -127,3 +126,38 @@ func matchNodes(t *tree.Tree, startNode node.Node, patt *regexp.Regexp, prt stri
127126

128127
log.Debugf("found %d entries matching \"%s\" in %v", cnt, patt.String(), time.Since(t0))
129128
}
129+
130+
// fix pattern
131+
func patchFindPattern(pattern string) string {
132+
// replace any dot with \.
133+
patt := strings.ReplaceAll(pattern, ".", "\\.")
134+
135+
// ensure pattern is enclosed in stars
136+
if !strings.Contains(patt, "*") {
137+
ret := fmt.Sprintf(".*%s.*", patt)
138+
log.Debugf("patched non pattern from \"%s\" to \"%s\"", patt, ret)
139+
return ret
140+
}
141+
142+
// replace all "*" with ".*" for golang pattern
143+
notDotStar := regexp.MustCompile(`([^\.])\*`)
144+
ret := notDotStar.ReplaceAllString(patt, "$1.*")
145+
146+
// replace the first star if any
147+
if strings.HasPrefix(ret, "*") {
148+
ret = fmt.Sprintf(".*%s", ret[1:])
149+
}
150+
151+
// limit start of line if not star
152+
if !strings.HasPrefix(ret, ".*") {
153+
ret = fmt.Sprintf("^%s", ret)
154+
}
155+
156+
// limit end of line if not star
157+
if !strings.HasSuffix(ret, ".*") {
158+
ret = fmt.Sprintf("%s$", ret)
159+
}
160+
161+
log.Debugf("patched pattern from \"%s\" to \"%s\"", pattern, ret)
162+
return ret
163+
}

internal/utilities/fs.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"math"
1616
"os"
1717
"path/filepath"
18-
"regexp"
1918
"strings"
2019
"time"
2120

@@ -203,35 +202,5 @@ func NotIn(needle string, stack []string) bool {
203202

204203
// PatchPattern fix pattern
205204
func PatchPattern(pattern string) string {
206-
// replace any dot with \.
207-
patt := strings.ReplaceAll(pattern, ".", "\\..")
208-
209-
// ensure pattern is enclosed in stars
210-
if !strings.Contains(patt, "*") {
211-
ret := fmt.Sprintf(".*%s.*", patt)
212-
log.Debugf("patched non pattern from \"%s\" to \"%s\"", patt, ret)
213-
return ret
214-
}
215-
216-
// replace all "*" with ".*" for golang pattern
217-
notDotStar := regexp.MustCompile(`([^\.])\*`)
218-
ret := notDotStar.ReplaceAllString(patt, "$1.*")
219-
220-
// replace the first star if any
221-
if strings.HasPrefix(ret, "*") {
222-
ret = fmt.Sprintf(".*%s", ret[1:])
223-
}
224-
225-
// limit start of line if not star
226-
if !strings.HasPrefix(ret, ".*") {
227-
ret = fmt.Sprintf("^%s", ret)
228-
}
229-
230-
// limit end of line if not star
231-
if !strings.HasSuffix(ret, ".*") {
232-
ret = fmt.Sprintf("%s$", ret)
233-
}
234-
235-
log.Debugf("patched pattern from \"%s\" to \"%s\"", pattern, ret)
236-
return ret
205+
return pattern
237206
}

tests-ng/test-index-ignores.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,42 @@ out="${tmpd}/output.txt"
2626
## create a fake dir
2727
src="${tmpd}/to-index"
2828
mkdir -p "${src}"
29+
30+
# add some hidden and non hidden files
2931
mkdir "${src}/.hidden"
3032
echo "hidden" > "${src}/.hidden/hiddenfile"
3133
mkdir "${src}/nothidden"
3234
echo "not-hidden" > "${src}/nothidden/subfile"
3335
echo "hidden" > "${src}/.file-hidden"
3436
echo "not-hidden" > "${src}/notfile-hidden"
3537

38+
# add some with extensions
39+
mkdir -p "${src}/with.ext"
40+
echo "withext" > "${src}/with.ext/inside"
41+
echo "theext" > "${src}/file.ext"
42+
3643
# index
3744
echo ">>> indexing <<<"
38-
"${bin}" index -a -C -c "${catalog}" --ignore='.*' "${tmpd}/to-index" gocatcli
45+
"${bin}" index -a -C -c "${catalog}" --debug --ignore='\.+' --ignore='\.ext' "${tmpd}/to-index" gocatcli
3946
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1
4047

4148
# ls
42-
echo ">>> test ls <<<"
49+
echo ">>> test index ignore ls <<<"
4350
"${bin}" -c "${catalog}" ls -a -r | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
4451
# shellcheck disable=SC2126
4552
#expected=$(find "${cur}/../" -not -path '*/.git*' | grep -v '^.$' | wc -l)
4653
cat_file "${out}"
4754

4855
expected=4
49-
cnt=$(cat "${out}" | wc -l)
56+
cnt=$(wc -l "${out}" | awk '{print $1}')
5057
[ "${cnt}" != "${expected}" ] && echo "expecting ${expected} lines got ${cnt}" && exit 1
5158

52-
cat "${out}" | grep 'notfile-hidden' || exit 1
53-
cat "${out}" | grep 'nothidden' || exit 1
54-
cat "${out}" | grep 'subfile' || exit 1
55-
56-
cat "${out}" | grep '\.hidden' && exit 1
57-
cat "${out}" | grep 'hiddenfile' && exit 1
58-
cat "${out}" | grep '\.file-hidden' && exit 1
59+
grep 'notfile-hidden' "${out}" || exit 1
60+
grep 'nothidden' "${out}" || exit 1
61+
grep 'subfile' "${out}" || exit 1
62+
grep '\.hidden' "${out}" && exit 1
63+
grep 'hiddenfile' "${out}" && exit 1
64+
grep '\.file-hidden' "${out}" && exit 1
5965

6066
echo "test $(basename "${0}") OK!"
6167
exit 0

tests-ng/test-index.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ out="${tmpd}/output.txt"
2525

2626
# index
2727
echo ">>> test index <<<"
28-
"${bin}" index -a -C -c "${catalog}" --ignore=".git" "${cur}/../" gocatcli
28+
"${bin}" index -a -C -c "${catalog}" --debug --ignore='\.git' "${cur}/../" gocatcli
2929
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1
3030

3131
# ls
32-
echo ">>> test ls <<<"
32+
echo ">>> test index ls <<<"
3333
"${bin}" -c "${catalog}" ls -a -r | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
3434
# shellcheck disable=SC2126
3535
#expected=$(find "${cur}/../" -not -path '*/.git*' | grep -v '^.$' | wc -l)
3636
cat_file "${out}"
37-
"${cur}/plist.py" -l "${cur}/../" --ignore '*/.git*'
37+
"${cur}/plist.py" -l "${cur}/../" --ignore '*/\.git*'
3838
expected=$("${cur}/plist.py" "${cur}/../" --ignore '*/.git*')
3939
cnt=$(tail -n +2 "${out}" | sed '/^$/d' | wc -l)
4040
[ "${cnt}" != "${expected}" ] && echo "expecting ${expected} lines got ${cnt}" && exit 1
@@ -47,11 +47,11 @@ echo ">>> test index <<<"
4747
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1
4848

4949
# ls
50-
echo ">>> test ls (2) <<<"
50+
echo ">>> test index ls (2) <<<"
5151
"${bin}" --debug -c "${catalog}" ls -a -r | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
5252
# shellcheck disable=SC2126
5353
#expected=$(find "${cur}/../internal" -not -path '*/.git*' | grep -v '^.$' | wc -l)
54-
expected=$("${cur}/plist.py" "${cur}/../internal" --ignore '*/.git*')
54+
expected=$("${cur}/plist.py" "${cur}/../internal" --ignore '*/\.git*')
5555
cnt=$(tail -n +2 "${out}" | sed '/^$/d' | wc -l)
5656
[ "${cnt}" != "${expected}" ] && echo "expecting ${expected} lines got ${cnt}" && exit 1
5757

tests-ng/test-long.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ out="${tmpd}/output.txt"
2727
"${bin}" --debug index -a -C -c "${catalog}" "${cur}/../internal" internal
2828
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1
2929

30-
echo ">>> test ls <<<"
30+
echo ">>> test long ls <<<"
3131
"${bin}" --debug ls -l -r -a -c "${catalog}" | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
3232
# shellcheck disable=SC2126
3333
expected=$(find "${cur}/../internal" -not -path '*/.git*' | grep -v '^.$' | wc -l)

tests-ng/test-nested-same-name.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ echo "top-file" > "${topf}"
4040

4141
# index
4242
echo ">>> index dir <<<"
43-
"${bin}" index -a -C --debug -c "${catalog}" --ignore=".git" "${top}" top
43+
"${bin}" index -a -C --debug -c "${catalog}" --ignore="\.git" "${top}" top
4444
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1
4545

4646
# ls

tests-ng/test-tree.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ catalog="${tmpd}/catalog"
2424
out="${tmpd}/output.txt"
2525

2626
# index
27-
"${bin}" index -a -C -c "${catalog}" --ignore=".git" "${cur}/../" gocatcli
27+
"${bin}" index -a -C -c "${catalog}" --ignore="\.git" "${cur}/../" gocatcli
2828
[ ! -e "${catalog}" ] && echo "catalog not created" && exit 1
2929

3030
# tree

tests-ng/test-update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ echo "size:${size} VS exp:${expected}"
5656
[ "${size}" != "${expected}" ] && echo "expecting ${expected} (got ${size})" && exit 1
5757

5858
echo ">>> test re-index with ignore <<<"
59-
"${bin}" index -a -f -C -c "${catalog}" --ignore='*.go' "${tmpd}/internal" internal
59+
"${bin}" index -a -f -C -c "${catalog}" --ignore='tes\.go' "${tmpd}/internal" internal
6060
"${bin}" ls -r -S -a -c "${catalog}" | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
6161
cat_file "${out}"
6262
grep '^.*.go$' "${out}" && (echo ".go files found" && exit 1)

0 commit comments

Comments
 (0)