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

import (
	"errors"
	"os"
	"path/filepath"

	"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.
func (all *Repos) NewGoPath(basepath string, gopath string, url string) (*Repo, error) {
	if gopath == "" {
		return nil, errors.New("blank gopath")
	}
	if r := all.FindByGoPath(gopath); r != nil {
		// already had this gopath
		return r, errors.New("gitpb.NewGoPath() duplicate gopath " + gopath)
	}
	log.Info("gitpb.NewGoPath() Attempting to add new path", basepath, gopath)

	// if .git doesn't exist, error out here
	gitpath := filepath.Join(basepath, gopath, ".git")
	_, err := os.Stat(gitpath)
	if err != nil {
		log.Warn("gitpb.NewGoPath() not a git directory", gitpath)
		return nil, err
	}

	// add a new one here
	newr := Repo{
		FullPath: filepath.Join(basepath, gopath),
		GoPath:   gopath,
		URL:      url,
	}
	newr.Tags = new(GitTags)
	// newr.UpdateGit()
	newr.UpdateGitTags()
	newr.GoDeps = new(GoDeps)
	// newr.RedoGoMod()

	switch newr.goListRepoType() {
	case "plugin":
		newr.GoPlugin = true
	case "protobuf":
		newr.GoProtobuf = true
	case "library":
		newr.GoLibrary = true
	case "binary":
		newr.GoBinary = true
	}

	if all.AppendUniqueGoPath(&newr) {
		// worked
		return &newr, nil
	}
	if r := all.FindByGoPath(gopath); r != nil {
		// already had this gopath
		return r, errors.New("gitpb.NewGoPath() AppendUnique() failed but Find() worked" + gopath)
	}
	return nil, errors.New("repo gitpb.NewGoPath() should never have gotten here " + gopath)
}

func (repo *Repo) SetDevelBranchName(bname string) {
	repo.DevelBranchName = bname
}

func (repo *Repo) SetUserBranchName(bname string) {
	repo.UserBranchName = bname
}