move some code to gitpb

This commit is contained in:
Jeff Carr 2024-12-15 15:51:53 -06:00
parent 8018774c10
commit 6392687a63
2 changed files with 7 additions and 82 deletions

View File

@ -1,62 +0,0 @@
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 repoIgnoresGoMod(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 false, 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
}

27
main.go
View File

@ -145,6 +145,13 @@ func doMain(repo *gitpb.Repo) error {
repo.Run([]string{"git", "notes", "remove"})
}
if err := repo.RepoIgnoresGoMod(); err != nil {
log.Info("never modify go.mod or go.sum for this repo", repo.GoPath)
log.Info("We recommend you add 'go.*' to your .gitignore file and store those files as git tag metadata")
repo.ParseGoSum()
return nil
}
// erase the go.mod and go.sum files
eraseGoMod(repo)
@ -184,26 +191,6 @@ func doMain(repo *gitpb.Repo) error {
}
}
ok, err := repoIgnoresGoMod(repo)
if err != nil {
log.Info(repo.GoPath, "some wierd git error happened. investigate.", err)
return err
}
// if ok, then git owns 'go.mod' and we can't really do anything
// todo: ignore this with --force
if ok {
log.Info(repo.GoPath, "git says it does not own go.mod")
// continue and attempt to create go.mod and go.sum
} else {
if forge.Config.IsReadOnly(repo.GoPath) {
log.Info("you can not push to read only repositories.", repo.GoPath)
log.Info("change your .config/forge/ to indicate you own this repository")
return nil
}
perfect = false
// continue and attempt to create go.mod and go.sum
}
if repo.CheckDirty() {
perfect = false
if argv.Strict {