summaryrefslogtreecommitdiff
path: root/doRepos.go
blob: f10300d3bea4a5f59a033b5782518f4417ce826b (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
package main

import (
	"os"
	"time"

	"go.wit.com/lib/config"
	"go.wit.com/lib/env"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
	"google.golang.org/protobuf/proto"
)

func doRepos() error {
	if argv.Repos.List != nil {
		footer := me.forge.Repos.PrintForgedTable()
		log.Info("footer:", footer)
		// me.forge.PrintPullTable(me.forge.Repos)
		// return doReposList()
		return nil
	}

	if argv.Repos.Devel != nil {
		found := gitpb.NewRepos()
		for repo := range me.forge.Repos.IterAll() {
			if repo.Tags.Devel == nil {
				continue
			}
			found.Append(repo)
		}
		footer := found.PrintForgedTable()
		log.Info("footer:", footer)
		return nil
	}

	if argv.Repos.Reload != nil {
		var count int
		for repo := range me.forge.Repos.IterAll() {
			if err := repo.ReloadCheck(); err != nil {
				count += 1
			}
		}
		log.Info("repos reloaded:", count)
		if count > 0 {
			me.forge.Save()
		}
		return nil
	}

	if argv.Repos.Scan != nil {
		log.Infof("start repos scan repos.Len()=%d %s\n", me.forge.Repos.Len(), env.Get("ReposDir"))
		_, err := scanForgedDir(env.Get("ReposDir"))
		me.forge.Save()
		return err
	}

	if argv.Repos.Fix != nil {
		for repo := range me.forge.Repos.IterAll() {
			if repo.GetMasterBranchName() == "" {
				me.forge.VerifyBranchNames(repo)
				log.Info("ABNORMAL: master branch name was blank in", repo.GetFullPath())
			}
			if repo.Tags == nil {
				log.Infof("%s Tags == nil\n", repo.GetFullPath())
				continue
			}
			if repo.Tags.Master == nil {
				if found := repo.GetRemoteTag(repo.GetMasterBranchName()); found != nil {
					// log.Info("found master tag    ", repo.FullPath, found)
					repo.Tags.Master = proto.Clone(found).(*gitpb.GitTag)
					config.SetChanged("repos", true)
				} else {
					log.Info("not found master tag (Reload() ?)", repo.FullPath)
				}
				continue
			}
			if repo.Tags.Devel == nil {
				if repo.GetDevelBranchName() == "" {
					repo.SetDevelBranchName("devel")
				}
				if found := repo.GetRemoteTag(repo.GetDevelBranchName()); found != nil {
					log.Info("found devel tag    ", repo.FullPath, found)
					repo.Tags.Devel = proto.Clone(found).(*gitpb.GitTag)
					config.SetChanged("repos", true)
				} else {
					// log.Info("not found devel tag (Reload() ?)", repo.FullPath)
				}
				continue
			}
		}
		me.forge.Save()
		/*
			count := me.forge.RillReload()
			if count != 0 {
				me.forge.Save()
				log.Infof("%d repos changed\n", count)
				return nil
			}
			log.Infof("no repos changed\n")
		*/
		return nil
	}

	if argv.Repos.Pull != nil {
		var count int
		for repo := range me.forge.Repos.IterAll() {
			dur := repo.GitPullAge()
			if dur < time.Hour {
				continue
			}
			count += 1
			log.Infof("%s %s git pull\n", shell.FormatDuration(dur), repo.FullPath)
			if stats := repo.RunRealtimeVerbose([]string{"git", "fetch", "origin"}); stats.Error != nil {
				log.Infof("%s %s failed %v\n", shell.FormatDuration(dur), repo.FullPath, stats.Error)
			}
			/*
				if _, err := repo.GitPull(); err != nil {
					log.Infof("%s %s failed %v\n", shell.FormatDuration(dur), repo.FullPath, err)
					repo.RunVerbose([]string{"git", "remote", "prune", "origin"})
				}
			*/
			if count > 70 {
				break
			}
		}
		if count != 0 {
			me.forge.Save()
		}
		return nil
	}

	log.Infof("Repos len=%d\n", me.forge.Repos.Len())
	if me.forge.Repos.Len() < 2 {
		return nil
	}
	found := gitpb.NewRepos()
	found.Append(me.forge.Repos.Repos[0])
	found.Append(me.forge.Repos.Repos[1])
	footer := found.PrintForgedTable()
	log.Info(footer)
	return nil
}

func verifyForged() {
	var changed bool = false
	for repo := range me.forge.Repos.IterAll() {
		// check to see if 'git clone' has already been run
		_, err := os.Stat(repo.FullPath)
		if os.IsNotExist(err) {
			log.Info("repo no longer exists", repo.FullPath, err)
			me.forge.Repos.Delete(repo)
			changed = true
			continue
		}
		if err != nil {
			log.Info("repo has some other error", repo.FullPath, err)
			continue
		}
		// repo is fine
	}
	if changed {
		me.forge.Save()
	}
}

func tryGitClone(repo *gitpb.Repo, dir string) error {
	if err := forgepb.RunGitClone("git.clone", dir, "https://"+repo.Namespace); err == nil {
		return nil
	}
	giturl, err := forgepb.FindGoImport("https://" + repo.Namespace)
	if err == nil {
		log.Info("TRY THIS INSTEAD!!!!", giturl)
		if err := forgepb.RunGitClone("git.clone", dir, giturl); err != nil {
			log.Info("git clone still failed", giturl, err)
			return err
		}
		return nil
	}
	log.Info("git clone failed", err)
	return err
}