-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-archive.go
184 lines (150 loc) · 3.97 KB
/
git-archive.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, Ctrl IQ, Inc. All rights reserved
package gobuild
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
type ArchiveFormat uint8
const (
TgzArchive ArchiveFormat = iota
ZipArchive
)
type GitArchive struct {
gd *GitDescription
prefix string
}
func NewGitArchive(prefix string) (*GitArchive, error) {
var err error
ga := new(GitArchive)
ga.gd, err = GitDescribe()
if err != nil {
return nil, err
}
if ga.gd.tag == nil {
return nil, fmt.Errorf("no tag found to create archive from")
} else if ga.gd.n > 0 {
tagName := ga.gd.tag.Name
return nil, fmt.Errorf("tag %s must also be HEAD", tagName)
}
ga.prefix = prefix
return ga, nil
}
func (ga *GitArchive) Create(format ArchiveFormat, w io.Writer, extraFiles ...string) error {
switch format {
case TgzArchive:
return ga.createTgzArchive(w, extraFiles...)
case ZipArchive:
return ga.createZipArchive(w, extraFiles...)
}
return nil
}
func (ga *GitArchive) createTgzArchive(w io.Writer, extraFiles ...string) error {
gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
for _, entry := range append(ga.gd.ListEntries(), extraFiles...) {
err := addEntryToTar(ga.prefix, entry, tarWriter)
if err != nil {
return fmt.Errorf("while adding file %s to tar archive: %s", entry, err)
}
}
return nil
}
func (ga *GitArchive) createZipArchive(w io.Writer, extraFiles ...string) error {
zipWriter := zip.NewWriter(w)
for _, entry := range append(ga.gd.ListEntries(), extraFiles...) {
err := addEntryToZip(ga.prefix, entry, zipWriter)
if err != nil {
return fmt.Errorf("while adding file %s to zip archive: %s", entry, err)
}
}
return zipWriter.Close()
}
func addEntryToTar(prefix, path string, w *tar.Writer) error {
fi, err := os.Stat(path)
if err != nil {
return fmt.Errorf("while getting information for file %s: %s", path, err)
}
link := ""
if fi.Mode()&os.ModeSymlink != 0 {
link, err = os.Readlink(path)
if err != nil {
return fmt.Errorf("while reading symlink %s: %s", path, err)
}
}
header, err := tar.FileInfoHeader(fi, link)
if err != nil {
return fmt.Errorf("while getting tar header for file %s: %s", path, err)
}
header.Name = filepath.Join(prefix, path)
if fi.IsDir() {
header.Name += "/"
}
err = w.WriteHeader(header)
if err != nil {
return fmt.Errorf("while writing tar header for file %s: %s", path, err)
}
if fi.Mode().IsRegular() {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("while opening file %s: %s", path, err)
}
defer file.Close()
_, err = io.Copy(w, file)
if err != nil {
return fmt.Errorf("while copying file %s to tar: %s", path, err)
}
}
return nil
}
func addEntryToZip(prefix, path string, w *zip.Writer) error {
fi, err := os.Stat(path)
if err != nil {
return fmt.Errorf("while getting information for file %s: %s", path, err)
}
header, err := zip.FileInfoHeader(fi)
if err != nil {
return fmt.Errorf("while getting zip information header for file %s: %s", path, err)
}
header.Name = filepath.Join(prefix, path)
header.Method = zip.Deflate
if fi.Mode().IsDir() {
header.Name += "/"
}
f, err := w.CreateHeader(header)
if err != nil {
return fmt.Errorf("while create zip file %s: %s", path, err)
}
if fi.Mode().IsRegular() {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("while opening file %s: %s", path, err)
}
defer file.Close()
_, err = io.Copy(f, file)
if err != nil {
return fmt.Errorf("while copying file %s to zip archive: %s", path, err)
}
} else {
var data []byte
if fi.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(path)
if err != nil {
return fmt.Errorf("while reading symlink %s: %s", path, err)
}
data = []byte(target)
}
_, err = f.Write(data)
if err != nil {
return fmt.Errorf("while copying file %s to zip archive: %s", path, err)
}
}
return nil
}