summaryrefslogtreecommitdiff
path: root/scanRepoDir.go
blob: 6459ce0f1866ed27ff34f50a2bbbd503fd1efc44 (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
package forgepb

import (
	"github.com/destel/rill"
	"go.wit.com/lib/config"
	"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 %v\n", repo.FullPath, err)
		return err
	}
	return nil
}

func (f *Forge) TestScan() error {
	f.Repos = gitpb.NewRepos()
	dirs, err := gitDirectoriesNew(f.Config.ReposDir)
	if err != nil {
		return err
	}
	for i, fullpath := range dirs {
		repo, err := gitpb.NewRepo(fullpath)
		if err != nil {
			log.Info("ReAdd() error", fullpath, err)
		}
		log.Info(i, "worked", repo.FullPath)
		repo = f.Repos.Append(repo)
		f.VerifyBranchNames(repo)
		if f.Config.IsReadOnly(repo.GetGoPath()) {
			repo.ReadOnly = true
		}
		repo.ReloadCheck()
		if i > 5 {
			break
		}
	}
	return nil
}

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) ScanRepoDir() error {
	dirs, err := gitDirectoriesNew(f.Config.ReposDir)
	if err != nil {
		return err
	}

	log.Info("doing reload()")
	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, int(f.Config.RillX), func(id string) (*gitpb.Repo, error) {
		return f.checkNamespace(id)
	})

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

	return counter, err
}