| 1 |
package gitignore |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/go-git/go-billy/v5" |
| 11 |
"github.com/go-git/go-git/v5/internal/path_util" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/format/config" |
| 13 |
gioutil "github.com/go-git/go-git/v5/utils/ioutil" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
commentPrefix = "#" |
| 18 |
coreSection = "core" |
| 19 |
excludesfile = "excludesfile" |
| 20 |
gitDir = ".git" |
| 21 |
gitignoreFile = ".gitignore" |
| 22 |
gitconfigFile = ".gitconfig" |
| 23 |
systemFile = "/etc/gitconfig" |
| 24 |
infoExcludeFile = gitDir + "/info/exclude" |
| 25 |
) |
| 26 |
|
| 27 |
// readIgnoreFile reads a specific git ignore file. |
| 28 |
func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) { |
| 29 |
|
| 30 |
ignoreFile, _ = path_util.ReplaceTildeWithHome(ignoreFile) |
| 31 |
|
| 32 |
f, err := fs.Open(fs.Join(append(path, ignoreFile)...)) |
| 33 |
if err == nil { |
| 34 |
defer f.Close() |
| 35 |
|
| 36 |
scanner := bufio.NewScanner(f) |
| 37 |
for scanner.Scan() { |
| 38 |
s := scanner.Text() |
| 39 |
if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 { |
| 40 |
ps = append(ps, ParsePattern(s, path)) |
| 41 |
} |
| 42 |
} |
| 43 |
} else if !os.IsNotExist(err) { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
return |
| 48 |
} |
| 49 |
|
| 50 |
// ReadPatterns reads the .git/info/exclude and then the gitignore patterns |
| 51 |
// recursively traversing through the directory structure. The result is in |
| 52 |
// the ascending order of priority (last higher). |
| 53 |
func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) { |
| 54 |
ps, _ = readIgnoreFile(fs, path, infoExcludeFile) |
| 55 |
|
| 56 |
subps, _ := readIgnoreFile(fs, path, gitignoreFile) |
| 57 |
ps = append(ps, subps...) |
| 58 |
|
| 59 |
var fis []os.FileInfo |
| 60 |
fis, err = fs.ReadDir(fs.Join(path...)) |
| 61 |
if err != nil { |
| 62 |
return |
| 63 |
} |
| 64 |
|
| 65 |
for _, fi := range fis { |
| 66 |
if fi.IsDir() && fi.Name() != gitDir { |
| 67 |
if NewMatcher(ps).Match(append(path, fi.Name()), true) { |
| 68 |
continue |
| 69 |
} |
| 70 |
|
| 71 |
var subps []Pattern |
| 72 |
subps, err = ReadPatterns(fs, append(path, fi.Name())) |
| 73 |
if err != nil { |
| 74 |
return |
| 75 |
} |
| 76 |
|
| 77 |
if len(subps) > 0 { |
| 78 |
ps = append(ps, subps...) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
func loadPatterns(fs billy.Filesystem, path string) (ps []Pattern, err error) { |
| 87 |
f, err := fs.Open(path) |
| 88 |
if err != nil { |
| 89 |
if os.IsNotExist(err) { |
| 90 |
return nil, nil |
| 91 |
} |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
|
| 95 |
defer gioutil.CheckClose(f, &err) |
| 96 |
|
| 97 |
b, err := io.ReadAll(f) |
| 98 |
if err != nil { |
| 99 |
return |
| 100 |
} |
| 101 |
|
| 102 |
d := config.NewDecoder(bytes.NewBuffer(b)) |
| 103 |
|
| 104 |
raw := config.New() |
| 105 |
if err = d.Decode(raw); err != nil { |
| 106 |
return |
| 107 |
} |
| 108 |
|
| 109 |
s := raw.Section(coreSection) |
| 110 |
efo := s.Options.Get(excludesfile) |
| 111 |
if efo == "" { |
| 112 |
return nil, nil |
| 113 |
} |
| 114 |
|
| 115 |
ps, err = readIgnoreFile(fs, nil, efo) |
| 116 |
if os.IsNotExist(err) { |
| 117 |
return nil, nil |
| 118 |
} |
| 119 |
|
| 120 |
return |
| 121 |
} |
| 122 |
|
| 123 |
// LoadGlobalPatterns loads gitignore patterns from the gitignore file |
| 124 |
// declared in a user's ~/.gitconfig file. If the ~/.gitconfig file does not |
| 125 |
// exist the function will return nil. If the core.excludesfile property |
| 126 |
// is not declared, the function will return nil. If the file pointed to by |
| 127 |
// the core.excludesfile property does not exist, the function will return nil. |
| 128 |
// |
| 129 |
// The function assumes fs is rooted at the root filesystem. |
| 130 |
func LoadGlobalPatterns(fs billy.Filesystem) (ps []Pattern, err error) { |
| 131 |
home, err := os.UserHomeDir() |
| 132 |
if err != nil { |
| 133 |
return |
| 134 |
} |
| 135 |
|
| 136 |
return loadPatterns(fs, fs.Join(home, gitconfigFile)) |
| 137 |
} |
| 138 |
|
| 139 |
// LoadSystemPatterns loads gitignore patterns from the gitignore file |
| 140 |
// declared in a system's /etc/gitconfig file. If the /etc/gitconfig file does |
| 141 |
// not exist the function will return nil. If the core.excludesfile property |
| 142 |
// is not declared, the function will return nil. If the file pointed to by |
| 143 |
// the core.excludesfile property does not exist, the function will return nil. |
| 144 |
// |
| 145 |
// The function assumes fs is rooted at the root filesystem. |
| 146 |
func LoadSystemPatterns(fs billy.Filesystem) (ps []Pattern, err error) { |
| 147 |
return loadPatterns(fs, systemFile) |
| 148 |
} |