File fix-gitignore-bypass.patch of Package glow

From f2fc75e04cb80239cb499be19b74484f039f4e2f Mon Sep 17 00:00:00 2001
From: aitva <aitva@pm.me>
Date: Fri, 26 May 2023 15:00:57 +0200
Subject: [PATCH] fix: --all bypass .gitignore rules (#285)

---
 ui/filesearch.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 ui/ui.go         | 13 ++++----
 2 files changed, 93 insertions(+), 6 deletions(-)
 create mode 100644 ui/filesearch.go

diff --git a/ui/filesearch.go b/ui/filesearch.go
new file mode 100644
index 00000000..4dd86c0e
--- /dev/null
+++ b/ui/filesearch.go
@@ -0,0 +1,86 @@
+package ui
+
+import (
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/muesli/gitcha"
+)
+
+type searchResult struct {
+	Path string
+	Info os.FileInfo
+}
+
+// findFilesExcept search a folder returns the files matching the pattern in
+// list. It excludes files matching the rules from .gitignore or pattern in
+// ignorePatterns.
+func findFilesExcept(path string, list []string, ignorePatterns []string) (chan searchResult, error) {
+	tmpCh, err := gitcha.FindFilesExcept(path, list, ignorePatterns)
+	if err != nil {
+		return nil, err
+	}
+
+	ch := make(chan searchResult)
+	go func() {
+		for tmp := range tmpCh {
+			ch <- searchResult{
+				Path: tmp.Path,
+				Info: tmp.Info,
+			}
+		}
+	}()
+
+	return ch, nil
+}
+
+// findFiles search a folder recursively and returns files matching the pattern
+// in list.
+func findFiles(path string, list []string) (chan searchResult, error) {
+	path, err := filepath.Abs(path)
+	if err != nil {
+		return nil, err
+	}
+	path, err = filepath.EvalSymlinks(path)
+	if err != nil {
+		return nil, err
+	}
+	st, err := os.Stat(path)
+	if err != nil {
+		return nil, err
+	}
+	if !st.IsDir() {
+		return nil, err
+	}
+
+	ch := make(chan searchResult)
+	go func() {
+		defer close(ch)
+
+		_ = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
+			for _, v := range list {
+				matched := strings.EqualFold(filepath.Base(path), v)
+				if !matched {
+					matched, _ = filepath.Match(strings.ToLower(v), strings.ToLower(filepath.Base(path)))
+				}
+
+				if matched {
+					res, err := filepath.Abs(path)
+					if err == nil {
+						ch <- searchResult{
+							Path: res,
+							Info: info,
+						}
+					}
+
+					// only match each path once
+					return nil
+				}
+			}
+			return nil
+		})
+	}()
+
+	return ch, nil
+}
diff --git a/ui/ui.go b/ui/ui.go
index df7e37f8..67387c4e 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -72,7 +72,7 @@ type (
 	keygenSuccessMsg       struct{}
 	initLocalFileSearchMsg struct {
 		cwd string
-		ch  chan gitcha.SearchResult
+		ch  chan searchResult
 	}
 )
 
@@ -176,7 +176,7 @@ type model struct {
 
 	// Channel that receives paths to local markdown files
 	// (via the github.com/muesli/gitcha package)
-	localFileFinder chan gitcha.SearchResult
+	localFileFinder chan searchResult
 }
 
 // unloadDocument unloads a document from the pager. Note that while this
@@ -505,12 +505,13 @@ func findLocalFiles(m model) tea.Cmd {
 			log.Println("local directory is:", cwd)
 		}
 
-		var ignore []string
-		if !m.common.cfg.ShowAllFiles {
-			ignore = ignorePatterns(m)
+		var ch chan searchResult
+		if m.common.cfg.ShowAllFiles {
+			ch, err = findFiles(cwd, markdownExtensions)
+		} else {
+			ch, err = findFilesExcept(cwd, markdownExtensions, ignorePatterns(m))
 		}
 
-		ch, err := gitcha.FindFilesExcept(cwd, markdownExtensions, ignore)
 		if err != nil {
 			if debug {
 				log.Println("error finding local files:", err)
openSUSE Build Service is sponsored by