forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
commit_archive.go
79 lines (65 loc) · 1.43 KB
/
commit_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
package git
import (
"os"
"path/filepath"
"github.com/Unknwon/cae"
"github.com/Unknwon/cae/tz"
"github.com/Unknwon/cae/zip"
)
type ArchiveType int
const (
AT_ZIP ArchiveType = iota + 1
AT_TARGZ
)
func (c *Commit) CreateArchive(path string, archiveType ArchiveType) error {
f, err := os.OpenFile(path, os.O_CREATE, 0644)
if err == nil {
f.Close()
}
f, err = os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_EXCL, 0644)
if err != nil {
return err
}
defer f.Close()
var streamer cae.Streamer
switch archiveType {
case AT_ZIP:
streamer = zip.NewStreamArachive(f)
case AT_TARGZ:
streamer = tz.NewStreamArachive(f)
}
defer streamer.Close()
return createArchive(&c.Tree, streamer)
}
func createArchive(tree *Tree, streamer cae.Streamer, relPaths ...string) error {
var relPath string
if len(relPaths) > 0 {
relPath = relPaths[0]
}
for _, te := range tree.ListEntries() {
if te.IsDir() {
err := streamer.StreamFile(filepath.Join(relPath, te.name), te, nil)
if err != nil {
return err
}
newTree, err := te.ptree.SubTree(te.name)
if err != nil {
return err
}
if err = createArchive(newTree, streamer, filepath.Join(relPath, te.name)); err != nil {
return err
}
} else {
dataRc, err := te.Blob().Data()
if err != nil {
return err
}
if err := streamer.StreamReader(relPath, te, dataRc); err != nil {
dataRc.Close()
return err
}
dataRc.Close()
}
}
return nil
}