gitpb/reloadIsTracked.go

75 lines
2.0 KiB
Go
Raw Normal View History

2024-12-15 12:14:48 -06:00
package gitpb
import (
2024-12-15 15:53:08 -06:00
"errors"
2024-12-15 12:14:48 -06:00
"fmt"
2024-12-15 15:53:08 -06:00
"go.wit.com/log"
2024-12-15 12:14:48 -06:00
)
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
}
2024-12-15 15:53:08 -06:00
// is it a good idea to run go-mod-clean in this repo?
// for now, check if this repo should be ignored
// TODO: go.mod and go.sum should be moved to git tag metadata
func (repo *Repo) RepoIgnoresGoMod() error {
2024-12-17 01:15:31 -06:00
repo.GoInfo.GitIgnoresGoSum = false
2024-12-15 12:14:48 -06:00
file := "go.mod"
if tracked, err := repo.isTracked(file); err != nil {
2024-12-17 06:37:14 -06:00
msg := fmt.Sprintf("%s Error checking if %s tracked: %v\n", repo.GetGoPath(), file, err)
2024-12-15 15:53:08 -06:00
log.Info("gitpb:", msg)
return err
2024-12-15 12:14:48 -06:00
} else {
if tracked {
2024-12-17 06:37:14 -06:00
msg := fmt.Sprintf("%s %s is tracked by Git.\n", repo.GetGoPath(), file)
2024-12-15 15:53:08 -06:00
log.Info("gitpb:", msg)
return errors.New(msg)
2024-12-15 12:14:48 -06:00
}
}
if ignored, err := repo.isIgnored(file); err != nil {
if err != nil {
2024-12-17 06:37:14 -06:00
msg := fmt.Sprintf("%s Error checking if ignored: %v\n", repo.GetGoPath(), err)
2024-12-15 15:53:08 -06:00
log.Info("gitpb:", msg)
return err
2024-12-15 12:14:48 -06:00
}
} else {
if ignored {
2024-12-17 06:37:14 -06:00
fmt.Printf("%s %s is ignored by Git.\n", repo.GetGoPath(), file)
2024-12-17 01:15:31 -06:00
repo.GoInfo.GitIgnoresGoSum = true
2024-12-15 15:53:08 -06:00
return nil
2024-12-15 12:14:48 -06:00
}
}
2024-12-17 06:37:14 -06:00
msg := fmt.Sprintf("%s %s is neither tracked nor ignored by Git.\n", repo.GetGoPath(), file)
2024-12-15 12:14:48 -06:00
// 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
2024-12-15 15:53:08 -06:00
log.Info("gitpb:", msg)
return errors.New(msg)
2024-12-15 12:14:48 -06:00
}