1
2
3
4
5 package codehost
6
7 import (
8 "archive/zip"
9 "encoding/xml"
10 "fmt"
11 "io"
12 "os"
13 "path"
14 "path/filepath"
15 "time"
16 )
17
18 func svnParseStat(rev, out string) (*RevInfo, error) {
19 var log struct {
20 Logentry struct {
21 Revision int64 `xml:"revision,attr"`
22 Date string `xml:"date"`
23 } `xml:"logentry"`
24 }
25 if err := xml.Unmarshal([]byte(out), &log); err != nil {
26 return nil, vcsErrorf("unexpected response from svn log --xml: %v\n%s", err, out)
27 }
28
29 t, err := time.Parse(time.RFC3339, log.Logentry.Date)
30 if err != nil {
31 return nil, vcsErrorf("unexpected response from svn log --xml: %v\n%s", err, out)
32 }
33
34 info := &RevInfo{
35 Name: fmt.Sprintf("%d", log.Logentry.Revision),
36 Short: fmt.Sprintf("%012d", log.Logentry.Revision),
37 Time: t.UTC(),
38 Version: rev,
39 }
40 return info, nil
41 }
42
43 func svnReadZip(dst io.Writer, workDir, rev, subdir, remote string) (err error) {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 remotePath := remote
63 if subdir != "" {
64 remotePath += "/" + subdir
65 }
66
67 out, err := Run(workDir, []string{
68 "svn", "list",
69 "--non-interactive",
70 "--xml",
71 "--incremental",
72 "--recursive",
73 "--revision", rev,
74 "--", remotePath,
75 })
76 if err != nil {
77 return err
78 }
79
80 type listEntry struct {
81 Kind string `xml:"kind,attr"`
82 Name string `xml:"name"`
83 Size int64 `xml:"size"`
84 }
85 var list struct {
86 Entries []listEntry `xml:"entry"`
87 }
88 if err := xml.Unmarshal(out, &list); err != nil {
89 return vcsErrorf("unexpected response from svn list --xml: %v\n%s", err, out)
90 }
91
92 exportDir := filepath.Join(workDir, "export")
93
94 if err := os.RemoveAll(exportDir); err != nil {
95 return err
96 }
97 defer os.RemoveAll(exportDir)
98
99 _, err = Run(workDir, []string{
100 "svn", "export",
101 "--non-interactive",
102 "--quiet",
103
104
105 "--native-eol", "LF",
106 "--ignore-externals",
107 "--ignore-keywords",
108
109 "--revision", rev,
110 "--", remotePath,
111 exportDir,
112 })
113 if err != nil {
114 return err
115 }
116
117
118
119
120
121
122 basePath := path.Join(path.Base(remote), subdir)
123
124 zw := zip.NewWriter(dst)
125 for _, e := range list.Entries {
126 if e.Kind != "file" {
127 continue
128 }
129
130 zf, err := zw.Create(path.Join(basePath, e.Name))
131 if err != nil {
132 return err
133 }
134
135 f, err := os.Open(filepath.Join(exportDir, e.Name))
136 if err != nil {
137 if os.IsNotExist(err) {
138 return vcsErrorf("file reported by 'svn list', but not written by 'svn export': %s", e.Name)
139 }
140 return fmt.Errorf("error opening file created by 'svn export': %v", err)
141 }
142
143 n, err := io.Copy(zf, f)
144 f.Close()
145 if err != nil {
146 return err
147 }
148 if n != e.Size {
149 return vcsErrorf("file size differs between 'svn list' and 'svn export': file %s listed as %v bytes, but exported as %v bytes", e.Name, e.Size, n)
150 }
151 }
152
153 return zw.Close()
154 }
155
View as plain text