summaryrefslogtreecommitdiff
path: root/clone.go
blob: dbec8736aa3520313746f91294355be26e59eeeb (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main

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

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

/*
# to support distributed 'git bug'

# create a new user:
git bug user new -e "[email protected]" -n "Jeff Carr"

git pull origin +refs/bugs/\*:refs/bugs/\*
git pull origin +refs/identities/\*:refs/identities/\*

git show-ref | grep refs/bugs/

git log refs/bugs/<some-id>

git config --add remote.origin.fetch '+refs/bugs/*:refs/bugs/*'
git config --add remote.origin.fetch '+refs/identities/*:refs/identities/*'

git config --get-all remote.origin.fetch

[remote "origin"]
	url = ...
	fetch = +refs/heads/*:refs/remotes/origin/*
	fetch = +refs/bugs/*:refs/bugs/*
	fetch = +refs/identities/*:refs/identities/*

# remove the caches
rm -rf .git/git-bug

# rebuild the cache with any command
git bug user

*/

// CleanRepoURL removes http://, https://, and .git suffix from the given URL if present.
func CleanRepoURL(url string) string {
	// Trim protocol prefix
	if strings.HasPrefix(url, "http://") {
		url = strings.TrimPrefix(url, "http://")
	} else if strings.HasPrefix(url, "https://") {
		url = strings.TrimPrefix(url, "https://")
	}

	// Trim trailing .git
	url = strings.TrimSuffix(url, ".git")

	return url
}

func clone(gopath string) (*gitpb.Repo, error) {
	// if the user defined a repo, attempt to download it now
	if gopath == "" {
		// nothing to clone
		// user probably wants to --recursive on current working dir
		return nil, errors.New("gopath was blank")
	}
	gopath = CleanRepoURL(gopath)
	os.Setenv("REPO_AUTO_CLONE", "true")
	// pb, _ := forge.NewGoPath(gopath)
	check := forge.FindAnyPath(filepath.Join(forge.Config.ReposDir, gopath))
	if check != nil {
		if check.IsValidDir() {
			// repo already exists and is valid
			return check, nil
		}
	}
	pb, err := forge.GoClone(gopath)
	if err != nil {
		log.Info("clone() could not download err:", err)
		return nil, err
	}
	if argv.NonRecursive {
		// clone and nothing else
		return pb, nil
	}

	autoWork()
	if err := makeValidGoSum(pb); err != nil {
		return nil, err
	}

	log.Info("go-clone clone() onward and upward")
	return pb, nil
}

// really only does go.sum things
// so not 'really' recursive
// but that is because go.sum is supposed
// to have everything required in it
func recursiveClone(check *gitpb.Repo) error {
	var good int
	var bad int

	badmap := make(map[string]error)

	if check == nil {
		return errors.New("repo was nil")
	}
	log.Info("STARTING RECURSIVE CLONE", check.Namespace)
	log.Info("STARTING RECURSIVE CLONE", check.Namespace)
	// if just cloned, parse the go.sum file for deps
	if check.ParseGoSum() {
	} else {
		makeValidGoSum(check)
	}

	check.ReloadForce()

	if check.GoDeps == nil {
		log.Info("repo godeps == nil", check.Namespace)
		return errors.New("no go deps?")
	}

	// probably this should never be 0 because GoPrimitive should have been true otherwise
	if check.GoDeps.Len() == 0 {
		log.Info("repo len(godeps) == 0", check.Namespace)
		return errors.New("go.sum never parsed?")
	}

	log.Info("deps for", check.Namespace, "len()", check.GoDeps.Len())
	deps := check.GoDeps.SortByGoPath()
	for deps.Scan() {
		depRepo := deps.Next()
		log.Info("download:", depRepo.GetGoPath())
		_, err := clone(depRepo.GetGoPath())
		if err != nil {
			log.Info("recursiveClone() could not download", depRepo.GetGoPath())
			log.Info("err:", err)
			bad += 1
			badmap[depRepo.GetGoPath()] = err
		} else {
			log.Info("downloaded", depRepo.GetGoPath())
			good += 1
		}
	}
	log.Info("got", good, "repos", "failed on", bad, "repos")
	if bad != 0 {
		log.Info("clone() ERROR len(badmap)", len(badmap))
		for gopath, err := range badmap {
			log.Info("clone() ERROR", gopath, err)
		}
		if !argv.Ignore {
			return errors.New("clone failed on some repos")
		}
	}
	return nil
}

func makeValidGoSum(check *gitpb.Repo) error {
	// attempt to grab the notes
	check.RunQuiet([]string{"git", "fetch", "origin", "refs/notes/*:refs/notes/*"})

	if err := check.RunVerbose([]string{"forge", "list"}); err != nil {
		log.Info("")
		log.Info("Do you have go-mod-clean? Otherwise:")
		log.Info("  go install go.wit.com/apps/go-mod-clean@latest")
		log.Info("")
	}

	log.Info("try running go-mod-clean")
	// update go.sum and go.mod
	if err := check.RunVerbose([]string{"go-mod-clean", "lax"}); err != nil {
		log.Info("")
		log.Info("Do you have go-mod-clean? Otherwise:")
		log.Info("  go install go.wit.com/apps/go-mod-clean@latest")
		log.Info("")
	}

	if check.ParseGoSum() {
		return nil
	}

	// if this fails, just use go mod
	if err := check.ValidGoSum(); err != nil {
		cmd := []string{"go", "mod", "init", check.Namespace}
		log.Info("try running", cmd)
		if _, err := check.RunQuiet(cmd); err != nil {
			log.Info("go mod init failed", err)
		}
		if _, err := check.RunQuiet([]string{"go", "mod", "tidy"}); err != nil {
			log.Info("go mod tidy failed", err)
		}
	}
	if check.ParseGoSum() {
		return nil
	}

	return fmt.Errorf("could not make a valid go.mod")
}