OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.prog;
15
 
16
import java.io.BufferedReader;
17
import java.io.IOException;
18
import java.io.InputStreamReader;
149 ilm 19
import java.util.regex.Matcher;
20
import java.util.regex.Pattern;
17 ilm 21
 
22
public class SVNUtils {
23
 
24
    public static void main(String[] args) throws IOException {
25
        if (args[0].equals("lastChangedRev")) {
26
            System.out.println(lastChangedRev(args[1]));
27
        } else {
28
            System.err.println("must choose a method");
29
        }
30
    }
31
 
32
    public static Process svn(String[] args) throws IOException {
33
        final String[] cmdNArgs = new String[args.length + 1];
34
        cmdNArgs[0] = "svn";
35
        System.arraycopy(args, 0, cmdNArgs, 1, args.length);
36
        return Runtime.getRuntime().exec(cmdNArgs);
37
    }
38
 
39
    public static String lastChangedRev(String path) throws IOException {
40
        final Process p = svn(new String[] { "info", path + "@HEAD" });
41
 
42
        final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
43
        String line;
44
        String res = null;
45
        while ((line = reader.readLine()) != null) {
46
            if (line.startsWith("Last Changed Rev"))
47
                res = line.substring(line.indexOf(':') + 1).trim();
48
        }
49
        return res;
50
    }
51
 
149 ilm 52
    // match placeholder $Rev$ fixed length $Rev:: $ and of course with value $Rev: 12 $
151 ilm 53
    // don't require to match to the end to allow callers to only know the start of the substitute
54
    // and to not bother with the pattern
55
    private static final Pattern KEYWORD_PATTERN = Pattern.compile("^\\$\\p{Alpha}+:{0,2}\\p{Blank}*(.*?)\\p{Blank}*\\$");
149 ilm 56
 
57
    /**
151 ilm 58
     * Return the value of the subversion keyword. E.g. use with
149 ilm 59
     * <code>private static final String REV = getKeywordValue("$Rev$")</code> (don't forget to
60
     * <code>svn propset svn:keywords</code>).
61
     *
151 ilm 62
     * @param substitute the svn substitute, trailing characters not part of the keyword will be
63
     *        ignored, e.g. "$Rev: 12 $ other chars".
149 ilm 64
     * @return the value, empty string if none, e.g. "12".
65
     * @throws IllegalArgumentException if substitute is not valid.
66
     */
67
    public static String getKeywordValue(String substitute) throws IllegalArgumentException {
68
        final Matcher matcher = KEYWORD_PATTERN.matcher(substitute);
151 ilm 69
        if (!matcher.find())
149 ilm 70
            throw new IllegalArgumentException("SVN format not recognized");
71
 
72
        return matcher.group(1);
73
    }
17 ilm 74
}