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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
makegen.go -- Makefile generator for Gitaly
This file is used to generate _build/Makefile. In _build/Makefile we
can assume that we are in a GOPATH (rooted at _build) and that
$GOPATH/bin is in PATH. The generator runs in the root of the Gitaly
tree. The goal of the generator is to use as little dynamic behaviors
in _build/Makefile (e.g. shelling out to find a list of files), and do
these things as much as possible in Go and then pass them into the
template.
The working directory of makegen.go and the Makefile it generates is
_build.
*/
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
)
func main() {
gm := &gitalyMake{}
tmpl := template.New("Makefile")
tmpl.Funcs(map[string]interface{}{
"join": strings.Join,
})
tmpl = template.Must(tmpl.Parse(templateText))
err := tmpl.Execute(os.Stdout, gm)
if err != nil {
log.Fatalf("execution failed: %s", err)
}
}
type gitalyMake struct {
commandPackages []string
cwd string
versionPrefixed string
goFiles []string
buildTime string
}
// BuildDir is the GOPATH root. It is also the working directory of the Makefile we are generating.
func (gm *gitalyMake) BuildDir() string {
if len(gm.cwd) > 0 {
return gm.cwd
}
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
gm.cwd, err = filepath.EvalSymlinks(cwd)
if err != nil {
log.Fatal(err)
}
return gm.cwd
}
func (gm *gitalyMake) Pkg() string { return "gitlab.com/gitlab-org/gitaly" }
func (gm *gitalyMake) GoImports() string { return "bin/goimports" }
func (gm *gitalyMake) GoCovMerge() string { return "bin/gocovmerge" }
func (gm *gitalyMake) GoLint() string { return "bin/golint" }
func (gm *gitalyMake) GoVendor() string { return "bin/govendor" }
func (gm *gitalyMake) StaticCheck() string { return filepath.Join(gm.BuildDir(), "bin/staticcheck") }
func (gm *gitalyMake) CoverageDir() string { return filepath.Join(gm.BuildDir(), "cover") }
func (gm *gitalyMake) GitalyRubyDir() string { return filepath.Join(gm.SourceDir(), "ruby") }
func (gm *gitalyMake) GitlabShellRelDir() string { return "ruby/vendor/gitlab-shell" }
func (gm *gitalyMake) GitlabShellDir() string {
return filepath.Join(gm.SourceDir(), gm.GitlabShellRelDir())
}
// SourceDir is the location of gitaly's files, inside the _build GOPATH.
func (gm *gitalyMake) SourceDir() string { return filepath.Join(gm.BuildDir(), "src", gm.Pkg()) }
func (gm *gitalyMake) TestRepoStoragePath() string {
path := os.Getenv("TEST_REPO_STORAGE_PATH")
if len(path) == 0 {
log.Fatal("TEST_REPO_STORAGE_PATH is not set")
}
return path
}
func (gm *gitalyMake) TestRepo() string {
return filepath.Join(gm.TestRepoStoragePath(), "gitlab-test.git")
}
func (gm *gitalyMake) GitTestRepo() string {
return filepath.Join(gm.TestRepoStoragePath(), "gitlab-git-test.git")
}
func (gm *gitalyMake) CommandPackages() []string {
if len(gm.commandPackages) > 0 {
return gm.commandPackages
}
entries, err := ioutil.ReadDir(filepath.Join(gm.SourceDir(), "cmd"))
if err != nil {
log.Fatal(err)
}
for _, dir := range entries {
if !dir.IsDir() {
continue
}
gm.commandPackages = append(gm.commandPackages, filepath.Join(gm.Pkg(), "cmd", dir.Name()))
}
return gm.commandPackages
}
func (gm *gitalyMake) Commands() []string {
var out []string
for _, pkg := range gm.CommandPackages() {
out = append(out, filepath.Base(pkg))
}
return out
}
func (gm *gitalyMake) BuildTime() string {
if len(gm.buildTime) > 0 {
return gm.buildTime
}
now := time.Now().UTC()
gm.buildTime = fmt.Sprintf("%d%02d%02d.%02d%02d%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
return gm.buildTime
}
func (gm *gitalyMake) GoLdFlags() string {
return fmt.Sprintf("-ldflags '-X %s/internal/version.version=%s -X %s/internal/version.buildtime=%s'", gm.Pkg(), gm.Version(), gm.Pkg(), gm.BuildTime())
}
func (gm *gitalyMake) VersionPrefixed() string {
if len(gm.versionPrefixed) > 0 {
return gm.versionPrefixed
}
cmd := exec.Command("git", "describe")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Printf("%s: %v", strings.Join(cmd.Args, " "), err)
gm.versionPrefixed = "unknown"
return gm.versionPrefixed
}
gm.versionPrefixed = strings.TrimSpace(string(out))
return gm.versionPrefixed
}
func (gm *gitalyMake) Version() string { return strings.TrimPrefix(gm.VersionPrefixed(), "v") }
func (gm *gitalyMake) GoFiles() []string {
if len(gm.goFiles) > 0 {
return gm.goFiles
}
root := gm.SourceDir() + "/." // Add "/." to traverse symlink
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && path != root {
if path == filepath.Join(root, "ruby") || path == filepath.Join(root, "vendor") {
return filepath.SkipDir
}
if name := info.Name(); name == "testdata" || strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") {
return filepath.SkipDir
}
}
if !info.IsDir() && strings.HasSuffix(path, ".go") {
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
gm.goFiles = append(gm.goFiles, rel)
}
return nil
})
sort.Strings(gm.goFiles)
return gm.goFiles
}
func (gm *gitalyMake) AllPackages() []string {
pkgMap := make(map[string]struct{})
for _, f := range gm.GoFiles() {
pkgMap[filepath.Dir(filepath.Join(gm.Pkg(), f))] = struct{}{}
}
var pkgs []string
for k := range pkgMap {
pkgs = append(pkgs, k)
}
sort.Strings(pkgs)
return pkgs
}
var templateText = `
# _build/Makefile
#
# This is an auto-generated Makefile. Do not edit. Do not invoke
# directly, use ../Makefile instead. This file is generated using
# makegen.go.
#
# These variables may be overriden at runtime by top-level make
PREFIX ?= /usr/local
INSTALL_DEST_DIR := $(DESTDIR)$(PREFIX)/bin/
BUNDLE_FLAGS ?= --deployment
ASSEMBLY_ROOT ?= {{ .BuildDir }}/assembly
unexport GOROOT
unexport GOBIN
.NOTPARALLEL:
.PHONY: all
all: build
.PHONY: build
build: ../.ruby-bundle
go install {{ .GoLdFlags }} {{ join .CommandPackages " " }}
# This file is used by Omnibus and CNG to skip the "bundle install"
# step. Both Omnibus and CNG assume it is in the Gitaly root, not in
# _build. Hence the '../' in front.
../.ruby-bundle: {{ .GitalyRubyDir }}/Gemfile.lock {{ .GitalyRubyDir }}/Gemfile
cd {{ .GitalyRubyDir }} && bundle config # for debugging
cd {{ .GitalyRubyDir }} && bundle install $(BUNDLE_FLAGS)
cd {{ .GitalyRubyDir }} && bundle show gitaly-proto # sanity check
touch $@
.PHONY: install
install: build
mkdir -p $(INSTALL_DEST_DIR)
cd bin && install {{ join .Commands " " }} $(INSTALL_DEST_DIR)
.PHONY: force-ruby-bundle
force-ruby-bundle:
rm -f ../.ruby-bundle
# Assembles all runtime components into a directory
# Used by the GDK: run 'make assemble ASSEMBLY_ROOT=.../gitaly'
.PHONY: assemble
assemble: force-ruby-bundle build assemble-internal
# assemble-internal does not force 'bundle install' to run again
.PHONY: assemble-internal
assemble-internal: assemble-ruby assemble-go
.PHONY: assemble-go
assemble-go: build
rm -rf $(ASSEMBLY_ROOT)/bin
mkdir -p $(ASSEMBLY_ROOT)/bin
cd bin && install {{ join .Commands " " }} $(ASSEMBLY_ROOT)/bin
.PHONY: assemble-ruby
assemble-ruby:
rm -rf $(ASSEMBLY_ROOT)/ruby
mkdir -p $(ASSEMBLY_ROOT)
rm -rf {{ .GitalyRubyDir }}/tmp {{ .GitlabShellDir }}/tmp
cp -r {{ .GitalyRubyDir }} $(ASSEMBLY_ROOT)/ruby
rm -rf $(ASSEMBLY_ROOT)/ruby/spec $(ASSEMBLY_ROOT)/{{ .GitlabShellRelDir }}/spec $(ASSEMBLY_ROOT)/{{ .GitlabShellRelDir }}/gitlab-shell.log
binaries: assemble
@if [ $$(uname -m) != 'x86_64' ]; then echo Incorrect architecture for build: $(uname -m); exit 1; fi
@cd $(ASSEMBLY_ROOT) && shasum -a 256 bin/* | tee checksums.sha256.txt
{{ .TestRepo }}:
git clone --bare --quiet https://gitlab.com/gitlab-org/gitlab-test.git $@
# Git notes aren't fetched by default with git clone
git -C $@ fetch origin refs/notes/*:refs/notes/*
rm -rf $@/refs
mkdir -p $@/refs/heads $@/refs/tags
cp {{ .SourceDir }}/_support/gitlab-test.git-packed-refs $@/packed-refs
git -C $@ fsck --no-progress
{{ .GitTestRepo }}:
git clone --bare --quiet https://gitlab.com/gitlab-org/gitlab-git-test.git $@
rm -rf $@/refs
mkdir -p $@/refs/heads $@/refs/tags
cp {{ .SourceDir }}/_support/gitlab-git-test.git-packed-refs $@/packed-refs
git -C $@ fsck --no-progress
.PHONY: prepare-tests
prepare-tests: {{ .TestRepo }} {{ .GitTestRepo }} ../.ruby-bundle
.PHONY: test
test: test-go rspec rspec-gitlab-shell
.PHONY: test-go
test-go: prepare-tests
@go test -count=1 {{ join .AllPackages " " }} # count=1 bypasses go 1.10 test caching
.PHONY: race-go
race-go: prepare-tests
@go test -race {{ join .AllPackages " " }}
.PHONY: rspec
rspec: assemble-go prepare-tests
cd {{ .GitalyRubyDir }} && bundle exec rspec
.PHONY: rspec-gitlab-shell
rspec-gitlab-shell: {{ .GitlabShellDir }}/config.yml assemble-go prepare-tests
# rspec in {{ .GitlabShellRelDir }}
@cd {{ .GitalyRubyDir }} && bundle exec bin/ruby-cd {{ .GitlabShellDir }} rspec
{{ .GitlabShellDir }}/config.yml: {{ .GitlabShellDir }}/config.yml.example
cp $< $@
.PHONY: verify
verify: lint check-formatting staticcheck govendor-status notice-up-to-date govendor-tagged rubocop
.PHONY: lint
lint: {{ .GoLint }}
# golint
@cd {{ .SourceDir }} && go run _support/lint.go
{{ .GoLint }}:
go get golang.org/x/lint/golint
.PHONY: check-formatting
check-formatting: {{ .GoImports }}
# goimports
@cd {{ .SourceDir }} && goimports -e -l {{ join .GoFiles " " }} | awk '{ print } END { if(NR>0) { print "Formatting error, run make format"; exit(1) } }'
{{ .GoImports }}:
go get golang.org/x/tools/cmd/goimports
.PHONY: format
format: {{ .GoImports }}
# In addition to fixing imports, goimports also formats your code in the same style as gofmt
# so it can be used as a replacement.
@cd {{ .SourceDir }} && goimports -w -l {{ join .GoFiles " " }}
.PHONY: staticcheck
staticcheck: {{ .StaticCheck }}
# staticcheck
@cd {{ .SourceDir }} && {{ .StaticCheck }} {{ join .AllPackages " " }}
# Install staticcheck
{{ .StaticCheck }}:
go get honnef.co/go/tools/cmd/staticcheck
.PHONY: govendor-status
govendor-status: {{ .GoVendor }}
# govendor status
@cd {{ .SourceDir }} && govendor status
{{ .GoVendor }}:
go get github.com/kardianos/govendor
.PHONY: notice-up-to-date
notice-up-to-date: {{ .GoVendor }} clean-ruby-vendor-go
# notice-up-to-date
@(cd {{ .SourceDir }} && govendor license -template _support/notice.template | cmp - NOTICE) || (echo >&2 "NOTICE requires update: 'make notice'" && false)
.PHONY: notice
notice: {{ .GoVendor }} clean-ruby-vendor-go
cd {{ .SourceDir }} && govendor license -template _support/notice.template -o NOTICE
.PHONY: clean-ruby-vendor-go
clean-ruby-vendor-go:
cd {{ .SourceDir }} && mkdir -p ruby/vendor && find ruby/vendor -type f -name '*.go' -delete
.PHONY: govendor-tagged
govendor-tagged: {{ .GoVendor }}
# govendor-tagged
@cd {{ .SourceDir }} && _support/gitaly-proto-tagged
.PHONY: rubocop
rubocop: ../.ruby-bundle
cd {{ .GitalyRubyDir }} && bundle exec rubocop --parallel
.PHONY: cover
cover: prepare-tests {{ .GoCovMerge }}
@echo "NOTE: make cover does not exit 1 on failure, don't use it to check for tests success!"
mkdir -p "{{ .CoverageDir }}"
rm -f {{ .CoverageDir }}/*.out "{{ .CoverageDir }}/all.merged" "{{ .CoverageDir }}/all.html"
@cd {{ .SourceDir }} && go run _support/test-cover-parallel.go {{ .CoverageDir }} {{ join .AllPackages " " }}
{{ .GoCovMerge }} {{ .CoverageDir }}/*.out > "{{ .CoverageDir }}/all.merged"
go tool cover -html "{{ .CoverageDir }}/all.merged" -o "{{ .CoverageDir }}/all.html"
@echo ""
@echo "=====> Total test coverage: <====="
@echo ""
@go tool cover -func "{{ .CoverageDir }}/all.merged"
{{ .GoCovMerge }}:
go get github.com/wadey/gocovmerge
.PHONY: docker
docker:
rm -rf docker/
mkdir -p docker/bin/
rm -rf {{ .GitalyRubyDir }}/tmp
cp -r {{ .GitalyRubyDir }} docker/ruby
rm -rf docker/ruby/vendor/bundle
{{ $pkg := .Pkg }}
{{ $goLdFlags := .GoLdFlags }}
{{ range $cmd := .Commands }}
GOOS=linux GOARCH=amd64 go build {{ $goLdFlags }} -o "docker/bin/{{ $cmd }}" {{ $pkg }}/cmd/{{ $cmd }}
{{ end }}
cp {{ .SourceDir }}/Dockerfile docker/
docker build -t gitlab/gitaly:{{ .VersionPrefixed }} -t gitlab/gitaly:latest docker/
`
|