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

import (
	"os"
	"path/filepath"

	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/log"
)

func doPatch() error {
	if argv.Patch.Submit != "" {
		_, err := me.forge.SubmitDevelPatchSet(argv.Patch.Submit)
		if err != nil {
			return err
		}
		return nil
	}

	if argv.Patch.Get != nil {
		return doPatchGet()
	}

	if argv.Patch.List != nil {
		return doPatchList()
	}

	// if no option is given to patch, list out the
	// repos that have patches ready in them
	findReposWithPatches()
	if me.found.Len() == 0 {
		log.Info("you currently have no patches in your user branches")
		return nil
	}
	me.forge.PrintHumanTable(me.found)
	return nil
}

func doPatchList() error {
	psets, err := openPatchsets()
	if err != nil {
		log.Info("Open Patchsets failed", err)
		return err
	}
	log.Info("got psets len", len(psets.Patchsets))
	all := psets.SortByName()
	for all.Scan() {
		pset := all.Next()
		// log.Info("pset name =", pset.Name)
		dumpPatchset(pset)
	}

	//	if err := savePatchsets(psets); err != nil {
	//		return err
	//	}
	return nil
}

func savePatchsets(psets *forgepb.Patchsets) error {
	data, err := psets.Marshal()
	if err != nil {
		log.Info("protobuf.Marshal() failed:", err)
		return err
	}
	fullpath := filepath.Join(me.forge.GetGoSrc(), "patchsets.pb")
	var pfile *os.File
	pfile, err = os.OpenFile(fullpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Info("Patchsets save failed:", err, fullpath)
		return err
	}
	pfile.Write(data)
	pfile.Close()
	return nil
}

func openPatchsets() (*forgepb.Patchsets, error) {
	fullpath := filepath.Join(me.forge.GetGoSrc(), "patchsets.pb")
	data, err := os.ReadFile(fullpath)
	if err != nil {
		log.Info("Patchsets open failed:", err, fullpath)
		return nil, err
	}

	psets := new(forgepb.Patchsets)
	err = psets.Unmarshal(data)
	if err != nil {
		log.Info("Unmarshal patchsets failed", err)
		return nil, err
	}
	return psets, nil
}

// returns bad if patches can not be applied
// logic is not great here but it was a first pass
func dumpPatchset(pset *forgepb.Patchset) bool {

	// don't even bother to continue if we already know it's broken
	if pset.State == "BROKEN" {
		log.Printf("Patchset Name: %-24s Author: %s <%s> IS BAD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
		return false
	} else {
		log.Printf("Patchset Name: %-24s Author: %s <%s> IS GOOD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
	}

	/*
		log.Info("applyPatches() State", pset.State)
		log.Info("applyPatches() COMMENT", pset.Comment)
		log.Info("applyPatches() Branch Name", pset.GetStartBranchName())
		log.Info("applyPatches() Start Hash", pset.GetStartBranchHash())
	*/

	var count int
	var bad int
	all := pset.Patches.SortByFilename()
	for all.Scan() {
		p := all.Next()
		if IsValidPatch(p) {
			// ok
		} else {
			pset.State = "BROKEN"
			bad += 1
		}
		count += 1
	}
	log.Info("pset has", count, "total patches, ", bad, "bad patches")
	if bad == 0 {
		return true
	}
	return false
}

func IsValidPatch(p *forgepb.Patch) bool {
	basepath, filename := filepath.Split(p.Filename)
	repo := me.forge.FindByGoPath(basepath)
	if argv.Verbose {
		log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
	}
	if repo == nil {
		log.Info("can not apply patch! repo not found", basepath, filename)
		return false
	}
	if repo.DevelHash() != p.StartHash {
		log.Info("can not apply patch! devel hash mismatch", basepath, filename)
		return false
	}
	if repo.DevelHash() == p.StartHash {
		log.Info("local devel hash:", repo.DevelHash(), "matches patch hash", p.StartHash, "and can be applied")
	}
	log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
	for _, line := range p.Files {
		log.Info("\t", line)
	}
	return true
}

func doPatchGet() error {
	psets, err := me.forge.GetPatchesets()
	if err != nil {
		log.Info("Get Patchsets failed", err)
		return err
	}
	log.Info("Got Patchsets ok", psets.Uuid)

	log.Info("got psets len", len(psets.Patchsets))
	all := psets.SortByName()
	for all.Scan() {
		pset := all.Next()
		// log.Info("pset name =", pset.Name)
		dumpPatchset(pset)
	}
	return nil
}