monochromegane/the_platinum_searcher — pull request #42 by zph (2014-06-24 00:48:47+00:00)
Title: Add git's global gitignore setting

Issue solved: When using `pt`, I expect it to match `ag` behavior. I tried out `pt` and noticed that it does not respect git's global gitignore setting. The behavior in `ag` is that it executes `git config -z --get core.excludesfile` and if there's a global gitignore file, it will use those ignored files as part of it's ignored file list.

Solution: Execute git command to check if there's a global gitignore setting. If so, use that as a filter for ignoring files.

Credit for idea and git commands: https://github.com/ggreer/the_silver_searcher/pull/175.

Design questions/items needing revision:

- I'm relatively new to golang, if there are stylistic changes to better fit this project please let me know.
- I placed the behavior for global gitignore in the same file where `#addHomePtIgnore` is located. Is there a better location?
- I'm uncertain how to test this behavior, but if you like the patch I'll either research it further or take your advice on testing.
- It's currently tied into the same conditional in `find.go` where we check for `self.Option.NoPtIgnore == false`. Should the global ignore have it's own option rather than being tied into `NoPtIgnore`?

Thank you for building this tool! I expect this patch will need revision before being included.

Hopefully this PR can start that conversation.

diff --git a/search/find/find.go b/search/find/find.go
index 82c612c..773a0e9 100644
--- a/search/find/find.go
+++ b/search/find/find.go
@@ -8,6 +8,7 @@ import (
"github.com/monochromegane/the_platinum_searcher/search/pattern"
"io/ioutil"
"os"
+	"os/exec"
"os/user"
"path/filepath"
"strings"
@@ -35,6 +36,7 @@ func (self *Finder) findStream(pattern *pattern.Pattern) {
func (self *Finder) findFile(root string, pattern *pattern.Pattern) {
if self.Option.NoPtIgnore == false {
self.addHomePtIgnore()
+	self.addGlobalGitIgnore()
}
Walk(root, self.Option.Ignore, self.Option.Follow, func(path string, info *FileInfo, depth int, ig ignore.Ignore, err error) (error, ignore.Ignore) {
if info.IsDir() {
@@ -161,6 +163,13 @@ func contains(path string, patterns *[]string) bool {
}

func (self *Finder) addHomePtIgnore() {
+	homeDir := setHomeDir()
+	if homeDir != "" {
+	self.Option.Ignore = append(self.Option.Ignore, ignore.IgnorePatterns(homeDir, []string{".ptignore"})...)
+	}
+}
+
+func setHomeDir() string {
usr, err := user.Current()
var homeDir string
if err == nil {
@@ -169,7 +178,27 @@ func (self *Finder) addHomePtIgnore() {
// Maybe it's cross compilation without cgo support. (darwin, unix)
homeDir = os.Getenv("HOME")
}
+	return homeDir
+}
+
+func (self *Finder) addGlobalGitIgnore() {
+	homeDir := setHomeDir()
if homeDir != "" {
-	self.Option.Ignore = append(self.Option.Ignore, ignore.IgnorePatterns(homeDir, []string{".ptignore"})...)
+	globalIgnore := globalGitIgnore()
+	if globalIgnore != "" {
+	self.Option.Ignore = append(self.Option.Ignore, ignore.IgnorePatterns(homeDir, []string{globalIgnore})...)
+	}
+	}
+}
+
+func globalGitIgnore() string {
+	gitCmd := "git config --get core.excludesfile"
+	file, err := exec.Command("bash", "-c", gitCmd).Output()
+	var filename string
+	if err != nil {
+	filename = ""
+	} else {
+	filename = strings.TrimSpace(filepath.Base(string(file)))
}
+	return strings.TrimSpace(filename)
}