summaryrefslogtreecommitdiff
path: root/isTracked.go
blob: 391ecd5c8a2b2e0c0dd23c87a2c967133b2f3e55 (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
package main

import (
	"fmt"
	"os"
	"os/exec"

	"go.wit.com/lib/protobuf/gitpb"
)

func isTracked(file string) (bool, error) {
	cmd := exec.Command("git", "ls-files", "--error-unmatch", file)
	err := cmd.Run()
	if err == nil {
		return true, nil
	}
	if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {
		return false, nil // File not tracked
	}
	return false, fmt.Errorf("error checking tracked status: %v", err)
}

func isIgnored(file string) (bool, error) {
	cmd := exec.Command("git", "check-ignore", "-q", file)
	err := cmd.Run()
	if err == nil {
		return true, nil
	}
	if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {
		return false, nil // File not ignored
	}
	return false, fmt.Errorf("error checking ignored status: %v", err)
}

func repoOwnsGoMod(repo *gitpb.Repo) (bool, error) {
	os.Chdir(repo.FullPath)
	file := "go.mod"

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

	if tracked {
		fmt.Printf("%s %s is tracked by Git.\n", repo.GoPath, file)
		return true, nil
	}

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

	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)
	return false, nil
}