summaryrefslogtreecommitdiff
path: root/new.go
blob: cd23bbec5f98a0b52e62a25a4f1343fe14b15c0f (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
package repostatus

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

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

var windowMap map[string]*RepoStatus

func init() {
	windowMap = make(map[string]*RepoStatus)
}

// deprecate this
func ListAllOld() {
	for path, rs := range windowMap {
		log.Warn(rs.GetMasterVersion(), path)
	}
}

// returns the object for the path
// deprecate this
func FindPathOld(path string) *RepoStatus {
	if windowMap[path] == nil {
		log.Log(INFO, "FindPath() not initialized yet", path)
		return nil
	}
	return windowMap[path]
}

func NewRepoStatusWindow(path string) (error, *RepoStatus) {
	if windowMap[path] == nil {
		log.Log(INFO, "NewRepoStatusWindow() adding new", path)
	} else {
		log.Warn("This already exists yet for path", path)
		log.Warn("should return windowMap[path] here")
		return nil, windowMap[path]
	}

	var realpath string

	homeDir, err := os.UserHomeDir()
	if err != nil {
		log.Log(WARN, "Error getting home directory:", err)
		return err, nil
	}
	goSrcDir := filepath.Join(homeDir, "go/src")

	// allow arbitrary paths, otherwise, assume the repo is in ~/go/src
	if strings.HasPrefix(path, "/") {
		realpath = path
	} else if strings.HasPrefix(path, "~") {
		// TODO: example this to homedir
		tmp := strings.TrimPrefix(path, "~")
		realpath = filepath.Join(homeDir, tmp)
	} else {
		realpath = filepath.Join(goSrcDir, path)
	}

	filename := filepath.Join(realpath, ".git/config")

	_, err = os.Open(filename)
	if err != nil {
		// log.Log(WARN, "Error reading .git/config:", filename, err)
		// log.Log(WARN, "TODO: find .git/config in parent directory")
		return err, nil
	}

	rs := &RepoStatus{
		ready: false,
	}
	rs.tags = make(map[string]string)
	rs.window = gadgets.RawBasicWindow("GO Repo Details " + path)
	rs.window.Horizontal()
	rs.window.Make()
	basebox := rs.window.Box()
	group := basebox.NewGroup("stuff")
	primarybox := group.Box()
	primarybox.Horizontal()
	box2 := group.Box()
	rs.ready = true
	rs.window.Custom = func() {
		rs.Hide()
		log.Warn("repostatus user closed the window()")
	}

	// display the status of the git repository
	rs.drawGitStatus(primarybox)

	// display the git branches and options
	rs.makeBranchesBox(primarybox)

	// show standard git commit and merge controls
	rs.drawGitCommands(primarybox)

	// save ~/go/src & the whole path strings
	rs.path.SetValue(path)
	rs.goSrcPath.SetValue(goSrcDir)
	rs.realPath.SetValue(realpath)

	// add all the tags
	rs.makeTagBox(box2)

	rs.readGitConfig()

	rs.readOnly.SetValue("true")
	// ignore everything else for now
	if strings.HasPrefix(path, "go.wit.com") {
		rs.readOnly.SetValue("false")
	}
	if strings.HasPrefix(path, "git.wit.org") {
		rs.readOnly.SetValue("false")
	}

	// tries 'master', 'main', etc.
	rs.guessMainWorkingName()
	// tries 'devel', etc
	rs.guessDevelWorkingName()
	// sets this to os.Username
	rs.setUserWorkingName()

	windowMap[path] = rs
	return nil, rs
}