summaryrefslogtreecommitdiff
path: root/find.go
blob: d8f2231a320b35f38c5e93ae34b9dcc21e0ea3b7 (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"go.wit.com/lib/protobuf/gitpb"
)

// this populates a slice of protobuf records representing each git repo
// var me.found []*gitpb.Repo
//
// so, it makes a subset of repos that are then used performing actions on
//
// by default, it adds every repo

func (f *FindCmd) findRepos() *gitpb.Repos {
	if f == nil {
		findMine()
		return me.found
	}

	if f.All {
		findAll()
		return me.found
	}

	if f.Private {
		findPrivate()
		return me.found
	}

	if f.Mine {
		findMine()
		return me.found
	}

	if f.Favorites {
		findFavorites()
		return me.found
	}

	if f.Dirty {
		return findDirty()
	}

	if f.User {
		findUser()
		return me.found
	}

	findAll()
	return me.found
}

func findPrivate() {
	for repo := range me.forge.Repos.IterByFullPath() {
		if me.forge.Config.IsPrivate(repo.GetGoPath()) {
			me.found.AppendByGoPath(repo)
		}
	}
}

// finds repos that are writable
func findMine() {
	// log.Printf("get mine %s\n", me.forge.GetGoSrc())
	for repo := range me.forge.Repos.IterByFullPath() {

		if me.forge.Config.IsWritable(repo.GetGoPath()) {
			me.found.AppendByGoPath(repo)
		}
	}
}

// finds repos the user has marked as favorites in the forge .config
func findFavorites() {
	// log.Printf("get favorites %s\n", me.forge.GetGoSrc())
	for repo := range me.forge.Repos.IterByFullPath() {

		if me.forge.Config.IsFavorite(repo.GetGoPath()) {
			me.found.AppendByGoPath(repo)
		}
	}
}

// finds repos that git is reporting as dirty
func findDirty() *gitpb.Repos {
	found := gitpb.NewRepos()
	for repo := range me.forge.Repos.IterByFullPath() {
		if repo.IsDirty() {
			found.AppendByGoPath(repo)
		}
	}
	return found
}

func findAll() {
	for repo := range me.forge.Repos.IterByFullPath() {
		me.found.AppendByGoPath(repo)
	}
}

func findUser() {
	for repo := range me.forge.Repos.IterByFullPath() {
		if repo.GetCurrentBranchName() == repo.GetUserBranchName() {
			me.found.AppendByGoPath(repo)
		}
	}
}

func findPublishable() {
	for repo := range me.forge.Repos.IterByFullPath() {
		if repo.GetTargetVersion() == "" {
			continue
		}
		me.found.AppendByGoPath(repo)
	}
}

func findReposWithPatches() {
	for repo := range me.forge.Repos.IterByFullPath() {
		if repo.GetTargetVersion() != "" {
			// add everything that has a target version set
			me.found.AppendByGoPath(repo)
			continue
		}
		if repo.IsDirty() {
			// always add dirty branches
			me.found.AppendByGoPath(repo)
			continue
		}
		if repo.GetUserVersion() == "" || repo.GetUserVersion() == "uerr" {
			// skip anything without a user branch
			continue
		}
		if repo.GetUserVersion() != repo.GetDevelVersion() {
			me.found.AppendByGoPath(repo)
			continue
		}

		// this is an old test to see if the current 'last tag' is accurate and should be removed
		if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
			continue
		}
		if repo.GetLastTag() != repo.GetMasterVersion() {
			me.found.AppendByGoPath(repo)
			repo.FindLastTag()
			continue
		}
	}
}