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

import (
	"fmt"
	"os"

	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func list() {
	log.DaemonMode(true)
	if argv.ListConf {
		me.forge.ConfigPrintTable()
		os.Exit(0)
	}

	if argv.List {
		repos := me.forge.Repos.SortByGoPath()
		for repos.Scan() {
			repo := repos.Next()
			verifyPrint(repo)
		}
		os.Exit(0)
	}

	if argv.GetMine {
		log.Printf("get mine %s", me.forge.GetGoSrc())
		os.Exit(0)
	}

	if argv.GetFav {
		log.Printf("get favorites")
		os.Exit(0)
	}
}

func verifyPrint(repo *gitpb.Repo) {
	var end string
	if repo.CheckDirty() {
		end += "(dirty) "
	}
	s := make(map[string]string)
	if !verify(repo, s) {
		log.Info("going to delete", repo.GoPath)
		if argv.Real {
			me.forge.Repos.DeleteByGoPath(repo.GetGoPath())
			me.forge.Repos.ConfigSave()
		} else {
			log.Info("need argv --real to delete", repo.GoPath)
			os.Exit(0)
		}
	}
	if me.forge.IsReadOnly(repo) && !argv.ReadOnly {
		if repo.ReadOnly {
			return
		}
		log.Info("readonly flag on repo is wrong", repo.GoPath)
		return
	}
	start := fmt.Sprintf("%-40s %-8s %-20s %-20s %-20s", s["gopath"], s["rtype"], s["cver"], s["mver"], s["cver"])
	if s["url"] != "" {
		end += "(" + s["url"] + ") "
	}
	if repo.ReadOnly {
		end += "(readonly) "
	}
	// end += fmt.Sprintf("(%s,%s,%s,%s) ", s["mname"], s["dname"], s["uname"], s["cname"])
	log.Info(start, end)
}

func verify(repo *gitpb.Repo, s map[string]string) bool {
	if !repo.IsValid() {
		return false
	}
	s["gopath"] = repo.GetGoPath()
	s["rtype"] = repo.RepoType()

	s["mname"] = repo.GetMasterBranchName()
	if s["mname"] == "" {
		log.Info("verify() no master branch name", repo.GoPath)
		s["mver"] = repo.GetMasterVersion()
		return false
	}
	// only verify the master branch name with read-only repos
	if me.forge.IsReadOnly(repo) {
		s["mver"] = repo.GetMasterVersion()
		return true
	}

	s["dname"] = repo.GetDevelBranchName()
	if s["dname"] == "" {
		log.Info("verify() no devel branch name", repo.GoPath)
		return false
	}
	s["uname"] = repo.GetUserBranchName()
	if s["uname"] == "" {
		log.Info("verify() no user branch name", repo.GoPath)
		return false
	}
	s["cname"] = repo.GetCurrentBranchName()

	s["mver"] = repo.GetMasterVersion()
	if s["mver"] == "" {
		log.Info("verify() no master branch name", repo.GoPath, repo.GetMasterBranchName())
		return false
	}
	s["dver"] = repo.GetDevelVersion()
	if s["dver"] == "" {
		log.Info("verify() no devel branch name", repo.GoPath, repo.GetDevelBranchName())
		return false
	}
	s["uver"] = repo.GetUserVersion()
	if s["uver"] == "" {
		log.Info("verify() no user branch name", repo.GoPath, repo.GetUserBranchName())
		return false
	}
	s["cver"] = repo.GetCurrentBranchVersion()
	s["url"] = repo.URL

	return true
}