// Copyright 2017-2025 WIT.COM Inc. All rights reserved. // Use of this source code is governed by the GPL 3.0 package main import ( "time" "go.wit.com/lib/cobol" "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) // returns true if 'git pull' should be run func needToUpdateRepo(repo *gitpb.Repo) (*gitpb.Repo, error) { if repo.Tags == nil { return nil, nil } if repo.Tags.Master == nil { return nil, nil } found := me.forge.Repos.FindByNamespace(repo.Namespace) if found == nil { return nil, nil } if found.Tags == nil { return nil, nil } if found.Tags.Master == nil { return nil, nil } newtime := repo.Tags.Master.Creatordate.AsTime() ourtime := found.Tags.Master.Creatordate.AsTime() dur := newtime.Sub(ourtime) if dur < time.Minute { return nil, nil } log.Infof("checking for updates %s %s\n", cobol.FormatDuration(dur), found.Namespace) /* dur := time.Since(repo.Tags.Master.Authordate.AsTime()) when := repo.Tags.Master.Creatordate.AsTime() dur = time.Since(when) log.Infof("stuff %s age=%s %v\n", repo.Namespace, cobol.FormatDuration(dur), when) */ return found, nil } // is every repo on the devel branch? func doPull() error { if argv.Pull.Update != nil { submit := me.forge.PrepareCheckRepos() updatepb, regPB, err := submit.HttpPost(myServer(), "updateURL") if err != nil { log.Info("err =", err) } if regPB == nil { log.Info("regPB==nil") } if updatepb == nil { log.Info("server sent nil back") return err } log.Infof("pull update pb.Len()=%d\n", updatepb.Len()) return nil } if argv.Pull.Check != nil { err := doFixUrls() return err } if argv.Pull.List != nil { found := gitpb.NewRepos() var count int for { if count > 10 { break } count += 1 found.Append(me.forge.Repos.Repos[count]) } found.SortNamespace() me.forge.PrintPullTable(found) return nil } // below this, you must not be in 'normal' mode if me.forge.Config.Mode == forgepb.ForgeMode_NORMAL { log.Info("you must check out the devel or master branches") return nil } if argv.Pull.Force || argv.Force { stats := me.forge.RillFuncError(rillPull) count := me.forge.RillReload() if count != 0 { me.forge.ConfigSave() } repoerr := gitpb.NewRepos() for path, stat := range stats { if stat.Err == nil { continue } repo := me.forge.Repos.FindByFullPath(path) if repo == nil { log.Info("path deleted while running?", path) continue } r := repoerr.Clone(repo) r.State = "git pull failed" + repo.State dur := stat.End.Sub(stat.Start) if dur > 1*time.Second { msg := log.Sprintf("pull time (%s)", cobol.FormatDuration(dur)) r.State = msg + repo.State } } repoerr.SortNamespace() footer := me.forge.PrintPullTable(repoerr) log.Info("git pull FAILED on these:", footer) me.sh.GoodExit("git pull done") } log.Info("do a pull check here?") return nil } func findGitTag(repo *gitpb.Repo, hash string) *gitpb.GitTag { for _, tag := range repo.Tags.GitTags { if tag.Hash == hash { return tag } // log.Info(i, tag.Hash, tag.Refname, tag) } for i, tag := range repo.Tags.GitTags { log.Info(hash, i, tag) } okExit("") return nil } func rillPull(repo *gitpb.Repo) error { if repo.IsDirty() { // never do dirty repos return nil } t, _ := repo.LastGitPull() if time.Since(t) < time.Minute*10 && !argv.Force { if argv.Verbose { log.Info(repo.GetFullPath(), "git pulled too recently", cobol.FormatDuration(time.Since(t))) } return nil } if err := repo.RunVerbose([]string{"git", "fetch", "origin", "--prune"}); err != nil { return err } cur := repo.GetCurrentBranchName() if !repo.IsBranchRemote(cur) { if argv.Verbose { log.Info(repo.GetFullPath(), "branch is not remote", cur) } return nil } if err := repo.RunVerbose([]string{"git", "pull"}); err != nil { return err } return nil }