summaryrefslogtreecommitdiff
path: root/doList.go
blob: 350d15d943fafd67f1f5b8a275c867ac64e1500c (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
package main

import (
	"fmt"

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

func doList() error {
	log.Info("do list here")

	me.all = forgepb.NewPatchsets()

	/*
		err := filepath.WalkDir("/var/lib/forged/patchset", func(path string, d os.DirEntry, err error) error {
			if err != nil {
				// Handle possible errors, like permission issues
				fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
				return err
			}

			if d.IsDir() {
				// log.Info("path is dir", path)
				return nil
			} else {
				_, fname := filepath.Split(path)
				log.Info("found", fname, path)
				return nil
			}

			return nil
		})
	*/
	for pset := range me.all.IterAll() {
		showPatchsets(pset)
	}
	return nil
}

func showPatchsets(pb *forgepb.Patchset) error {
	author := "Author: " + pb.GitAuthorName
	author += " <" + pb.GitAuthorEmail + ">"

	// author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
	// author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
	fmt.Println(pb.Name, pb.Comment, author)
	for i, patches := range pb.Patches.Patches {
		log.Info("\tnew patches:", i, patches.CommitHash, patches.Namespace)
	}
	/*
		for patch := range pb.IterAll() {
			comment := cleanSubject(patch.Comment)
			log.Info("\tnew patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment)
		}
	*/
	return nil
}

/*
// adds submitted patches not specifically assigned to a patchset
// to the generic patchset called "forge auto commit"
func addRandomPatch(patch *forgepb.Patch) {
	for pset := range me.all.IterAll() {
		if pset.Name == "forge auto commit" {
			newpb := proto.Clone(patch).(*forgepb.Patch)
			if newpb != nil {
				pset.Patches.Append(newpb)
			}
		}
	}
	log.Warn("patchset.Name == 'forge auto commit' could not be found so the patch in", patch.Namespace, "could not be added")
}

func addPatchset(filename string, pb *forgepb.Patchset) {
	if pb.Name == "forge auto commit" {
		author := "Author: " + pb.GitAuthorName
		author += " <" + pb.GitAuthorEmail + ">"

		// author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
		// author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
		fmt.Println(filename, pb.Name, pb.Comment, author)
		for _, patch := range pb.Patches.Patches {
			// log.Info("\tnew patch:", i, patch.CommitHash, patch.Namespace)
			if findPatch(patch) {
				// log.Info("\talready found!!!!!!!", pset.Uuid, patch.Namespace)
			} else {
				log.Info("\tnew patch:", filename, pb.Name, pb.Comment, author)
			}
		}
		// add each of the patches to the general pool
	} else {
		// Clone() this protobuf into me.all
		var newpb *forgepb.Patchset
		newpb = proto.Clone(pb).(*forgepb.Patchset)
		if newpb != nil {
			me.all.Append(newpb)
		}
	}
}
*/

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

	for pset := range me.all.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 mergePatchsets() {
	dirname := filepath.Join(LIBDIR, "patchset/")
	// Open the directory
	entries, err := os.ReadDir(dirname)
	if err != nil {
		fmt.Printf("Error reading directory: %v\n", err)
		return
	}

	// Iterate through the directory entries
	for _, entry := range entries {
		// Check if the entry is a file and matches the *.pb pattern
		if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" {
			bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name()))
			if err != nil {
				fmt.Println(entry.Name(), err)
				continue
			}
			var p *forgepb.Patchset
			p = new(forgepb.Patchset)
			err = p.Unmarshal(bytes)
			if err != nil {
				fmt.Println(entry.Name(), err)
				continue
			}
			addPatchset(entry.Name(), p)
		}
	}
}
*/