summaryrefslogtreecommitdiff
path: root/repo.new.go
blob: cd338f08a91ab7c815e1566f9fd9d97e1664f0b8 (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
package gitpb

import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"go.wit.com/log"
)

// scans in a new git repo. If it detects the repo is a golang project,
// then it parses the go.mod/go.sum files
// TODO: try adding python, rails, perl, rust, other language things?
// I probably will never have time to try that, but I'd take patches for anyone
// that might see this note and feel so inclined.
// todo: use Repos.Lock() ?
func (all *Repos) NewGoRepo(fullpath string, gopath string) (*Repo, error) {
	if gopath == "" {
		return nil, errors.New("blank gopath")
	}
	if r := all.FindByFullPath(fullpath); r != nil {
		log.Info("gitpb.NewGoPath() already has gopath", r.GetGoPath())
		log.Info("gitpb.NewGoPath() already has FullPath", r.FullPath)
		// already had this gopath
		return r, errors.New("gitpb.NewGoPath() duplicate gopath " + gopath)
	}

	// add a new one here
	newr := Repo{
		FullPath:  fullpath,
		Namespace: gopath,
	}
	newr.Times = new(GitTimes)

	newr.GoInfo = new(GoInfo)
	newr.GoInfo.GoPath = gopath
	// everything happens in here
	newr.ReloadForce()

	newr.ValidateUTF8()
	if all.AppendByFullPath(&newr) {
		// worked
		return &newr, nil
	} else {
		// this is dumb, probably never happens. todo: use Repos.Lock()
		if r := all.FindByFullPath(fullpath); r != nil {
			// already had this gopath
			return r, errors.New("gitpb.NewGoPath() AppendUnique() failed but Find() worked" + gopath)
		}
	}
	// todo: use Repos.Lock()
	return nil, errors.New("repo gitpb.NewGoPath() should never have gotten here " + gopath)
}

func (all *Repos) NewRepo(fullpath string, namespace string) (*Repo, error) {
	if r := all.FindByFullPath(fullpath); r != nil {
		log.Info("gitpb.NewRepo() might already have namespace", r.GetNamespace())
		log.Info("gitpb.NewRepo() already has FullPath", r.FullPath)
		// already had this gopath
		return r, errors.New("gitpb.NewRepo() duplicate path " + fullpath)
	}

	// add a new one here
	newr := Repo{
		FullPath:  fullpath,
		Namespace: namespace,
	}
	newr.Times = new(GitTimes)

	// everything happens in here
	newr.ReloadForce()

	newr.ValidateUTF8()
	if all.AppendByFullPath(&newr) {
		// worked
		return &newr, nil
	}

	// todo: use Repos.Lock()
	return nil, errors.New("gitpb.NewRepo() append failed " + fullpath)
}

func NewRepo(fullpath string) (*Repo, error) {
	// add a new one here
	repo := Repo{
		FullPath: fullpath,
	}
	repo.Times = new(GitTimes)

	// everything happens in here
	repo.ReloadForce()
	repo.ValidateUTF8()
	if repo.Namespace == "" {
		wd, _ := os.Getwd()
		repo.Namespace = wd
	}

	// fall back to URL?
	if repo.Namespace == "" {
		giturl := repo.GetURL()
		if giturl == "" {
			log.Info(repo.FullPath, "Namespace & URL are both blank. Warn the user of a local repo.")
			return &repo, nil
		}
		// log.Info("GET Namespace from URL", giturl)
		if tmpURL, err := ParseGitURL(giturl); err == nil {
			// log.Info(repo.FullPath, "namespace might be:", tmpURL.Hostname(), tmpURL.Path)
			repo.Namespace = filepath.Join(tmpURL.Hostname(), tmpURL.Path)
		}
	}
	return &repo, nil
}