start skipping repos with tracked go.* files

This commit is contained in:
Jeff Carr 2024-12-13 17:13:29 -06:00
parent 8c68cff762
commit 3a83cf030d
2 changed files with 84 additions and 0 deletions

62
isTracked.go Normal file
View File

@ -0,0 +1,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
}

22
main.go
View File

@ -131,6 +131,28 @@ func doMain(repo *gitpb.Repo) error {
return errors.New(repo.GoPath + " not in the git master branch")
}
ok, err := repoOwnsGoMod(repo)
if err != nil {
return err
}
// if ok, then git owns 'go.mod' and we can't really do anything
// todo: ignore this with --force
if ok {
return nil
} else {
if forge.Config.IsReadOnly(repo.GoPath) {
log.Info("skipping read only", repo.GoPath)
return nil
}
}
if repo.CheckDirty() {
log.Info("")
log.Info("You can not run this on dirty branches.")
log.Info("")
return errors.New(repo.GoPath + " git repo is dirty")
}
// re-create go.sum and go.mod
if err := redoGoMod(repo); err != nil {
return err