1 | |
package net.sf.statsvn.util.svnkit; |
2 | |
|
3 | |
import java.io.ByteArrayInputStream; |
4 | |
import java.io.ByteArrayOutputStream; |
5 | |
import java.io.File; |
6 | |
import java.io.IOException; |
7 | |
import java.util.Vector; |
8 | |
|
9 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
10 | |
import net.sf.statsvn.util.BinaryDiffException; |
11 | |
import net.sf.statsvn.util.StringUtils; |
12 | |
import net.sf.statsvn.util.SvnDiffUtils; |
13 | |
|
14 | |
import org.tmatesoft.svn.core.SVNDepth; |
15 | |
import org.tmatesoft.svn.core.SVNException; |
16 | |
import org.tmatesoft.svn.core.wc.SVNClientManager; |
17 | |
import org.tmatesoft.svn.core.wc.SVNRevision; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class SvnKitDiff extends SvnDiffUtils { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
private static String replaceRelativePathWithinDiffData(File rootDirectory, String diffData) { |
32 | 0 | String rootPath = rootDirectory.getAbsoluteFile().getAbsolutePath(); |
33 | |
|
34 | 0 | rootPath = StringUtils.replace(File.separator, "/", rootPath); |
35 | |
|
36 | 0 | return StringUtils.replace(rootPath, "", diffData); |
37 | |
} |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public SvnKitDiff(SvnKitProcessor processor) { |
45 | 0 | super(processor); |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public File getCheckoutDirectory() { |
53 | 0 | return getSvnKitProcessor().getCheckoutDirectory(); |
54 | |
} |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public Vector getLineDiff(String newRevNr) throws IOException, BinaryDiffException { |
62 | 0 | ByteArrayOutputStream diffBytes = new ByteArrayOutputStream(); |
63 | 0 | int revisionNo = Integer.parseInt(newRevNr); |
64 | |
try { |
65 | 0 | getManager().getDiffClient().doDiff(getCheckoutDirectory(), SVNRevision.create(revisionNo), SVNRevision.create(revisionNo - 1), |
66 | |
SVNRevision.create(revisionNo), SVNDepth.INFINITY, false, diffBytes, null); |
67 | 0 | } catch (SVNException ex) { |
68 | 0 | handleSvnException(ex); |
69 | 0 | } |
70 | 0 | String modDiffDataStr = replaceRelativePathWithinDiffData(getCheckoutDirectory(), diffBytes.toString()); |
71 | |
|
72 | 0 | final Vector answer = new Vector(); |
73 | 0 | parseMultipleDiffStream(answer, new ByteArrayInputStream(modDiffDataStr.getBytes())); |
74 | 0 | return answer; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public int[] getLineDiff(String oldRevNr, String newRevNr, String filename) throws IOException, BinaryDiffException { |
81 | |
|
82 | 0 | int oldRevisionNo = Integer.parseInt(oldRevNr); |
83 | 0 | int newRevisionNo = Integer.parseInt(newRevNr); |
84 | 0 | File newFile = new File(getProcessor().getInfoProcessor().relativeToAbsolutePath(filename)); |
85 | 0 | File oldFile = newFile; |
86 | 0 | ByteArrayOutputStream diffBytes = new ByteArrayOutputStream(); |
87 | |
try { |
88 | 0 | getManager().getDiffClient().doDiff(oldFile, SVNRevision.create(oldRevisionNo), newFile, SVNRevision.create(newRevisionNo), SVNDepth.INFINITY, |
89 | |
false, diffBytes, null); |
90 | 0 | } catch (SVNException ex) { |
91 | 0 | handleSvnException(ex); |
92 | 0 | } |
93 | 0 | String modDiffDataStr = replaceRelativePathWithinDiffData(getCheckoutDirectory(), diffBytes.toString()); |
94 | |
|
95 | 0 | return parseSingleDiffStream(new ByteArrayInputStream(modDiffDataStr.getBytes())); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public SVNClientManager getManager() { |
104 | 0 | return getSvnKitProcessor().getManager(); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public SvnKitProcessor getSvnKitProcessor() { |
112 | 0 | return (SvnKitProcessor) getProcessor(); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private void handleSvnException(SVNException ex) throws IOException { |
122 | 0 | String msg = "svn diff " + ex.getMessage(); |
123 | 0 | SvnConfigurationOptions.getTaskLogger().error(msg); |
124 | 0 | throw new IOException(msg); |
125 | |
} |
126 | |
} |