summaryrefslogtreecommitdiff
path: root/patchset.config.go
blob: 94f52b0d92ad1d66e0a6f8163db1b55b3216268a (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
175
176
177
178
179
180
181
182
183
package forgepb

import (
	"fmt"
	"os"
	"regexp"
	"strings"

	"github.com/google/uuid"
	"go.wit.com/lib/config"
	"go.wit.com/lib/env"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
	"google.golang.org/protobuf/proto"
)

func (f *Forge) InitPatchsets() error {
	f.Sets = NewSets()
	err := config.LoadCacheDir(f.Sets)
	return err
}

func (f *Forge) SavePatchsets() error {
	filename := env.Get("PatchPB")
	regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		log.Info("SavePatchsets() filename open error:", filename, err)
		// fmt.Fprintln(w, "filename open error:", filename, err)
		return err
	}
	defer regfile.Close()

	newpb := proto.Clone(f.Sets).(*Sets)
	if newpb == nil {
		for pset := range f.Sets.IterAll() {
			pset.ShowPatchsets()
		}
		return log.Errorf("SavePatchsets() Clone failed!")
	}

	data, err := newpb.Marshal()
	if err != nil {
		log.Infof("SavePatchset() proto.Marshal() error %v\n", err)
		return err
	}
	log.Infof("SavePatchset() worked (%d) bytes on %d patches\n", len(data), f.Sets.Len())
	regfile.Write(data)
	return nil
}

func cleanSubject(line string) string {
	// Regular expression to remove "Subject:" and "[PATCH...]" patterns
	re := regexp.MustCompile(`(?i)^Subject:\s*(\[\s*PATCH[^\]]*\]\s*)?`)
	cleaned := re.ReplaceAllString(line, "")
	return strings.TrimSpace(cleaned)
}

func (pb *Set) ShowPatchsets() error {
	author := "Author: " + pb.GitAuthorName
	author += " <" + pb.GitAuthorEmail + ">"
	log.Printf("%-16s %s %s %s\n", string(pb.Uuid)[0:8], pb.Name, pb.Comment, author)
	for _, patch := range pb.Patches.Patches {
		comment := cleanSubject(patch.Comment)
		log.Printf("\t%-8s %-50s %-50s\n", string(patch.CommitHash)[0:8], patch.Namespace, comment)
	}
	//	for patch := range pb.IterAll() {
	//		comment := cleanSubject(patch.Comment)
	//		log.Info("\tnew patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment)
	//	}
	return nil
}

// adds a patch. returns true if patch is new
func (f *Forge) AddPatch(patch *Patch) bool {
	if f.findPatch(patch) {
		// log.Info("\talready found!!!!!!!", pset.Uuid, patch.Namespace)
		return false
	}
	if f.AddNewPatch(patch) {
		log.Info("\tnew patch added:", patch.CommitHash, patch.Gs, patch.Gae, patch.Gan)
		return true
	}
	log.Info("\tnew patch failed:", patch.CommitHash, patch.Gs)
	return false
}

// adds a patchset or just the patches
func (f *Forge) AddPatchset(pb *Set) bool {
	var changed bool
	// if the name of the patchset is "forge auto commit"
	// then just add all the patches
	if pb.Name == "forge auto commit" {
		author := "Author: " + pb.GitAuthorName
		author += " <" + pb.GitAuthorEmail + ">"

		fmt.Println(pb.Name, pb.Comment, author)
		for _, patch := range pb.Patches.Patches {
			// log.Info("\tnew patch:", i, patch.CommitHash, patch.Namespace)
			if f.findPatch(patch) {
				// log.Info("\talready found!!!!!!!", pset.Uuid, patch.Namespace)
			} else {
				if f.AddNewPatch(patch) {
					log.Info("\tnew patch added:", patch.CommitHash, pb.Name, pb.Comment, author)
					changed = true
				} else {
					log.Info("\tnew patch failed:", patch.CommitHash, pb.Name, pb.Comment, author)
				}

			}
		}
		return changed
	}

	// if you got here, this patchset was submitted with a name

	// Has this patchset already been submitted?
	for pset := range f.Sets.IterAll() {
		if pset.Uuid == pb.Uuid {
			log.Info("ALREADY ADDED", pset.Uuid, pset.Name)
			return false
		} else {
		}
	}

	f.Sets.Append(pb)
	return true
}

func makeDefaultPatchset() *Set {
	fauto := new(Set)
	fauto.Name = "forge auto commit"
	fauto.Patches = NewPatches()
	fauto.Uuid = uuid.New().String()
	return fauto
}

// adds submitted patches not
// If the patch was actually new, return true
func (f *Forge) AddNewPatch(patch *Patch) bool {
	log.Info("todo: fix this. find where to add the patch")
	return false
	/*
		// ignore patch if it's already here
		if f.findPatch(patch) {
			log.Info("already found patch", patch.CommitHash, patch.Namespace)
			return false
		}
		fauto := f.findAutoPatchset()
		if fauto == nil {
			// should have made the default patchset
			return false
		}
		fauto.Patches.Append(patch)
		return true
	*/
}

// returns true if the patch already exists in the protobuf
func (f *Forge) findPatch(newpatch *Patch) bool {
	// log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace)

	for pset := range f.Sets.IterAll() {
		for _, patch := range pset.Patches.Patches {
			if patch.CommitHash == newpatch.CommitHash {
				// log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace)
				return true
			}

		}
	}

	return false
}

func (f *Forge) IsPatchApplied(newpatch *Patch) (*gitpb.Repo, bool) {
	log.Info("todo: find namespace and repo for patch", newpatch.Filename)
	if f.findPatch(newpatch) {
		log.Info("\tfindPatch() patch was found")
	} else {
		log.Info("\tfindPatch() patch was not found")
	}
	return nil, false
}