summaryrefslogtreecommitdiff
path: root/patchset.config.go
blob: d931299b50d0604a12275bfb5857a9a979f2d767 (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
package forgepb

import (
	"os"
	"path/filepath"
	"regexp"
	"strings"

	"go.wit.com/log"
	"google.golang.org/protobuf/proto"
)

func (f *Forge) LoadPatchsets() error {
	f.Patchsets = NewPatchsets()

	filename := filepath.Join(f.patchDir, "all-patches.pb")

	data, err := os.ReadFile(filename)
	if err != nil {
		return err
	}

	err = f.Patchsets.Unmarshal(data)
	if err != nil {
		log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
		return err
	}
	log.Infof("LoadPatchsets() worked ok %d\n", f.Patchsets.Len())
	return nil
}

func (f *Forge) SavePatchsets() error {
	filename := filepath.Join(f.patchDir, "all-patches.pb")
	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.Patchsets).(*Patchsets)
	if newpb == nil {
		for pset := range f.Patchsets.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\n", len(data))
	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 *Patchset) 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
}