summaryrefslogtreecommitdiff
path: root/scanRepoDir.go
blob: c9c6d78e600b16385072413c5311f50c59cb4c23 (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
179
180
181
182
183
184
185
186
187
188
189
package forgepb

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

	"github.com/destel/rill"
	"go.wit.com/lib/cobol"
	"go.wit.com/lib/config"
	"go.wit.com/lib/env"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func reloadCheck(repo *gitpb.Repo) error {
	if err := repo.ReloadCheck(); err != nil {
		log.Infof("%s reload() says err(%v) change(%s)\n", repo.FullPath, err, repo.StateChange)
		return err
	}
	return nil
}

func (f *Forge) Reload() map[string]*RillStats {
	return f.RillRepos(reloadCheck)
}

func (f *Forge) checkNamespace(fullpath string) (*gitpb.Repo, error) {
	if repo := f.Repos.FindByFullPath(fullpath); repo != nil {
		return nil, nil
	}
	repo, err := gitpb.NewRepo(fullpath)
	if err != nil {
		log.Info(fullpath, err)
		return nil, err
	}
	return repo, err
}

func (f *Forge) RescanRepos() error {
	gopath := env.Get("gopath")
	fullpath := env.FullPath(gopath)
	if env.Verbose() {
		log.Info("RescanRepos() running in", fullpath)
	}
	f.scanRepoDir(fullpath)
	err := f.Save()
	// return errors.New("ScanReposByMode() not implemented yet")
	return err
}

// scans for .git/ directories
func (f *Forge) scanRepoDir(path string) error {
	if path == "" {
		return errors.New("ScanRepoDir() got sent blank dir")
	}
	dirs, err := gitDirectoriesNew(path)
	if err != nil {
		return err
	}

	stats := f.RillRepos(reloadCheck)
	for _, stat := range stats {
		if stat.Err == nil {
			continue
		}
		config.SetChanged("repos", true)
	}

	newcount, err := f.rillScanDirsNew(dirs)
	if err != nil {
		log.Info("go src dir problem. exit for now?", err)
		return err
	}
	if newcount != 0 {
		log.Info("forge go src scan found", newcount, "repos")
		config.SetChanged("repos", true)
	}
	return err
}

// rill is awesome. long live rill
// attempt scan with rill
func (f *Forge) rillScanDirsNew(fullpaths []string) (int, error) {
	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(fullpaths, nil)

	// Read users from the API.  // Concurrency = 20
	dirs := rill.Map(ids, cobol.Int(env.Get("RillX")), func(id string) (*gitpb.Repo, error) {
		return f.checkNamespace(id)
	})

	var counter int
	// Activate users.  // Concurrency = 10
	err := rill.ForEach(dirs, cobol.Int(env.Get("RillY")), func(repo *gitpb.Repo) error {
		if repo == nil {
			return nil
		}
		repo = f.Repos.Clone(repo)
		f.VerifyBranchNames(repo)
		if f.Config.IsReadOnly(repo.GetGoPath()) {
			repo.ReadOnly = true
		}
		repo.ReloadCheck()
		counter += 1
		return nil
	})

	return counter, err
}

// doesn't enter the directory any further when it finds a .git/
// not stupid like my old version
func gitDirectoriesNew(srcDir string) ([]string, error) {
	if srcDir == "" {
		log.Info("SCANNING DISALBED")
		return nil, errors.New("SCANNING DISALBED")
	}
	var all []string
	var untracked []string
	var dirs []string
	var trip bool
	err := filepath.WalkDir(srcDir, 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)
		} else {
			_, fname := filepath.Split(path)
			switch fname {
			case "repos.pb":
			case "gowork.pb":
			case "go.work":
			case "go.work.last":
			case "go.work.sum":
			default:
				// log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
				untracked = append(untracked, path)
				trip = true
				var match bool
				for _, adir := range dirs {
					if strings.HasPrefix(path, adir) {
						match = true
					}
				}
				if match {
					// already have this dir
				} else {
					dirs = append(dirs, path)
				}

			}
			return nil
		}

		gitdir := filepath.Join(path, ".git")
		_, err2 := os.Stat(gitdir)
		if !os.IsNotExist(err2) {
			all = append(all, path)
			return filepath.SkipDir
		}
		return nil
	})
	//
	// probably always leave this here forever
	// this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
	// because everything is either checked in or deleted. An important thing to know!
	if trip {
		log.Info("WARNING:")
		log.Info("WARNING: you have files that are untracked by .git repos here")
		log.Info("WARNING:")
		log.Printf("WARNING: you have %d untracked files %d paths\n", len(untracked), len(dirs))
		count := 0
		for _, file := range untracked {
			log.Info(file)
			if count > 7 {
				break
			}
			count += 1
		}
	}
	return all, err
}