This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
/
launchpad.go
136 lines (120 loc) · 3.27 KB
/
launchpad.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
// Copyright 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
package gosrc
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/md5"
"encoding/hex"
"io"
"net/http"
"path"
"regexp"
"sort"
"strings"
)
func init() {
addService(&service{
pattern: regexp.MustCompile(`^launchpad\.net/(?P<repo>(?P<project>[a-z0-9A-Z_.\-]+)(?P<series>/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P<dir>/[a-z0-9A-Z_.\-/]+)*$`),
prefix: "launchpad.net/",
get: getLaunchpadDir,
})
}
type byHash []byte
func (p byHash) Len() int { return len(p) / md5.Size }
func (p byHash) Less(i, j int) bool {
return -1 == bytes.Compare(p[i*md5.Size:(i+1)*md5.Size], p[j*md5.Size:(j+1)*md5.Size])
}
func (p byHash) Swap(i, j int) {
var temp [md5.Size]byte
copy(temp[:], p[i*md5.Size:])
copy(p[i*md5.Size:(i+1)*md5.Size], p[j*md5.Size:])
copy(p[j*md5.Size:], temp[:])
}
func getLaunchpadDir(ctx context.Context, client *http.Client, match map[string]string, savedEtag string) (*Directory, error) {
c := &httpClient{client: client}
if match["project"] != "" && match["series"] != "" {
rc, err := c.getReader(ctx, expand("https://code.launchpad.net/{project}{series}/.bzr/branch-format", match))
switch {
case err == nil:
rc.Close()
// The structure of the import path is launchpad.net/{root}/{dir}.
case IsNotFound(err):
// The structure of the import path is is launchpad.net/{project}/{dir}.
match["repo"] = match["project"]
match["dir"] = expand("{series}{dir}", match)
default:
return nil, err
}
}
p, err := c.getBytes(ctx, expand("https://bazaar.launchpad.net/+branch/{repo}/tarball", match))
if err != nil {
return nil, err
}
gzr, err := gzip.NewReader(bytes.NewReader(p))
if err != nil {
return nil, err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
var hash []byte
inTree := false
dirPrefix := expand("+branch/{repo}{dir}/", match)
var files []*File
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
d, f := path.Split(h.Name)
if !isDocFile(f) {
continue
}
b := make([]byte, h.Size)
if _, err := io.ReadFull(tr, b); err != nil {
return nil, err
}
m := md5.New()
m.Write(b)
hash = m.Sum(hash)
if !strings.HasPrefix(h.Name, dirPrefix) {
continue
}
inTree = true
if d == dirPrefix {
files = append(files, &File{
Name: f,
BrowseURL: expand("http://bazaar.launchpad.net/+branch/{repo}/view/head:{dir}/{0}", match, f),
Data: b})
}
}
if !inTree {
return nil, NotFoundError{Message: "Directory tree does not contain Go files."}
}
sort.Sort(byHash(hash))
m := md5.New()
m.Write(hash)
hash = m.Sum(hash[:0])
etag := hex.EncodeToString(hash)
if etag == savedEtag {
return nil, NotModifiedError{}
}
return &Directory{
BrowseURL: expand("http://bazaar.launchpad.net/+branch/{repo}/view/head:{dir}/", match),
Etag: etag,
Files: files,
LineFmt: "%s#L%d",
ProjectName: match["repo"],
ProjectRoot: expand("launchpad.net/{repo}", match),
ProjectURL: expand("https://launchpad.net/{repo}/", match),
VCS: "bzr",
}, nil
}