Skip to content

Instantly share code, notes, and snippets.

@John-K
Created August 12, 2019 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save John-K/5443ca3f80bfec1f2548f2cb92ea4a50 to your computer and use it in GitHub Desktop.
Save John-K/5443ca3f80bfec1f2548f2cb92ea4a50 to your computer and use it in GitHub Desktop.
go script to minimize gitignore rules for those super hairy repos
// minignore.go
// John Kelley <john@kelley.ca>
// January 2019
// Covered by BSD license
//
// minignore parses all of the rules in your gitignore files and omits those rules which are covered by others
// (including duplicate lines). The output is written to the given path with '.new' appended to the filename
//
// Usage: go run minignore.go .gitignore
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"io/ioutil"
"github.com/sabhiram/go-gitignore"
"strings"
)
func main() {
new_data := bytes.NewBuffer(nil)
lines := make(map[string]bool)
if len(os.Args) != 2 {
fmt.Println("Usage: go run minignore.go <gitignore path>")
return
}
data,err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println("Could not read file:", os.Args[1])
return
}
{
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
lines[scanner.Text()] = true
}
}
scanner1 := bufio.NewScanner(bytes.NewReader(data))
for scanner1.Scan() {
// log.Println("Matching ", scanner1.Text())
filt, err := ignore.CompileIgnoreLines(scanner1.Text())
if err != nil {
log.Fatal(err)
}
scanner2 := bufio.NewScanner(bytes.NewReader(data))
for scanner2.Scan() {
if 0 == strings.Compare(scanner1.Text(), scanner2.Text()) {
continue
}
if filt.MatchesPath(scanner2.Text()) {
log.Printf(" Deleting %s (matches %s)\n", scanner2.Text(), scanner1.Text())
delete(lines, scanner2.Text())
}
}
if err := scanner2.Err(); err != nil {
log.Fatal(err)
}
}
if err := scanner1.Err(); err != nil {
log.Fatal(err)
}
{
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
if lines[scanner.Text()] {
new_data.WriteString(scanner.Text()+"\n")
}
}
}
if new_data.Len() > 0 {
err := ioutil.WriteFile(os.Args[1] + ".new", new_data.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment