better logic for valid go.mod files
This commit is contained in:
parent
3a83cf030d
commit
22c29b3625
|
@ -32,7 +32,7 @@ func isIgnored(file string) (bool, error) {
|
||||||
return false, fmt.Errorf("error checking ignored status: %v", err)
|
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)
|
os.Chdir(repo.FullPath)
|
||||||
file := "go.mod"
|
file := "go.mod"
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func repoOwnsGoMod(repo *gitpb.Repo) (bool, error) {
|
||||||
|
|
||||||
if tracked {
|
if tracked {
|
||||||
fmt.Printf("%s %s is tracked by Git.\n", repo.GoPath, file)
|
fmt.Printf("%s %s is tracked by Git.\n", repo.GoPath, file)
|
||||||
return true, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ignored, err := isIgnored(file)
|
ignored, err := isIgnored(file)
|
||||||
|
|
25
main.go
25
main.go
|
@ -42,7 +42,7 @@ func main() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// figure out what directory we are running in
|
// figure out what directory we are running in
|
||||||
check := findPwdRepo()
|
check = findPwdRepo()
|
||||||
if check == nil {
|
if check == nil {
|
||||||
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
|
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
|
@ -103,13 +103,11 @@ func doMain(repo *gitpb.Repo) error {
|
||||||
} else {
|
} else {
|
||||||
log.Info(repo.GoPath, "is valid according to forge")
|
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
|
// skip restore if --force
|
||||||
if !argv.Force {
|
if !argv.Force {
|
||||||
|
// erase the go.mod and go.sum files
|
||||||
|
eraseGoMod(repo)
|
||||||
cname := repo.GetCurrentBranchName()
|
cname := repo.GetCurrentBranchName()
|
||||||
// try to restore from the git metadata
|
// try to restore from the git metadata
|
||||||
if err := repo.AutogenRestore(cname); err != nil {
|
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() {
|
if repo.GetMasterBranchName() != repo.GetCurrentBranchName() {
|
||||||
log.Info("")
|
log.Info("")
|
||||||
log.Info("You can only run go-mod-clean on a git master branch.")
|
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")
|
return errors.New(repo.GoPath + " not in the git master branch")
|
||||||
}
|
}
|
||||||
|
|
||||||
ok, err := repoOwnsGoMod(repo)
|
ok, err := repoIgnoresGoMod(repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Info(repo.GoPath, "some wierd git error happened. investigate.", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// if ok, then git owns 'go.mod' and we can't really do anything
|
// if ok, then git owns 'go.mod' and we can't really do anything
|
||||||
// todo: ignore this with --force
|
// todo: ignore this with --force
|
||||||
if ok {
|
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 {
|
} else {
|
||||||
if forge.Config.IsReadOnly(repo.GoPath) {
|
if forge.Config.IsReadOnly(repo.GoPath) {
|
||||||
log.Info("skipping read only", repo.GoPath)
|
log.Info("skipping read only", repo.GoPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
// continue and attempt to create go.mod and go.sum
|
||||||
}
|
}
|
||||||
|
|
||||||
if repo.CheckDirty() {
|
if repo.CheckDirty() {
|
||||||
|
@ -153,7 +160,9 @@ func doMain(repo *gitpb.Repo) error {
|
||||||
return errors.New(repo.GoPath + " git repo is dirty")
|
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 {
|
if err := redoGoMod(repo); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue