-
Notifications
You must be signed in to change notification settings - Fork 110
/
publish.go
223 lines (179 loc) · 4.38 KB
/
publish.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
package gxutil
import (
"fmt"
"os"
"path/filepath"
"strings"
homedir "github.com/mitchellh/go-homedir"
gi "github.com/sabhiram/go-gitignore"
)
func (pm *PM) PublishPackage(dir string, pkg *PackageBase) (string, error) {
// make sure we have the actual package dir, and not a hashdir
if _, err := os.Stat(filepath.Join(dir, PkgFileName)); err != nil {
// try appending the package name
_, err = os.Stat(filepath.Join(dir, pkg.Name, PkgFileName))
if err != nil {
return "", fmt.Errorf("%s did not contain a package!", dir)
}
dir = filepath.Join(dir, pkg.Name)
}
gitig, err := gi.CompileIgnoreFile(filepath.Join(dir, ".gitignore"))
if err != nil && !os.IsNotExist(err) {
return "", err
}
home, err := homedir.Dir()
if err != nil {
return "", err
}
pgig := filepath.Join(home, ".gitignore")
var ggitig *gi.GitIgnore
if _, err := os.Stat(pgig); err == nil {
var err error
ggitig, err = gi.CompileIgnoreFile(pgig)
if err != nil {
return "", err
}
}
gxig, err := gi.CompileIgnoreFile(filepath.Join(dir, ".gxignore"))
if err != nil && !os.IsNotExist(err) {
return "", err
}
var files []string
filepath.Walk(dir, func(p string, info os.FileInfo, err error) error {
// ignore directories
if info.IsDir() {
return nil
}
// get relative path
rel := p[len(dir):]
if dir[len(dir)-1] != '/' {
rel = rel[1:]
}
// make relative path cross platform safe
rel = filepath.ToSlash(rel)
// respect gitignore
if gitig != nil && gitig.MatchesPath(rel) {
return nil
}
// respect global gitignore
if ggitig != nil && ggitig.MatchesPath(rel) {
return nil
}
// respect gxignore
if gxig != nil && gxig.MatchesPath(rel) {
return nil
}
// dont publish the git repo
if strings.HasPrefix(rel, ".git") {
return nil
}
// dont publish gx repo files
if strings.HasPrefix(rel, ".gx/") || strings.HasSuffix(rel, ".gxrc") {
return nil
}
files = append(files, rel)
return nil
})
// we cant guarantee that the 'empty dir' object exists already
blank, err := pm.Shell().NewObject("unixfs-dir")
if err != nil {
return "", err
}
pm.blankDir = blank
pkgdir, err := pm.addFiles(dir, files)
if err != nil {
return "", err
}
final, err := pm.Shell().PatchLink(pm.blankDir, pkg.Name, pkgdir, true)
if err != nil {
return "", err
}
return final, pm.Shell().Pin(final)
}
type filetree struct {
children map[string]*filetree
}
func newFiletree() *filetree {
return &filetree{make(map[string]*filetree)}
}
func newFiletreeFromFiles(files []string) (*filetree, error) {
root := &filetree{make(map[string]*filetree)}
for _, f := range files {
f = strings.TrimRight(f, "/")
parts := strings.Split(f, "/")
if err := root.insert(parts); err != nil {
return nil, err
}
}
return root, nil
}
func (ft *filetree) insert(path []string) error {
if len(path) > 1 {
child, ok := ft.children[path[0]]
if !ok {
child = newFiletree()
ft.children[path[0]] = child
}
return child.insert(path[1:])
}
if len(path) == 1 {
_, ok := ft.children[path[0]]
if ok {
return fmt.Errorf("path already exists: %s", path[0])
}
ft.children[path[0]] = newFiletree()
return nil
}
panic("branch never reached")
}
func (pm *PM) addFiles(root string, files []string) (string, error) {
tree, err := newFiletreeFromFiles(files)
if err != nil {
return "", err
}
return pm.addTree(tree, root)
}
func (pm *PM) addFile(p string) (string, error) {
fi, err := os.Open(p)
if err != nil {
fmt.Printf("open failed: %s\n", err)
return "", err
}
defer fi.Close()
return pm.Shell().AddNoPin(fi)
}
func (pm *PM) addPathElem(v *filetree, f, cwd string) (string, error) {
if v == nil || len(v.children) == 0 {
// file or symlink here
p := filepath.Join(cwd, f)
stat, err := os.Lstat(p)
if err != nil {
fmt.Printf("file stat failed: %s\n", err)
return "", err
}
if stat.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(p)
if err != nil {
return "", err
}
return pm.Shell().AddLink(target)
}
return pm.addFile(p)
}
return pm.addTree(v, filepath.Join(cwd, f))
}
func (pm *PM) addTree(nd *filetree, cwd string) (string, error) {
cur := pm.blankDir
for f, v := range nd.children {
hash, err := pm.addPathElem(v, f, cwd)
if err != nil {
return "", err
}
patched, err := pm.Shell().Patch(cur, "add-link", f, hash)
if err != nil {
return "", err
}
cur = patched
}
return cur, nil
}