summaryrefslogtreecommitdiff
path: root/submitPatches.go
blob: 337d82aed340ac4de54c445962d1bc5b0bcf0118 (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
package main

import (
	"strconv"
	"strings"

	"go.wit.com/gui"
	"go.wit.com/lib/gadgets"
	"go.wit.com/lib/gui/repostatus"
	"go.wit.com/log"
)

type patchSummary struct {
	grid    *gui.Node
	updateB *gui.Node
	docsB   *gui.Node

	totalOL        *gadgets.OneLiner
	dirtyOL        *gadgets.OneLiner
	readonlyOL     *gadgets.OneLiner
	totalPatchesOL *gadgets.OneLiner

	reason  *gadgets.BasicEntry
	submitB *gui.Node

	allp []*patch
}

func submitPatchesBox(box *gui.Node) *patchSummary {
	s := new(patchSummary)
	group1 := box.NewGroup("Submit Patches Summary")
	s.grid = group1.RawGrid()

	s.updateB = s.grid.NewButton("Update Stats", func() {
		// globalDisplaySetRepoState()
		// reposwin.Toggle()
		s.Update()
	})

	s.updateB = s.grid.NewButton("List Patches", func() {
		for i, p := range s.allp {
			log.Info(i, p.ref, p.rs.String())
		}
	})

	s.updateB = s.grid.NewButton("Check repos are working", func() {
		for _, repo := range me.allrepos {
			log.Info("Check repo here:", repo.String())
			return
		}
	})

	s.grid.NextRow()

	s.totalOL = gadgets.NewOneLiner(s.grid, "Total")
	s.grid.NextRow()

	s.dirtyOL = gadgets.NewOneLiner(s.grid, "dirty")
	s.grid.NextRow()

	s.readonlyOL = gadgets.NewOneLiner(s.grid, "read-only")
	s.grid.NextRow()

	s.totalPatchesOL = gadgets.NewOneLiner(s.grid, "total commits")
	s.grid.NextRow()

	s.reason = gadgets.NewBasicEntry(s.grid, "patch name:")
	s.reason.Custom = func() {
		if s.reason.String() != "" {
			s.submitB.Enable()
		} else {
			s.submitB.Disable()
		}
	}
	s.submitB = s.grid.NewButton("Submit Patches", func() {
		for i, p := range s.allp {
			log.Info(i, p.ref, p.rs.String())
		}
	})
	// disable these until there are not dirty repos
	s.reason.Disable()
	s.submitB.Disable()

	return s
}

func (s *patchSummary) Update() {
	var total, dirty, readonly int
	for _, repo := range me.allrepos {
		total += 1
		if repo.status.CheckDirty() {
			dirty += 1
		}
		if repo.status.ReadOnly() {
			readonly += 1
		}
	}
	s.totalOL.SetText(strconv.Itoa(total) + " repos")
	s.dirtyOL.SetText(strconv.Itoa(dirty) + " repos")
	s.readonlyOL.SetText(strconv.Itoa(readonly) + " repos")

	p, allp := s.GetPatches()
	if s.allp == nil {
		s.allp = make([]*patch, 0, 0)
		s.allp = append(s.allp, allp...)
	}
	if dirty == 0 {
		s.totalPatchesOL.SetText(strconv.Itoa(p) + " patches")
		s.submitB.Enable()
		s.reason.Enable()
	} else {
		s.totalPatchesOL.SetText(strconv.Itoa(p) + " patches + ? dirty")
	}
}

type patch struct {
	ref     string
	comment string
	rs      *repostatus.RepoStatus
}

func (s *patchSummary) GetPatches() (int, []*patch) {
	var patchcount int
	patches := make([]*patch, 0, 0)
	for _, repo := range me.allrepos {
		// git log --oneline devel..jcarr
		userv := repo.status.GetUserVersion()
		develv := repo.status.GetDevelVersion()
		usern := repo.status.GetUserBranchName()
		develn := repo.status.GetDevelBranchName()
		if userv == develv {
			// log.Info("skipping unchanged repo", repo.String())
		} else {
			// log.Info("repo userv, develv", userv, develv)
			gitcmd := []string{"git", "log", "--oneline", develn + ".." + usern}
			// log.Info("Run:", gitcmd)
			err, output := repo.status.RunCmd(gitcmd)
			if err == nil {
				// patches := strings.Split(output, "\n")
				for _, line := range strings.Split(output, "\n") {
					parts := strings.Split(line, " ")
					newp := new(patch)
					newp.rs = repo.status
					newp.ref = parts[0]
					newp.comment = strings.Join(parts[1:], " ")
					log.Info("patch:", line, newp.rs.String())
					patchcount += 1
					patches = append(patches, newp)
				}
			} else {
				log.Info("git failed err=", err)
			}
		}
	}
	return patchcount, patches
}