summaryrefslogtreecommitdiff
path: root/doPull.go
blob: a2e8026defbf40defb2ca617fdb0f13e9180b2ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"errors"
	"time"

	"go.wit.com/lib/cobol"
	"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() (string, error) {
	var s string
	var err 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 "forge server error", err
		}
		s = log.Sprintf("pull update pb.Len()=%d\n", updatepb.Len())
		return s, err
	}
	if argv.Pull.Check != nil {
		err = doFixUrls()
		return "fixed urls", 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()
		footer := me.forge.PrintPullTable(found)
		return footer, nil
	}

	// below this, you must not be in 'normal' mode
	if me.forge.IsModeNormal() {
		s = "you must check out the devel or master branches"
		return s, errors.New("wrong mode " + me.forge.GetMode())
	}

	if argv.Pull.Force || argv.Force {
		stats := me.forge.RillFuncError(rillPull)
		count := me.forge.RillReload()
		if count != 0 {
			me.forge.Save()
		}
		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)
		if repoerr.Len() > 0 {
			err = errors.New("git pull FAILED on these: " + footer)
		}
		s = "git pull done"
	} else {
		s = "need --force for git pull"
	}
	return s, err

}

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
}