summaryrefslogtreecommitdiff
path: root/isTracked.go
blob: 2fc8ddfca8f7b31113701e12f33ae1608540f236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gitpb

import (
	"fmt"
)

func (repo *Repo) isTracked(file string) (bool, error) {
	cmd := []string{"git", "ls-files", "--error-unmatch", file}
	result := repo.Run(cmd)
	if result.Error != nil {
		return false, result.Error
	}
	if result.Exit != 0 {
		return false, nil
	}
	return true, nil
}

func (repo *Repo) isIgnored(file string) (bool, error) {
	cmd := []string{"git", "check-ignore", "-q", file}
	result := repo.Run(cmd)
	if result.Error != nil {
		return false, result.Error
	}
	if result.Exit == 0 {
		// exit with 0 means the file is ignored
		return true, nil
	}
	// non-zero exit means the file is not ignored
	return false, nil
}

func (repo *Repo) RepoIgnoresGoMod() (bool, error) {
	file := "go.mod"
	if tracked, err := repo.isTracked(file); err != nil {
		fmt.Printf("%s Error checking if %s tracked: %v\n", repo.GoPath, file, err)
		return false, err
	} else {
		if tracked {
			fmt.Printf("%s %s is tracked by Git.\n", repo.GoPath, file)
			return false, nil
		}
	}

	if ignored, err := repo.isIgnored(file); err != nil {
		if err != nil {
			fmt.Printf("%s Error checking if ignored: %v\n", repo.GoPath, err)
			return false, err
		}
	} else {

		if ignored {
			fmt.Printf("%s %s is ignored by Git.\n", repo.GoPath, file)
			return true, nil
		}
	}
	fmt.Printf("%s %s is neither tracked nor ignored by Git.\n", repo.GoPath, file)
	// this means, if you make a go.mod file, it'll add it to the repo to be tracked
	// so you need to either add it to .gitignore (this is what should happen)
	// or accept you want an auto-generated file to put endless garbage in your git repo
	// this obviously exposes my opinion on this subject matter
	return false, nil
}