summaryrefslogtreecommitdiff
path: root/prepareRelease.go
blob: e56cda1b9ed38a37382b3cfe312c61e56475b7cb (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
package main

import (
	"errors"
	"os"
	"path/filepath"
	"time"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func forceReleaseVersion(repo *gitpb.Repo) {
	if argv.Minor {
		// if v1.2.3 change to v.1.3.0
		repo.IncrementTargetMinor()
	} else {
		// if v1.2.3 change to v.1.2.4
		repo.IncrementTargetRevision()
	}
	me.forge.SetConfigSave(true)
}

func checkpkgcache(repo *gitpb.Repo) error {
	homedir, err := os.UserHomeDir()
	if err != nil {
		return err
	}
	rver := repo.GetLastTag()
	if rver == "" {
		return errors.New("could not get master version")
	}
	moddir := filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver)
	if shell.IsDir(moddir) {
		return nil
	}

	getpath := repo.GetGoPath() + "@" + repo.GetLastTag()
	_, err = me.startRepo.RunVerboseOnError([]string{"go", "get", getpath})
	return err
}

func rillRestore(repo *gitpb.Repo) error {
	if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
		return nil
	}
	if me.forge.Config.IsPrivate(repo.GetGoPath()) {
		return nil
	}
	if err := checkpkgcache(repo); err != nil {
		return err
	}
	_, err := repo.RunStrictNew([]string{"go-mod-clean", "--restore"})
	if err != nil {
		log.Info("go-mod-clean --restore failed", repo.GetGoPath(), err)
		return err
	}
	return nil
}

func rePrepareRelease() {
	// reload the config
	me.forge = forgepb.Init()
	me.found = new(gitpb.Repos)

	now := time.Now()
	me.forge.RillFuncError(rillRestore)
	log.Printf("showRestore() (%d total repos) took:%s\n", me.forge.Repos.Len(), shell.FormatDuration(time.Since(now)))

	all := me.forge.Repos.SortByFullPath()
	for all.Scan() {
		check := all.Next()

		if alreadyDone(check) {
			// means it was already published
			// protects against logic errors that might result
			// in an infinite loop
			log.Info("WARNING already done", check.GetGoPath())
			log.Info("WARNING already done", check.GetGoPath())
			log.Info("WARNING already done", check.GetGoPath())
			continue
		}

		if me.forge.Config.IsReadOnly(check.GetGoPath()) {
			// can't release readonly repos
			continue
		}
		// if master != lastTag, always increment
		master := check.GetMasterVersion()
		lastTag := check.GetLastTag()
		if master != lastTag {
			newmhash := check.GetTagHash(master)
			oldlhash := check.GetTagHash(lastTag)
			if newmhash == oldlhash {
				// they are actually equal
				continue
			}
			b1 := check.CountDiffObjects(oldlhash, newmhash)
			b2 := check.CountDiffObjects(newmhash, oldlhash)
			if b1 != 0 {
				log.Printf("HASH ERROR       %-50s tag %s  < %s\n", check.GetGoPath(), newmhash, oldlhash)
				log.Info("old vs new count", b1, b2, "git merge", oldlhash)
			}

			// if b1 == 0 && b2 == 0 {
			if gitpb.IsGoTagVersionGreater(lastTag, master) {
				// this function is not right really. the hash error above should catch it correctly
				// log.Printf("PROBABLY NOT NEE %-50s tag %s  < %s\n", check.GetGoPath(), lastTag, master)
			}
			log.Printf("NEED RELEASE FOR %-50s tag %s != %s\n", check.GetGoPath(), master, lastTag)
			forceReleaseVersion(check)
			me.found.AppendByGoPath(check)
			continue
		}

		if argv.Quick != nil {
			// if argv has 'quick' don't do anything
			// that doesn't actually have a patch
			if master == lastTag {
				continue
			}
		}

		if argv.Protobuf && check.GetRepoType() == "protobuf" {
			log.Printf("NEED RELEASE FOR %s err: %v\n", check.GetGoPath(), "because --protobuf")
			// if --protobuf, this will force upgrade each one
			forceReleaseVersion(check)
			me.found.AppendByGoPath(check)
			continue
		}

		// if the repo is a go binary or plugin for a new release for
		// any library version change
		//		if check.GetRepoType() == "binary" || check.GetRepoType() == "plugin" {
		// check if the package dependancies changed, if so, re-publish
		if err := me.forge.FinalGoDepsCheckOk(check, false); err == nil {
			// log.Printf("go.sum is perfect! %s\n", check.GetGoPath())
			continue
		} else {
			log.Printf("NEED RELEASE FOR %-50s err: %v\n", check.GetGoPath(), err)
			forceReleaseVersion(check)
			me.found.AppendByGoPath(check)
		}

	}
	me.forge.PrintHumanTable(me.found)
}

func printDone() {
	for _, gopath := range me.done {
		log.Info("printDone() THESE WERE PUBLISHED", gopath)
	}
	log.Sleep(1)
}

func alreadyDone(repo *gitpb.Repo) bool {
	for _, gopath := range me.done {
		// log.Info("WARNING already done", gopath, repo.GetGoPath())
		// log.Info("WARNING already done", gopath, repo.GetGoPath())
		// log.Info("WARNING already done", gopath, repo.GetGoPath())
		if repo.GetGoPath() == gopath {
			log.Info("FOUND. RETURN TRUE. already done", gopath, repo.GetGoPath())
			return true
		}
	}
	return false
}