summaryrefslogtreecommitdiff
path: root/repo.common.go
blob: 5e2f75ca005699a539ccd48842023428ceef932d (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
package gitpb

import (
	"errors"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"go.wit.com/lib/config"
	"go.wit.com/log"
)

func (pb *Repos) SaveSafe() error {
	err := config.SavePB(pb)
	if err == nil {
		log.Info(pb.Filename, "Saved with len", pb.Len())
	} else {
		log.Info(pb.Filename, "Saved with len", pb.Len(), "with err", err)
	}
	return err
}

// does this repo build a binary?
func (r *Repo) IsBinary() bool {
	if r.GoInfo != nil {
		return r.GoInfo.GetGoBinary()
	}
	// todo: detect C & other language binary packages
	return false
}

// does this repo build a binary?
func (r *Repo) IsGoPlugin() bool {
	if r.GoInfo != nil {
		return r.GoInfo.GetGoPlugin()
	}
	return false
}

func (repo *Repo) IsProtobuf() bool {
	return repo.GoInfo.GoProtobuf
}

// This returns the list of autogenerated protobuf files
// it assumes any file *.pb.go is autogenerated
//
// this are made from protoc / proto-gen-go
// these packages also use go.wit.com/apps/autogenpb
//
// errors() if a .proto file does not have an autogenerated .pb.go file
func (repo *Repo) ScanProtobuf() (bool, []string, error) {
	fullp, fullc, err := scanForProtobuf(repo.FullPath)
	protos := make(map[string]string)
	protoc := make(map[string]string)
	var anyfound bool = false
	for _, s := range fullp {
		filebase := filepath.Base(s)
		name := strings.TrimSuffix(filebase, ".proto")
		anyfound = true
		protos[name] = s
	}
	// make sure every .proto file has a .pb.go file
	for pname, _ := range protos {
		var found bool = false
		for _, s := range fullc {
			cfilebase := filepath.Base(s)
			cname := strings.TrimSuffix(cfilebase, ".pb.go")
			protoc[cname] = s
			if cname == pname {
				found = true
			}
		}
		if found {
			// log.Info("found ok")
		} else {
			// log.Info("gitpb: IsProtobuf() missing compiled proto file:", pname+".pb.go")
			err = errors.New("compiled file " + pname + ".pb.go missing")
		}
	}

	// assume every .pb.go file is autogenerated
	var allc []string
	for _, testf := range fullc {
		if strings.HasSuffix(testf, ".pb.go") {
			basef := filepath.Base(testf)
			allc = append(allc, basef) // no, not the 3.5" floppy disks
		}
	}
	return anyfound, allc, err
}

// look for any proto files. do not enter directories
// note: good golang libraries are best done in a single directory
func scanForProtobuf(srcDir string) ([]string, []string, error) {
	var protofiles []string
	var compiled []string
	err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			log.Log(WARN, "Error accessing path:", path, err)
			return err
		}

		// ignore the start dir
		if srcDir == path {
			return nil
		}

		if strings.HasSuffix(path, ".proto") {
			//
			protofiles = append(protofiles, path)
		}

		if strings.HasSuffix(path, ".pb.go") {
			compiled = append(compiled, path)
		}

		// don't go into any directories
		if info.IsDir() {
			return filepath.SkipDir
		}
		return nil
	})

	return protofiles, compiled, err
}

func (repo *Repo) GetProtoFiles() ([]string, error) {
	var protofiles []string
	srcDir := repo.GetFullPath()
	err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			log.Log(WARN, "Error accessing path:", path, err)
			return err
		}

		// ignore the start dir
		if srcDir == path {
			return nil
		}

		if strings.HasSuffix(path, ".proto") {
			//
			protofiles = append(protofiles, path)
		}

		if info.IsDir() {
			return filepath.SkipDir
		}
		return nil
	})

	return protofiles, err
}

func (pb *Repos) ActualSort() {
	repoMu.RLock()
	defer repoMu.RUnlock()
	sort.Sort(sortRepoNamespace(pb.Repos))
}

// returns the list of files that can be deleted
// show untracked files
// git ls-files --others
// git ls-files --others --exclude-standard
// git ls-files --others --ignored --exclude-standard
func (repo *Repo) GitDeleteOthers() ([]string, error) {
	var filelist []string
	var err error
	r, err := repo.RunQuiet([]string{"git", "ls-files", "--others"})
	if err != nil {
		return nil, err
	}
	for _, fname := range r.Stdout {
		filelist = append(filelist, filepath.Join(repo.FullPath, fname))
	}
	return filelist, nil
}