better logic for valid go.mod files

This commit is contained in:
Jeff Carr 2024-12-13 18:59:40 -06:00
parent 3a83cf030d
commit 22c29b3625
2 changed files with 19 additions and 10 deletions

View File

@ -32,7 +32,7 @@ func isIgnored(file string) (bool, error) {
return false, fmt.Errorf("error checking ignored status: %v", err)
}
func repoOwnsGoMod(repo *gitpb.Repo) (bool, error) {
func repoIgnoresGoMod(repo *gitpb.Repo) (bool, error) {
os.Chdir(repo.FullPath)
file := "go.mod"
@ -44,7 +44,7 @@ func repoOwnsGoMod(repo *gitpb.Repo) (bool, error) {
if tracked {
fmt.Printf("%s %s is tracked by Git.\n", repo.GoPath, file)
return true, nil
return false, nil
}
ignored, err := isIgnored(file)

25
main.go
View File

@ -42,7 +42,7 @@ func main() {
}
} else {
// figure out what directory we are running in
check := findPwdRepo()
check = findPwdRepo()
if check == nil {
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
os.Exit(-1)
@ -103,13 +103,11 @@ func doMain(repo *gitpb.Repo) error {
} else {
log.Info(repo.GoPath, "is valid according to forge")
}
if err := repo.ValidGoSum(); err == nil {
log.Info(repo.GoPath, "go.mod and go.sum are already valid")
return nil
}
// skip restore if --force
if !argv.Force {
// erase the go.mod and go.sum files
eraseGoMod(repo)
cname := repo.GetCurrentBranchName()
// try to restore from the git metadata
if err := repo.AutogenRestore(cname); err != nil {
@ -122,6 +120,12 @@ func doMain(repo *gitpb.Repo) error {
}
}
// double check here. use --force to remake them
if err := repo.ValidGoSum(); err == nil {
log.Info(repo.GoPath, "go.mod and go.sum are already valid")
return nil
}
if repo.GetMasterBranchName() != repo.GetCurrentBranchName() {
log.Info("")
log.Info("You can only run go-mod-clean on a git master branch.")
@ -131,19 +135,22 @@ func doMain(repo *gitpb.Repo) error {
return errors.New(repo.GoPath + " not in the git master branch")
}
ok, err := repoOwnsGoMod(repo)
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 {
return nil
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("skipping read only", repo.GoPath)
return nil
}
// continue and attempt to create go.mod and go.sum
}
if repo.CheckDirty() {
@ -153,7 +160,9 @@ func doMain(repo *gitpb.Repo) error {
return errors.New(repo.GoPath + " git repo is dirty")
}
// re-create go.sum and go.mod
log.Info(repo.GoPath, "GOING TO MAKE NEW go.* FILES")
// actually will re-create go.sum and go.mod now
if err := redoGoMod(repo); err != nil {
return err
}