Subversion Repositories XServices

Rev

Rev 150 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.svn;
17
 
18
import java.io.BufferedReader;
19
import java.io.File;
20
import java.io.IOException;
21
import java.io.InputStreamReader;
22
import java.util.ArrayList;
23
import java.util.Date;
24
import java.util.List;
25
import java.util.StringTokenizer;
26
 
27
import net.brutex.svn.SVNCommitInfo.ChangeType;
28
 
29
import org.apache.log4j.Logger;
30
 
31
/** Executes the svnlook utility
32
 *
33
 * @author Brian Rosenberger bru(at)brutex.de
34
 * @since 0.1
35
 
36
 */
37
public class SVNLookExecutor {
38
 
39
	private Logger logger = Logger.getLogger(SVNLookExecutor.class);
40
	private final File svnlook;
41
	private final String repos;
42
	private String TXN = null;
43
	private String rev = null;
44
 
45
	/**
46
	 * Instantiates a new sVN look executor.
47
	 *
48
	 * @param svnlook the svnlook
49
	 * @param repos the repos
50
	 */
51
	public SVNLookExecutor(File svnlook, String repos) {
52
		if(! svnlook.exists() ) throw new IllegalArgumentException( String.format("The svnlook executable at '%s' does not exist.", svnlook.toString()));
53
		if(! svnlook.isFile() ) throw new IllegalArgumentException( String.format("The svnlook utility at'%s' is not a file.", svnlook.toString()));
54
		logger.debug(String.format("Instantiating '%s' with svnlook at '%s'.", this.getClass().getCanonicalName(), svnlook.toString()));
55
		this.svnlook = svnlook;
56
		logger.debug(String.format("Working against svn repository at '%s'.", repos));
57
		this.repos = repos;
58
	}
59
 
60
 
61
	/**
62
	 * Execute svn look.
63
	 *
64
	 * @param command the command
65
	 * @return the string
66
	 */
67
	private String executeSVNLook(SVNLookCommand command) {
68
		StringBuilder sb = new StringBuilder();
69
		StringBuilder sberr = new StringBuilder();
70
 
71
		//This throws an IllegalArgumentException when neither TXN nor REV is valid
72
		String[] params;
73
		try {
74
			params = getTargetParam();
75
		} catch (IllegalArgumentException e) {
76
			logger.error(e.getMessage());
77
			throw e;
78
		}
79
		try {
80
			List<String> cmdline =  new ArrayList<String>(5);
81
			cmdline.add(svnlook.toString());
82
			cmdline.add(command.getValue());
83
			cmdline.add(params[0]);
84
			cmdline.add(params[1]);
85
			cmdline.add(repos);
86
 
87
			ProcessBuilder pf = new ProcessBuilder(cmdline);
88
			logger.debug(String.format("Executing svnlook with commandline '%s'.", pf.command()));
89
 
90
			Process svnprocess = pf.start();
91
			BufferedReader stdin = new BufferedReader(new InputStreamReader(svnprocess.getInputStream()));
92
			BufferedReader stderr = new BufferedReader(new InputStreamReader(svnprocess.getErrorStream()));
93
			String s;
94
			 while( (s = stdin.readLine()) != null ) {
95
				 sb.append(s + '\n');
96
			 }
97
			 while( (s = stderr.readLine()) != null ) {
98
				 sberr.append(s + '\n');
99
			 }
100
			 stdin.close(); stderr.close();
101
		} catch (IOException e) {
102
			logger.error( String.format( "Error calling the svnlook utility: '%s'", e.getMessage()));
103
			e.printStackTrace();
104
		}
105
 
106
		String error = sberr.toString();
107
		if( error.length()>0 ) {
108
			error = "Failed to call svnlook. The STDERR was '"+error+"'";
109
			logger.error(error);
110
			throw new IllegalArgumentException(error);
111
		}
112
		String output = sb.toString().trim();
113
		logger.debug(String.format("Svnlook output was '%s'", output));
114
		return output;
115
	}
116
 
117
	/*
118
	 * Returns either -t TXN oder -rev REVISION
119
	 *
120
	 * @returns
121
	 */
122
	private String[] getTargetParam() {
123
		String[] result = new String[2];
124
		if( (TXN==null || TXN.length()<=0) && (rev==null || rev.length()<=0) ) throw new IllegalArgumentException("Either TXN or revision must be provided.");
125
		if( TXN!=null && TXN.length()>0 && rev!=null && rev.length()>0 ) throw new IllegalArgumentException("Both, TXN and revision are given. Don't know what to use.");
126
		if( TXN!=null && TXN.length()>0) {
127
			result[0] = "-t";
128
			result[1] = TXN;
129
			return result;
130
		}
131
		if( rev!=null && rev.length()>0) {
132
			result[0] = "-r";
133
			result[1] = rev;
134
			return result;
135
		}
136
		throw new IllegalArgumentException("Either TXN or revision must be provided.");
137
	}
138
 
139
	public void setTXN(String TXN) {
140
		if(TXN==null || TXN.length()<=0) throw new IllegalArgumentException("TXN cannot be null or empty.");
141
		this.TXN = TXN;
142
	}
143
 
144
	public void setRev(String rev) {
145
		if(rev==null || rev.length()<=0) throw new IllegalArgumentException("Revision cannot be null or empty.");
146
		this.rev = rev;
147
	}
148
 
149
	public SVNCommitInfo getCommitInfo() {
150
		String author = executeSVNLook(SVNLookCommand.AUTHOR);
151
		String logmessage = executeSVNLook(SVNLookCommand.LOG);
152
		String files = executeSVNLook(SVNLookCommand.CHANGED);
153
 
154
		SVNCommitInfo result = new SVNCommitInfo(author,
155
												new Date(),
156
												logmessage);
157
 
158
 
159
		files += "\n";
160
		StringTokenizer tokenizer = new StringTokenizer(files, "\n");
161
		String s;
162
		while( tokenizer.hasMoreTokens()) {
163
			s=tokenizer.nextToken();
164
			logger.debug(String.format("Tokenizing file list. Token '%s'.", s));
165
			if(s.startsWith("A")) { result.addFileInfo(ChangeType.ADDED, s.substring(1).trim()); continue; }
166
			if(s.startsWith("D")) { result.addFileInfo(ChangeType.DELETED, s.substring(1).trim()); continue; }
167
			if(s.startsWith("UU")) { result.addFileInfo(ChangeType.BOTHUPDATE, s.substring(2).trim()); continue; }
168
			if(s.startsWith("_U")) { result.addFileInfo(ChangeType.METAUPDATE, s.substring(2).trim()); continue; }
169
			if(s.startsWith("U")) { result.addFileInfo(ChangeType.UPDATED, s.substring(1).trim()); continue; }
170
		}
171
		result.setTxn(TXN);
172
		result.setRev(rev);
173
 
174
		return result;
175
	}
176
 
177
}