| 1 | |
package net.sf.statsvn.util; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.InputStream; |
| 5 | |
import java.io.InputStreamReader; |
| 6 | |
import java.util.regex.Matcher; |
| 7 | |
import java.util.regex.Pattern; |
| 8 | |
|
| 9 | |
import net.sf.statcvs.util.LookaheadReader; |
| 10 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
public final class SvnStartupUtils { |
| 20 | |
private static final String SVN_VERSION_COMMAND = "svn --version"; |
| 21 | |
|
| 22 | |
private static final String SVN_MINIMUM_VERSION = "1.3.0"; |
| 23 | |
|
| 24 | |
private static final String SVN_MINIMUM_VERSION_DIFF_PER_REV = "1.4.0"; |
| 25 | |
|
| 26 | |
private static final String SVN_VERSION_LINE_PATTERN = ".* [0-9]+\\.[0-9]+\\.[0-9]+.*"; |
| 27 | |
|
| 28 | |
private static final String SVN_VERSION_PATTERN = "[0-9]+\\.[0-9]+\\.[0-9]+"; |
| 29 | |
|
| 30 | |
|
| 31 | |
private static final String SVN_INFO_WITHREPO_LINE_PATTERN = ".*<root>.+</root>.*"; |
| 32 | |
|
| 33 | |
private static final String SVN_REPO_ROOT_NOTFOUND = "Repository root not available - verify that the project was checked out with svn version " |
| 34 | |
+ SVN_MINIMUM_VERSION + " or above."; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | private SvnStartupUtils() { |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public static synchronized String checkSvnVersionSufficient() throws SvnVersionMismatchException { |
| 52 | 0 | ProcessUtils pUtils = null; |
| 53 | |
try { |
| 54 | |
|
| 55 | 0 | pUtils = ProcessUtils.call(SVN_VERSION_COMMAND); |
| 56 | 0 | final InputStream istream = pUtils.getInputStream(); |
| 57 | 0 | final LookaheadReader reader = new LookaheadReader(new InputStreamReader(istream)); |
| 58 | |
|
| 59 | 0 | while (reader.hasNextLine()) { |
| 60 | 0 | final String line = reader.nextLine(); |
| 61 | 0 | if (line.matches(SVN_VERSION_LINE_PATTERN)) { |
| 62 | |
|
| 63 | 0 | final Pattern pRegex = Pattern.compile(SVN_VERSION_PATTERN); |
| 64 | 0 | final Matcher m = pRegex.matcher(line); |
| 65 | 0 | if (m.find()) { |
| 66 | 0 | final String versionString = line.substring(m.start(), m.end()); |
| 67 | |
|
| 68 | |
|
| 69 | 0 | if (versionString.compareTo(SVN_MINIMUM_VERSION) >= 0) { |
| 70 | 0 | return versionString; |
| 71 | |
} else { |
| 72 | 0 | throw new SvnVersionMismatchException(versionString, SVN_MINIMUM_VERSION); |
| 73 | |
} |
| 74 | |
} |
| 75 | |
} |
| 76 | 0 | } |
| 77 | |
|
| 78 | 0 | if (pUtils.hasErrorOccured()) { |
| 79 | 0 | throw new IOException(pUtils.getErrorMessage()); |
| 80 | |
} |
| 81 | 0 | } catch (final IOException e) { |
| 82 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
| 83 | 0 | } catch (final RuntimeException e) { |
| 84 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
| 85 | |
} finally { |
| 86 | 0 | if (pUtils != null) { |
| 87 | |
try { |
| 88 | 0 | pUtils.close(); |
| 89 | 0 | } catch (final IOException e) { |
| 90 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
| 91 | 0 | } |
| 92 | |
} |
| 93 | |
} |
| 94 | |
|
| 95 | 0 | throw new SvnVersionMismatchException(); |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static synchronized void checkRepoRootAvailable() throws SvnVersionMismatchException { |
| 106 | 0 | ProcessUtils pUtils = null; |
| 107 | |
try { |
| 108 | 0 | final boolean rootOnlyTrue = true; |
| 109 | 0 | pUtils = SvnInfoUtils.getSvnInfo(rootOnlyTrue); |
| 110 | 0 | final InputStream istream = pUtils.getInputStream(); |
| 111 | 0 | final LookaheadReader reader = new LookaheadReader(new InputStreamReader(istream)); |
| 112 | |
|
| 113 | 0 | while (reader.hasNextLine()) { |
| 114 | 0 | final String line = reader.nextLine(); |
| 115 | 0 | if (line.matches(SVN_INFO_WITHREPO_LINE_PATTERN)) { |
| 116 | |
|
| 117 | |
|
| 118 | 0 | istream.close(); |
| 119 | |
return; |
| 120 | |
} |
| 121 | 0 | } |
| 122 | |
|
| 123 | 0 | if (pUtils.hasErrorOccured()) { |
| 124 | 0 | throw new IOException(pUtils.getErrorMessage()); |
| 125 | |
} |
| 126 | 0 | } catch (final Exception e) { |
| 127 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
| 128 | |
} finally { |
| 129 | 0 | if (pUtils != null) { |
| 130 | |
try { |
| 131 | 0 | pUtils.close(); |
| 132 | 0 | } catch (final IOException e) { |
| 133 | 0 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
| 134 | 0 | } |
| 135 | |
} |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | throw new SvnVersionMismatchException(SVN_REPO_ROOT_NOTFOUND); |
| 139 | |
|
| 140 | |
} |
| 141 | |
|
| 142 | |
public static synchronized boolean checkDiffPerRevPossible(final String version) { |
| 143 | |
|
| 144 | 0 | return version.compareTo(SVN_MINIMUM_VERSION_DIFF_PER_REV) >= 0; |
| 145 | |
} |
| 146 | |
} |