-
Notifications
You must be signed in to change notification settings - Fork 76
/
make.go
1065 lines (939 loc) · 33.7 KB
/
make.go
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"golang.org/x/net/publicsuffix"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/vcs"
)
type packageType int
const (
typeGuess packageType = iota
typeLibrary
typeProgram
typeLibraryProgram
typeProgramLibrary
)
var wrapAndSort string
var errUnsupportedHoster = errors.New("unsupported hoster")
func passthroughEnv() []string {
var relevantVariables = []string{
"HOME",
"PATH",
"HTTP_PROXY", "http_proxy",
"HTTPS_PROXY", "https_proxy",
"ALL_PROXY", "all_proxy",
"NO_PROXY", "no_proxy",
"GIT_PROXY_COMMAND",
"GIT_HTTP_PROXY_AUTHMETHOD",
}
var result []string
for _, variable := range relevantVariables {
if value, ok := os.LookupEnv(variable); ok {
result = append(result, fmt.Sprintf("%s=%s", variable, value))
}
}
return result
}
func findVendorDirs(dir string) ([]string, error) {
var vendorDirs []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info != nil && !info.IsDir() {
return nil // nothing to do for anything but directories
}
if info.Name() == ".git" ||
info.Name() == ".hg" ||
info.Name() == ".bzr" {
return filepath.SkipDir
}
if info.Name() == "vendor" {
rel, err := filepath.Rel(dir, path)
if err != nil {
return fmt.Errorf("filepath.Rel: %w", err)
}
vendorDirs = append(vendorDirs, rel)
}
return nil
})
return vendorDirs, err
}
func downloadFile(filename, url string) error {
dst, err := os.Create(filename)
if err != nil {
return fmt.Errorf("create: %w", err)
}
defer dst.Close()
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("http get: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("response: %s", resp.Status)
}
_, err = io.Copy(dst, resp.Body)
if err != nil {
return fmt.Errorf("copy: %w", err)
}
return nil
}
// upstream describes the upstream repo we are about to package.
type upstream struct {
rr *vcs.RepoRoot
tarPath string // path to the downloaded or generated orig tarball tempfile
compression string // compression method, either "gz" or "xz"
version string // Debian package upstream version number, e.g. 0.0~git20180204.1d24609
tag string // Latest upstream tag, if any
commitIsh string // commit-ish corresponding to upstream version to be packaged
remote string // git remote, set to short hostname if upstream git history is included
firstMain string // import path of the first main package within repo, if any
vendorDirs []string // all vendor sub directories, relative to the repo directory
repoDeps []string // the repository paths of all dependencies (e.g. github.com/zyedidia/glob)
hasGodeps bool // whether the Godeps/_workspace directory exists
hasRelease bool // whether any release tags exist, for debian/watch
isRelease bool // whether what we end up packaging is a tagged release
}
func (u *upstream) get(gopath, repo, rev string) error {
done := make(chan struct{})
defer close(done)
go progressSize("go get", filepath.Join(gopath, "src"), done)
rr, err := vcs.RepoRootForImportPath(repo, false)
if err != nil {
return fmt.Errorf("get repo root: %w", err)
}
u.rr = rr
dir := filepath.Join(gopath, "src", rr.Root)
if rev != "" {
// Run "git clone {repo} {dir}" and "git checkout {tag}"
return rr.VCS.CreateAtRev(dir, rr.Repo, rev)
}
// Run "git clone {repo} {dir}" (or the equivalent command for hg, svn, bzr)
return rr.VCS.Create(dir, rr.Repo)
}
func (u *upstream) tarballUrl() (string, error) {
repo := strings.TrimSuffix(u.rr.Repo, ".git")
repoU, err := url.Parse(repo)
if err != nil {
return "", fmt.Errorf("parse URL: %w", err)
}
switch repoU.Host {
case "github.com":
return fmt.Sprintf("%s/archive/%s.tar.%s",
repo, u.tag, u.compression), nil
case "gitlab.com", "salsa.debian.org":
parts := strings.Split(repoU.Path, "/")
if len(parts) < 3 {
return "", fmt.Errorf("incomplete repo URL: %s", u.rr.Repo)
}
project := parts[2]
return fmt.Sprintf("%s/-/archive/%s/%s-%s.tar.%s",
repo, u.tag, project, u.tag, u.compression), nil
case "git.sr.ht":
return fmt.Sprintf("%s/archive/%s.tar.%s",
repo, u.tag, u.compression), nil
default:
return "", errUnsupportedHoster
}
}
func (u *upstream) tarballFromHoster() error {
tarURL, err := u.tarballUrl()
if err != nil {
return err
}
done := make(chan struct{})
go progressSize("Download", u.tarPath, done)
log.Printf("Downloading %s", tarURL)
err = downloadFile(u.tarPath, tarURL)
close(done)
return err
}
func (u *upstream) tar(gopath, repo string) error {
f, err := os.CreateTemp("", "dh-make-golang")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
u.tarPath = f.Name()
f.Close()
if u.isRelease {
if u.hasGodeps {
log.Printf("Godeps/_workspace exists, not downloading tarball from hoster.")
} else {
u.compression = "gz"
if err := u.tarballFromHoster(); err == nil {
return nil
} else if err == errUnsupportedHoster {
log.Printf("INFO: Hoster does not provide release tarball\n")
} else {
return fmt.Errorf("tarball from hoster: %w", err)
}
}
}
u.compression = "xz"
base := filepath.Base(repo)
log.Printf("Generating temp tarball as %q\n", u.tarPath)
dir := filepath.Dir(repo)
cmd := exec.Command(
"tar",
"cJf",
u.tarPath,
"--exclude=.git",
"--exclude=Godeps/_workspace",
"--exclude="+base+"/debian",
base)
cmd.Dir = filepath.Join(gopath, "src", dir)
cmd.Stderr = os.Stderr
return cmd.Run()
}
// findMains finds main packages within the repo (useful to auto-detect the
// package type).
func (u *upstream) findMains(gopath, repo string) error {
cmd := exec.Command("go", "list", "-e", "-f", "{{.ImportPath}} {{.Name}}", repo+"/...")
cmd.Dir = filepath.Join(gopath, "src", repo)
cmd.Env = passthroughEnv()
cmd.Stderr = os.Stderr
log.Println("findMains: Running", cmd, "in", cmd.Dir)
out, err := cmd.Output()
if err != nil {
log.Println("WARNING: In findMains:", fmt.Errorf("%q: %w", cmd.Args, err))
// See https://bugs.debian.org/992610
log.Printf("Retrying without appending \"/...\" to repo")
cmd = exec.Command("go", "list", "-e", "-f", "{{.ImportPath}} {{.Name}}", repo)
cmd.Dir = filepath.Join(gopath, "src", repo)
cmd.Env = passthroughEnv()
cmd.Stderr = os.Stderr
log.Println("findMains: Running", cmd, "in", cmd.Dir)
out, err = cmd.Output()
if err != nil {
log.Println("WARNING: In findMains:", fmt.Errorf("%q: %w", cmd.Args, err))
}
}
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if strings.Contains(line, "/vendor/") ||
strings.Contains(line, "/Godeps/") ||
strings.Contains(line, "/samples/") ||
strings.Contains(line, "/examples/") ||
strings.Contains(line, "/example/") {
continue
}
if strings.HasSuffix(line, " main") {
u.firstMain = strings.TrimSuffix(line, " main")
break
}
}
return nil
}
func (u *upstream) findDependencies(gopath, repo string) error {
log.Printf("Determining dependencies\n")
cmd := exec.Command("go", "list", "-e", "-f", "{{join .Imports \"\\n\"}}\n{{join .TestImports \"\\n\"}}\n{{join .XTestImports \"\\n\"}}", repo+"/...")
cmd.Dir = filepath.Join(gopath, "src", repo)
cmd.Env = passthroughEnv()
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Println("WARNING: In findDependencies:", fmt.Errorf("%q: %w", cmd.Args, err))
// See https://bugs.debian.org/992610
log.Printf("Retrying without appending \"/...\" to repo")
cmd = exec.Command("go", "list", "-e", "-f", "{{join .Imports \"\\n\"}}\n{{join .TestImports \"\\n\"}}\n{{join .XTestImports \"\\n\"}}", repo)
cmd.Dir = filepath.Join(gopath, "src", repo)
cmd.Env = passthroughEnv()
cmd.Stderr = os.Stderr
out, err = cmd.Output()
if err != nil {
log.Println("WARNING: In findDependencies:", fmt.Errorf("%q: %w", cmd.Args, err))
}
}
godependencies := make(map[string]bool)
for _, p := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if p == "" {
continue // skip separators between import types
}
// Strip packages that are included in the repository we are packaging.
if strings.HasPrefix(p, repo+"/") || p == repo {
continue
}
if p == "C" {
// TODO: maybe parse the comments to figure out C deps from pkg-config files?
} else {
godependencies[p] = true
}
}
if len(godependencies) == 0 {
return nil
}
// Remove all packages which are in the standard lib.
cmd = exec.Command("go", "list", "std")
cmd.Stderr = os.Stderr
cmd.Env = passthroughEnv()
out, err = cmd.Output()
if err != nil {
return fmt.Errorf("go list std: (args: %v): %w", cmd.Args, err)
}
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
delete(godependencies, line)
}
// Resolve all packages to the root of their repository.
roots := make(map[string]bool)
for dep := range godependencies {
rr, err := vcs.RepoRootForImportPath(dep, false)
if err != nil {
log.Printf("Could not determine repo path for import path %q: %v\n", dep, err)
continue
}
roots[rr.Root] = true
}
u.repoDeps = make([]string, 0, len(godependencies))
for root := range roots {
u.repoDeps = append(u.repoDeps, root)
}
return nil
}
func makeUpstreamSourceTarball(repo, revision string, forcePrerelease bool) (*upstream, error) {
gopath, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
return nil, fmt.Errorf("create tmp dir: %w", err)
}
defer os.RemoveAll(gopath)
repoDir := filepath.Join(gopath, "src", repo)
var u upstream
log.Printf("Downloading %q\n", repo+"/...")
if err := u.get(gopath, repo, revision); err != nil {
return nil, fmt.Errorf("go get: %w", err)
}
// Verify early this repository uses git (we call pkgVersionFromGit later):
if _, err := os.Stat(filepath.Join(repoDir, ".git")); os.IsNotExist(err) {
return nil, fmt.Errorf("not a git repository; dh-make-golang currently only supports git")
}
if _, err := os.Stat(filepath.Join(repoDir, "debian")); err == nil {
log.Printf("WARNING: ignoring debian/ directory that came with the upstream sources\n")
}
u.vendorDirs, err = findVendorDirs(repoDir)
if err != nil {
return nil, fmt.Errorf("find vendor dirs: %w", err)
}
if len(u.vendorDirs) > 0 {
log.Printf("Deleting upstream vendor/ directories")
for _, dir := range u.vendorDirs {
if err := os.RemoveAll(filepath.Join(repoDir, dir)); err != nil {
return nil, fmt.Errorf("remove all: %w", err)
}
}
}
if _, err := os.Stat(filepath.Join(repoDir, "Godeps", "_workspace")); !os.IsNotExist(err) {
log.Println("Godeps/_workspace detected")
u.hasGodeps = true
}
log.Printf("Determining upstream version number\n")
u.version, err = pkgVersionFromGit(repoDir, &u, forcePrerelease)
if err != nil {
return nil, fmt.Errorf("get package version from Git: %w", err)
}
log.Printf("Package version is %q\n", u.version)
if err := u.findMains(gopath, repo); err != nil {
return nil, fmt.Errorf("find mains: %w", err)
}
if err := u.findDependencies(gopath, repo); err != nil {
return nil, fmt.Errorf("find dependencies: %w", err)
}
if err := u.tar(gopath, repo); err != nil {
return nil, fmt.Errorf("tar: %w", err)
}
return &u, nil
}
func runGitCommandIn(dir string, arg ...string) error {
cmd := exec.Command("git", arg...)
cmd.Dir = dir
cmd.Stderr = os.Stderr
return cmd.Run()
}
func createGitRepository(debsrc, gopkg, orig string, u *upstream,
includeUpstreamHistory bool, allowUnknownHoster bool, debianBranch string, pristineTar bool) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("get cwd: %w", err)
}
dir := filepath.Join(wd, debsrc)
if err := os.Mkdir(dir, 0755); err != nil {
return "", fmt.Errorf("mkdir: %w", err)
}
// "git init -b" is the one-liner we need here, however it was added in Git 2.28.
// For now we prefer to keep compatibility with older Git, so we do it in two
// rounds, "git init" then "git checkout".
//if err := runGitCommandIn(dir, "init", "-b", debianBranch); err != nil {
// return dir, err
//}
if err := runGitCommandIn(dir, "init"); err != nil {
return dir, fmt.Errorf("git init: %w", err)
}
if err := runGitCommandIn(dir, "checkout", "-q", "-b", debianBranch); err != nil {
return dir, fmt.Errorf("git checkout: %w", err)
}
// Set repository options
if debianName := getDebianName(); debianName != "TODO" {
if err := runGitCommandIn(dir, "config", "user.name", debianName); err != nil {
return dir, fmt.Errorf("git config user.name: %w", err)
}
}
if debianEmail := getDebianEmail(); debianEmail != "TODO" {
if err := runGitCommandIn(dir, "config", "user.email", debianEmail); err != nil {
return dir, fmt.Errorf("git config user.email: %w", err)
}
}
if err := runGitCommandIn(dir, "config", "push.default", "matching"); err != nil {
return dir, fmt.Errorf("git config push.default: %w", err)
}
// [remote "origin"]
originURL := "[email protected]:go-team/packages/" + debsrc + ".git"
log.Printf("Adding remote \"origin\" with URL %q\n", originURL)
if err := runGitCommandIn(dir, "remote", "add", "origin", originURL); err != nil {
return dir, fmt.Errorf("git remote add origin %s: %w", originURL, err)
}
if err := runGitCommandIn(dir, "config", "--add", "remote.origin.push", "+refs/heads/*:refs/heads/*"); err != nil {
return dir, fmt.Errorf("git config --add remote.origin.push */heads/*: %w", err)
}
if err := runGitCommandIn(dir, "config", "--add", "remote.origin.push", "+refs/tags/*:refs/tags/*"); err != nil {
return dir, fmt.Errorf("git config --add remote.origin.push */tags/*: %w", err)
}
// Preconfigure branches
branches := []string{debianBranch, "upstream"}
if pristineTar {
branches = append(branches, "pristine-tar")
}
for _, branch := range branches {
if err := runGitCommandIn(dir, "config", "branch."+branch+".remote", "origin"); err != nil {
return dir, fmt.Errorf("git config branch.%s.remote origin: %w", branch, err)
}
if err := runGitCommandIn(dir, "config", "branch."+branch+".merge", "refs/heads/"+branch); err != nil {
return dir, fmt.Errorf("git config branch.%s.merge refs/heads/%s: %w", branch, branch, err)
}
}
if includeUpstreamHistory {
u.remote, err = shortHostName(gopkg, allowUnknownHoster)
if err != nil {
return dir, fmt.Errorf("unable to fetch upstream history: %q", err)
}
if u.remote == "debian" {
u.remote = "salsa"
}
log.Printf("Adding remote %q with URL %q\n", u.remote, u.rr.Repo)
if err := runGitCommandIn(dir, "remote", "add", u.remote, u.rr.Repo); err != nil {
return dir, fmt.Errorf("git remote add %s %s: %w", u.remote, u.rr.Repo, err)
}
log.Printf("Running \"git fetch %s\"\n", u.remote)
if err := runGitCommandIn(dir, "fetch", u.remote); err != nil {
return dir, fmt.Errorf("git fetch %s: %w", u.remote, err)
}
}
// Import upstream orig tarball
arg := []string{"import-orig", "--no-interactive", "--debian-branch=" + debianBranch}
if pristineTar {
arg = append(arg, "--pristine-tar")
}
if includeUpstreamHistory {
arg = append(arg, "--upstream-vcs-tag="+u.commitIsh)
}
arg = append(arg, filepath.Join(wd, orig))
cmd := exec.Command("gbp", arg...)
cmd.Dir = dir
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return dir, fmt.Errorf("import-orig: %w", err)
}
{
f, err := os.OpenFile(filepath.Join(dir, ".gitignore"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return dir, fmt.Errorf("open .gitignore: %w", err)
}
// Beginning newline in case the file already exists and lacks a newline
// (not all editors enforce a newline at the end of the file):
if _, err := f.Write([]byte("\n/.pc/\n/_build/\n")); err != nil {
return dir, fmt.Errorf("write to .gitignore: %w", err)
}
if err := f.Close(); err != nil {
return dir, fmt.Errorf("close .gitignore: %w", err)
}
}
if err := runGitCommandIn(dir, "add", ".gitignore"); err != nil {
return dir, fmt.Errorf("git add .gitignore: %w", err)
}
if err := runGitCommandIn(dir, "commit", "-m", "Ignore _build and quilt .pc dirs via .gitignore"); err != nil {
return dir, fmt.Errorf("git commit (.gitignore): %w", err)
}
return dir, nil
}
// normalize package name into Debian standard[1]
// https://www.debian.org/doc/debian-policy/ch-controlfields.html#source
// Package names (both source and binary, see Package, Section 5.6.7) must
// consist only of lower case letters (a-z), digits (0-9), plus (+) and minus
// (-) signs, and periods (.). They must be at least two characters long and
// must start with an alphanumeric character.
func normalizeDebianPackageName(str string) string {
lowerDigitPlusMinusDot := func(r rune) rune {
switch {
case r >= 'a' && r <= 'z' || '0' <= r && r <= '9':
return r
case r >= 'A' && r <= 'Z':
return r + ('a' - 'A')
case r == '.' || r == '+' || r == '-':
return r
case r == '_':
return '-'
}
return -1
}
safe := strings.Trim(strings.Map(lowerDigitPlusMinusDot, str), "-")
if len(safe) < 2 {
return "TODO"
}
return safe
}
func shortHostName(gopkg string, allowUnknownHoster bool) (host string, err error) {
knownHosts := map[string]string{
// keep the list in alphabetical order
"bazil.org": "bazil",
"bitbucket.org": "bitbucket",
"blitiri.com.ar": "blitiri",
"cloud.google.com": "googlecloud",
"code.google.com": "googlecode",
"filippo.io": "filippo",
"fyne.io": "fyne",
"git.sr.ht": "sourcehut",
"github.com": "github",
"gitlab.com": "gitlab",
"go.cypherpunks.ru": "cypherpunks",
"go.mongodb.org": "mongodb",
"go.opentelemetry.io": "opentelemetry",
"go.step.sm": "step",
"go.uber.org": "uber",
"go4.org": "go4",
"gocloud.dev": "gocloud",
"golang.org": "golang",
"google.golang.org": "google",
"gopkg.in": "gopkg",
"honnef.co": "honnef",
"howett.net": "howett",
"k8s.io": "k8s",
"modernc.org": "modernc",
"pault.ag": "pault",
"rsc.io": "rsc",
"salsa.debian.org": "debian",
"sigs.k8s.io": "k8s-sigs",
"software.sslmate.com": "sslmate",
}
parts := strings.Split(gopkg, "/")
fqdn := parts[0]
if host, ok := knownHosts[fqdn]; ok {
return host, nil
}
if !allowUnknownHoster {
return "", fmt.Errorf("unknown hoster %q", fqdn)
}
suffix, _ := publicsuffix.PublicSuffix(fqdn)
host = fqdn[:len(fqdn)-len(suffix)-len(".")]
log.Printf("WARNING: Using %q as canonical hostname for %q. If that is not okay, please file a bug against %s.\n", host, fqdn, os.Args[0])
return host, nil
}
// debianNameFromGopkg maps a Go package repo path to a Debian package name,
// e.g. "golang.org/x/text" → "golang-golang-x-text".
// This follows https://fedoraproject.org/wiki/PackagingDrafts/Go#Package_Names
func debianNameFromGopkg(gopkg string, t packageType, customProgPkgName string, allowUnknownHoster bool) string {
parts := strings.Split(gopkg, "/")
if t == typeProgram || t == typeProgramLibrary {
if customProgPkgName != "" {
return normalizeDebianPackageName(customProgPkgName)
}
return normalizeDebianPackageName(parts[len(parts)-1])
}
host, err := shortHostName(gopkg, allowUnknownHoster)
if err != nil {
log.Fatalf("Cannot derive Debian package name: %v. See -help output for -allow_unknown_hoster\n", err)
}
parts[0] = host
return normalizeDebianPackageName("golang-" + strings.Join(parts, "-"))
}
func getDebianName() string {
if name := strings.TrimSpace(os.Getenv("DEBFULLNAME")); name != "" {
return name
}
if name := strings.TrimSpace(os.Getenv("DEBNAME")); name != "" {
return name
}
if u, err := user.Current(); err == nil && u.Name != "" {
return u.Name
}
return "TODO"
}
func getDebianEmail() string {
if email := strings.TrimSpace(os.Getenv("DEBEMAIL")); email != "" {
return email
}
mailname, err := os.ReadFile("/etc/mailname")
// By default, /etc/mailname contains "debian" which is not useful; check for ".".
if err == nil && strings.Contains(string(mailname), ".") {
if u, err := user.Current(); err == nil && u.Username != "" {
return u.Username + "@" + strings.TrimSpace(string(mailname))
}
}
return "TODO"
}
func writeITP(gopkg, debsrc, debversion string) (string, error) {
itpname := fmt.Sprintf("itp-%s.txt", debsrc)
f, err := os.Create(itpname)
if err != nil {
return itpname, fmt.Errorf("create file: %w", err)
}
defer f.Close()
// TODO: memoize
license, _, err := getLicenseForGopkg(gopkg)
if err != nil {
log.Printf("Could not determine license for %q: %v\n", gopkg, err)
license = "TODO"
}
author, _, err := getAuthorAndCopyrightForGopkg(gopkg)
if err != nil {
log.Printf("Could not determine author for %q: %v\n", gopkg, err)
author = "TODO"
}
description, err := getDescriptionForGopkg(gopkg)
if err != nil {
log.Printf("Could not determine description for %q: %v\n", gopkg, err)
description = "TODO"
}
fmt.Fprintf(f, "From: %q <%s>\n", getDebianName(), getDebianEmail())
fmt.Fprintf(f, "To: [email protected]\n")
fmt.Fprintf(f, "Subject: ITP: %s -- %s\n", debsrc, description)
fmt.Fprintf(f, "Content-Type: text/plain; charset=utf-8\n")
fmt.Fprintf(f, "Content-Transfer-Encoding: 8bit\n")
fmt.Fprintf(f, "X-Debbugs-CC: [email protected], [email protected]\n")
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "Package: wnpp\n")
fmt.Fprintf(f, "Severity: wishlist\n")
fmt.Fprintf(f, "Owner: %s <%s>\n", getDebianName(), getDebianEmail())
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "* Package name : %s\n", debsrc)
fmt.Fprintf(f, " Version : %s\n", debversion)
fmt.Fprintf(f, " Upstream Author : %s\n", author)
fmt.Fprintf(f, "* URL : %s\n", getHomepageForGopkg(gopkg))
fmt.Fprintf(f, "* License : %s\n", license)
fmt.Fprintf(f, " Programming Lang: Go\n")
fmt.Fprintf(f, " Description : %s\n", description)
fmt.Fprintf(f, "\n")
longdescription, err := getLongDescriptionForGopkg(gopkg)
if err != nil {
log.Printf("Could not determine long description for %q: %v\n", gopkg, err)
longdescription = "TODO: long description"
}
fmt.Fprintln(f, longdescription)
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "TODO: perhaps reasoning\n")
return itpname, nil
}
func copyFile(src, dest string) error {
input, err := os.Open(src)
if err != nil {
return fmt.Errorf("open: %w", err)
}
defer input.Close()
output, err := os.Create(dest)
if err != nil {
return fmt.Errorf("create: %w", err)
}
if _, err := io.Copy(output, input); err != nil {
return fmt.Errorf("copy: %w", err)
}
return output.Close()
}
func execMake(args []string, usage func()) {
fs := flag.NewFlagSet("make", flag.ExitOnError)
if usage != nil {
fs.Usage = usage
} else {
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [make] [FLAG]... <go-package-importpath>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example: %s make golang.org/x/oauth2\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "\"%s make\" downloads the specified Go package from the Internet,\nand creates new files and directories in the current working directory.\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
fs.PrintDefaults()
}
}
var gitRevision string
fs.StringVar(&gitRevision,
"git_revision",
"",
"git revision (see gitrevisions(7)) of the specified Go package\n"+
"to check out, defaulting to the default behavior of git clone.\n"+
"Useful in case you do not want to package e.g. current HEAD.")
var allowUnknownHoster bool
fs.BoolVar(&allowUnknownHoster,
"allow_unknown_hoster",
false,
"The pkg-go naming conventions use a canonical identifier for\n"+
"the hostname (see https://go-team.pages.debian.net/packaging.html),\n"+
"and the mapping is hardcoded into dh-make-golang.\n"+
"In case you want to package a Go package living on an unknown hoster,\n"+
"you may set this flag to true and double-check that the resulting\n"+
"package name is sane. Contact pkg-go if unsure.")
var dep14 bool
fs.BoolVar(&dep14,
"dep14",
true,
"Follow DEP-14 branch naming and use debian/sid (instead of master)\n"+
"as the default debian-branch.")
var pristineTar bool
fs.BoolVar(&pristineTar,
"pristine-tar",
false,
"Keep using a pristine-tar branch as in the old workflow.\n"+
"Discouraged, see \"pristine-tar considered harmful\"\n"+
"https://michael.stapelberg.ch/posts/2018-01-28-pristine-tar/\n"+
"and the \"Drop pristine-tar branches\" section at\n"+
"https://go-team.pages.debian.net/workflow-changes.html")
var forcePrerelease bool
fs.BoolVar(&forcePrerelease,
"force_prerelease",
false,
"Package @master or @tip instead of the latest tagged version")
var pkgTypeString string
fs.StringVar(&pkgTypeString,
"type",
"",
"Set package type, one of:\n"+
` * "library" (aliases: "lib", "l", "dev")`+"\n"+
` * "program" (aliases: "prog", "p")`+"\n"+
` * "library+program" (aliases: "lib+prog", "l+p", "both")`+"\n"+
` * "program+library" (aliases: "prog+lib", "p+l", "combined")`)
var customProgPkgName string
fs.StringVar(&customProgPkgName,
"program_package_name",
"",
"Override the program package name, and the source package name too\n"+
"when appropriate, e.g. to name github.com/cli/cli as \"gh\"")
var includeUpstreamHistory bool
fs.BoolVar(&includeUpstreamHistory,
"upstream_git_history",
true,
"Include upstream git history (Debian pkg-go team new workflow).\n"+
"New in dh-make-golang 0.3.0, currently experimental.")
fs.StringVar(&wrapAndSort,
"wrap-and-sort",
"a",
"Set how the various multi-line fields in debian/control are formatted.\n"+
"Valid values are \"a\", \"at\" and \"ast\", see wrap-and-sort(1) man page\n"+
"for more information.")
// ====================================================================
//
// Start actual make routine
//
// ====================================================================
log.Printf("Starting %q", buildVersionString())
err := fs.Parse(args)
if err != nil {
log.Fatalf("parse args: %v", err)
}
if fs.NArg() < 1 {
fs.Usage()
os.Exit(1)
}
gitRevision = strings.TrimSpace(gitRevision)
gopkg := fs.Arg(0)
// Ensure the specified argument is a Go package import path.
rr, err := vcs.RepoRootForImportPath(gopkg, false)
if err != nil {
log.Fatalf("Verifying arguments: %v — did you specify a Go package import path?", err)
}
if gopkg != rr.Root {
log.Printf("Continuing with repository root %q instead of specified import path %q (repositories are the unit of packaging in Debian)", rr.Root, gopkg)
gopkg = rr.Root
}
// Set default source and binary package names.
// Note that debsrc may change depending on the actual package type.
debsrc := debianNameFromGopkg(gopkg, typeLibrary, customProgPkgName, allowUnknownHoster)
debLib := debsrc + "-dev"
debProg := debianNameFromGopkg(gopkg, typeProgram, customProgPkgName, allowUnknownHoster)
var pkgType packageType
switch strings.TrimSpace(pkgTypeString) {
case "", "guess":
pkgType = typeGuess
case "library", "lib", "l", "dev":
pkgType = typeLibrary
case "program", "prog", "p":
pkgType = typeProgram
case "library+program", "lib+prog", "l+p", "both":
// Example packages: golang-github-alecthomas-chroma,
// golang-github-tdewolff-minify, golang-github-spf13-viper
pkgType = typeLibraryProgram
case "program+library", "prog+lib", "p+l", "combined":
// Example package: hugo
pkgType = typeProgramLibrary
default:
log.Fatalf("-type=%q not recognized, aborting\n", pkgTypeString)
}
// Set the debian branch.
debBranch := "master"
if dep14 {
debBranch = "debian/sid"
}
switch strings.TrimSpace(wrapAndSort) {
case "a":
// Current default, also what "cme fix dpkg" generates
wrapAndSort = "a"
case "at", "ta":
// -t, --trailing-comma, preferred by Martina Ferrari
// and currently used in quite a few packages
wrapAndSort = "at"
case "ast", "ats", "sat", "sta", "tas", "tsa":
// -s, --short-indent too, proposed by Guillem Jover
wrapAndSort = "ast"
default:
log.Fatalf("%q is not a valid value for -wrap-and-sort, aborting.", wrapAndSort)
}
if pkgType != typeGuess {
debsrc = debianNameFromGopkg(gopkg, pkgType, customProgPkgName, allowUnknownHoster)
if _, err := os.Stat(debsrc); err == nil {
log.Fatalf("Output directory %q already exists, aborting\n", debsrc)
}
}
// if pkgType == typeGuess, debsrc (also the output directory) will be
// determined later, i.e. after the upstream source has been downloaded.
if strings.ToLower(gopkg) != gopkg {
// Without -git_revision, specifying the package name in the wrong case
// will lead to two checkouts, i.e. wasting bandwidth. With
// -git_revision, packaging might fail.
//
// In case it turns out that Go package names should never contain any
// uppercase letters, we can just auto-convert the argument.
log.Printf("WARNING: Go package names are case-sensitive. Did you really mean %q instead of %q?\n",
gopkg, strings.ToLower(gopkg))
}
var (
eg errgroup.Group
golangBinaries map[string]string // map[goImportPath]debianBinaryPackage
)
// TODO: also check whether there already is a git repository on salsa.
eg.Go(func() error {
var err error
golangBinaries, err = getGolangBinaries()
return err
})
u, err := makeUpstreamSourceTarball(gopkg, gitRevision, forcePrerelease)
if err != nil {
log.Fatalf("Could not create a tarball of the upstream source: %v\n", err)
}
if pkgType == typeGuess {
if u.firstMain != "" {
log.Printf("Assuming you are packaging a program (because %q defines a main package), use -type to override\n", u.firstMain)
pkgType = typeProgram
debsrc = debianNameFromGopkg(gopkg, pkgType, customProgPkgName, allowUnknownHoster)
} else {
pkgType = typeLibrary
}
}
if _, err := os.Stat(debsrc); err == nil {
log.Fatalf("Output directory %q already exists, aborting\n", debsrc)
}
if err := eg.Wait(); err != nil {
log.Printf("Could not check for existing Go packages in Debian: %v", err)
}
if debbin, ok := golangBinaries[gopkg]; ok {
log.Printf("WARNING: A package called %q is already in Debian! See https://tracker.debian.org/pkg/%s\n",
debbin, debbin)
}
orig := fmt.Sprintf("%s_%s.orig.tar.%s", debsrc, u.version, u.compression)
log.Printf("Moving tempfile to %q\n", orig)
// We need to copy the file, merely renaming is not enough since the file
// might be on a different filesystem (/tmp often is a tmpfs).
if err := copyFile(u.tarPath, orig); err != nil {
log.Fatalf("Could not rename orig tarball from %q to %q: %v\n", u.tarPath, orig, err)
}
if err := os.Remove(u.tarPath); err != nil {
log.Printf("Could not remove tempfile %q: %v\n", u.tarPath, err)
}
debversion := u.version + "-1"
dir, err := createGitRepository(debsrc, gopkg, orig, u, includeUpstreamHistory, allowUnknownHoster, debBranch, pristineTar)
if err != nil {
log.Fatalf("Could not create git repository: %v\n", err)
}
debdependencies := make([]string, 0, len(u.repoDeps))
for _, dep := range u.repoDeps {
if len(golangBinaries) == 0 {
// fall back to heuristic
debdependencies = append(debdependencies, debianNameFromGopkg(dep, typeLibrary, "", allowUnknownHoster)+"-dev")
continue
}
bin, ok := golangBinaries[dep]
if !ok {
log.Printf("Build-Dependency %q is not yet available in Debian, or has not yet been converted to use XS-Go-Import-Path in debian/control", dep)