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
|
package repolist
import (
"fmt"
"os"
"sort"
)
type RepoIterator struct {
repos []*RepoRow
index int
}
// NewRepoIterator initializes a new iterator.
func NewRepoIterator(repos []*RepoRow) *RepoIterator {
return &RepoIterator{repos: repos}
}
// Scan moves to the next element and returns false if there are no more repos.
func (it *RepoIterator) Scan() bool {
if it.index >= len(it.repos) {
return false
}
it.index++
return true
}
// Repo returns the current repo.
func (it *RepoIterator) Repo() *RepoRow {
if it.repos[it.index-1] == nil {
for i, d := range it.repos {
fmt.Println("i =", i, d)
}
fmt.Println("len =", len(it.repos))
fmt.Println("repo == nil", it.index, it.index-1)
os.Exit(-1)
}
return it.repos[it.index-1]
}
// Use Scan() in a loop, similar to a while loop
//
// for iterator.Scan() {
// d := iterator.Repo()
// fmt.Println("Repo UUID:", d.Uuid)
// }
func (r *RepoList) ReposAll() *RepoIterator {
repoPointers := r.selectRepoAll()
iterator := NewRepoIterator(repoPointers)
return iterator
}
func (r *RepoList) ReposSortByName() *RepoIterator {
repoPointers := r.selectRepoAll()
sort.Sort(ByName(repoPointers))
iterator := NewRepoIterator(repoPointers)
return iterator
}
func (r *RepoList) UnmergedRepos() *RepoIterator {
repoPointers := r.selectRepoAll()
sort.Sort(ByName(repoPointers))
iterator := NewRepoIterator(repoPointers)
return iterator
}
type ByName []*RepoRow
func (a ByName) Len() int { return len(a) }
func (a ByName) Less(i, j int) bool { return a[i].GoPath() < a[j].GoPath() }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// SelectRepoPointers safely returns a slice of pointers to Repo records.
func (r *RepoList) selectRepoAll() []*RepoRow {
r.RLock()
defer r.RUnlock()
// Create a new slice to hold pointers to each Repo
// repoPointers := make([]*Repo, len(c.E.Repos))
var repoPointers []*RepoRow
for _, repo := range me.allrepos {
if repo == nil {
continue
}
if repo.Status == nil {
continue
}
if !repo.Status.InitOk {
continue
}
repoPointers = append(repoPointers, repo) // Copy pointers for safe iteration
}
return repoPointers
}
// SelectRepoPointers safely returns a slice of pointers to Repo records.
func (r *RepoList) selectUnmergedRepos() []*RepoRow {
r.RLock()
defer r.RUnlock()
// Create a new slice to hold pointers to each Repo
// repoPointers := make([]*Repo, len(c.E.Repos))
var repoPointers []*RepoRow
for _, repo := range me.allrepos {
if repo == nil {
continue
}
if repo.Status == nil {
continue
}
if !repo.Status.InitOk {
continue
}
if repo.ReadOnly() {
continue
}
if repo.State() == "PERFECT" {
continue
}
if repo.Status.IsReleased() {
continue
}
repoPointers = append(repoPointers, repo) // Copy pointers for safe iteration
}
return repoPointers
}
|