-
Notifications
You must be signed in to change notification settings - Fork 6
/
changeset.go
339 lines (275 loc) · 8.12 KB
/
changeset.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package svn
//#include <svn_types.h>
//#include <svn_pools.h>
//#include <svn_fs.h>
//#include <svn_delta.h>
//#include <svn_repos.h>
//#include <svn_diff.h>
/*
extern svn_error_t * Go_svn_repos_dir_delta2(svn_fs_root_t *src_root,
svn_fs_root_t *tgt_root,
const svn_delta_editor_t *editor,
void *edit_baton,
apr_pool_t *pool);
extern svn_stream_t * CreateWriterStream(void *baton, apr_pool_t *pool);
extern char * defaultEncoding();
extern svn_error_t * FileMimeType(svn_string_t **mimetype, svn_fs_root_t *root, const char *path, apr_pool_t *pool);
*/
import "C"
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"unsafe"
)
// ChangesetInfo is used to keep commit info
type ChangesetInfo struct {
Commit *Commit
ChangedPaths map[string]*ChangedEntry
}
// Changeset returns changeset details: author, date, log, change files, dirs, diff of files and properties
func (r *Repo) Changeset(rev int64, diffIgnoreSpace bool) (*ChangesetInfo, error) {
if rev == 0 {
return nil, fmt.Errorf("Invalid revision: %d", rev)
}
rez := &ChangesetInfo{}
var err error
rez.Commit, err = r.CommitInfo(rev)
if err != nil {
return nil, err
}
pool := C.svn_pool_create_ex(r.pool, nil)
defer C.svn_pool_destroy(pool)
var (
editor *C.svn_delta_editor_t
editBaton unsafe.Pointer
)
c := &collector{r: r, diffIgnoreSpace: diffIgnoreSpace}
c.changes = make(map[string]*ChangedEntry)
if e := C.svn_fs_revision_root(&c.baseRoot, r.fs, C.svn_revnum_t(rev)-1, pool); e != nil {
return nil, makeError(e)
}
defer C.svn_fs_close_root(c.baseRoot)
if e := C.svn_fs_revision_root(&c.root, r.fs, C.svn_revnum_t(rev), pool); e != nil {
return nil, makeError(e)
}
defer C.svn_fs_close_root(c.root)
// TODO improvement: rewrite editor #open_file #open_directory to limit entries count that we will process.
// We had cases with crazy repos with 16k modifications
if e := C.svn_repos_node_editor(&editor, &editBaton, r.repos, c.baseRoot, c.root, r.pool, pool); e != nil {
return nil, makeError(e)
}
if e := C.Go_svn_repos_dir_delta2(c.baseRoot, c.root, editor, editBaton, pool); e != nil {
return nil, makeError(e)
}
// if e := C.svn_repos_replay2(root, nil, -1, C.TRUE, editor, editBaton, nil, nil, pool); e != nil {
// return nil, makeError(e)
// }
var tree *C.svn_repos_node_t
tree = C.svn_repos_node_from_baton(editBaton)
if err = c.collectChangedPaths(tree, ""); err != nil {
return nil, err
}
rez.ChangedPaths = c.changes
return rez, nil
}
// ChangedEntry is used for storing a changed entry
type ChangedEntry struct {
Kind int // svn node kind
Action string // file operation A- added,R-changed,D-deleted
Diff string // file diff for changed text files
IsBinary bool // indicates if file is binary
}
type collector struct {
baseRoot *C.svn_fs_root_t
root *C.svn_fs_root_t
r *Repo
changes map[string]*ChangedEntry
diffIgnoreSpace bool
}
// Collect changed paths
// similar with print_changed_tree() from svnlook.c
func (c *collector) collectChangedPaths(node *C.svn_repos_node_t, path string) error {
if node == nil {
return nil
}
countMe := true
name := C.GoString(node.name)
fullPath := filepath.Join(path, name)
//log.Println(string(node.action), "Path", fullPath)
switch node.action {
case 'A':
//node.copyfrom_path && node.copyfrom_rev
case 'D':
case 'R':
if node.text_mod == C.FALSE && node.prop_mod == C.FALSE {
countMe = false
}
default:
countMe = false
}
var entry *ChangedEntry
if countMe {
entry = &ChangedEntry{Kind: int(node.kind), Action: string(node.action)}
c.changes[fullPath] = entry
//log.Printf("%s '%s'\n", string(node.action), fullPath)
}
if node.kind == C.svn_node_file {
var (
binaryA, binaryB bool
rootA, rootB *C.svn_fs_root_t
rA, rB int64
err error
)
// TODO: use can use here go routines, because it calculates, limit go routines to N
header := fmt.Sprintf("Index: %s\n", fullPath)
header += "===================================================================\n"
cfullPath := C.CString(fullPath)
defer C.free(unsafe.Pointer(cfullPath))
if node.action == 'R' || node.action == 'D' {
if binaryA, err = c.isBinary(c.baseRoot, cfullPath); err != nil {
return err
}
}
if node.action == 'R' || node.action == 'A' {
if binaryB, err = c.isBinary(c.root, cfullPath); err != nil {
return err
}
}
var buf *C.svn_stringbuf_t
cdiff := C.CString(entry.Diff)
buf = C.svn_stringbuf_create(cdiff, c.r.pool)
if binaryA || binaryB {
entry.IsBinary = true
header += "(Binary files differ)\n\n"
C.svn_stringbuf_appendcstr(buf, C.CString(header))
} else {
// TODO check file size and do not dump files bigger than 10Mb for example
rootA = c.baseRoot
rootB = c.root
if node.action == 'A' {
rootA = nil
}
if node.action == 'D' {
rootB = nil
}
f1Path, err := c.dumpFile(rootA, fullPath)
if err != nil {
return err
}
f2Path, err := c.dumpFile(rootB, fullPath)
if err != nil {
return err
}
//log.Println(f1Path, f2Path)
defer os.Remove(f1Path)
defer os.Remove(f2Path)
cf1Path := C.CString(f1Path)
defer C.free(unsafe.Pointer(cf1Path))
cf2Path := C.CString(f2Path)
defer C.free(unsafe.Pointer(cf2Path))
var diff *C.svn_diff_t
opts := C.svn_diff_file_options_create(c.r.pool) // svn_diff_file_options_t *
if c.diffIgnoreSpace {
opts.ignore_space = C.svn_diff_file_ignore_space_all
}
if err := C.svn_diff_file_diff_2(&diff, cf1Path, cf2Path, opts, c.r.pool); err != nil {
return makeError(err)
}
if C.svn_diff_contains_diffs(diff) == C.TRUE {
C.svn_stringbuf_appendcstr(buf, C.CString(header))
//log.Println("Has diff")
if rootA != nil {
rA = int64(C.svn_fs_revision_root_revision(rootA))
}
if rootB != nil {
rB = int64(C.svn_fs_revision_root_revision(rootB))
}
labelA := fmt.Sprintf("%s\t(revision %d)", fullPath, rA)
labelB := fmt.Sprintf("%s\t(revision %d)", fullPath, rB)
clabelA := C.CString(labelA)
clabelB := C.CString(labelB)
defer C.free(unsafe.Pointer(clabelA))
defer C.free(unsafe.Pointer(clabelB))
cstream := C.CreateWriterStream(unsafe.Pointer(buf), c.r.pool)
if err := C.svn_diff_file_output_unified4(
cstream,
diff,
cf1Path,
cf2Path,
clabelA,
clabelB,
C.defaultEncoding(),
nil,
C.FALSE,
-1,
nil,
nil,
c.r.pool,
); err != nil {
return makeError(err)
}
}
} // end binary check
entry.Diff = C.GoStringN(buf.data, (C.int)(buf.len))
} // end file diff
if node.prop_mod == C.TRUE {
// TODO properties diff
}
tmp := (*C.svn_repos_node_t)(node.child)
if tmp == nil {
return nil
}
if err := c.collectChangedPaths(tmp, fullPath); err != nil {
return err
}
for {
tmp = (*C.svn_repos_node_t)(tmp.sibling)
if tmp == nil {
break
}
if err := c.collectChangedPaths(tmp, fullPath); err != nil {
return err
}
}
return nil
}
// Dumps repository file to temporary file
// Returns file path
// TODO check max file size to generate diff for
func (c *collector) dumpFile(fsRoot *C.svn_fs_root_t, path string) (string, error) {
//log.Println("Dump", path)
var err error
tmp, err := ioutil.TempFile("", "svn-diff")
if err != nil {
return "", err
}
if fsRoot != nil {
subPool := initSubPool(c.r.pool)
svnStream, err := c.r.initSvnStream(fsRoot, subPool, path)
if err != nil {
return "", err
}
defer svnStream.Close()
_, err = io.Copy(tmp, svnStream)
if err != nil {
return "", fmt.Errorf("Can not dump %s: %s", path, err.Error())
}
}
if err = tmp.Close(); err != nil {
return "", fmt.Errorf("Can not close temp file %s: %s", tmp.Name(), err.Error())
}
return tmp.Name(), nil
}
func (c *collector) isBinary(fsRoot *C.svn_fs_root_t, path *C.char) (bool, error) {
var mimetype *C.svn_string_t
if err := C.FileMimeType(&mimetype, fsRoot, path, c.r.pool); err != nil {
return true, makeError(err)
}
if mimetype != nil && C.svn_mime_type_is_binary(mimetype.data) == C.TRUE {
return true, nil
}
return false, nil
}