Subversion Repositories XServices

Rev

Rev 150 | Details | Compare with Previous | 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.util.ArrayList;
19
import java.util.Date;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.regex.Matcher;
23
import java.util.regex.Pattern;
24
 
25
import org.apache.log4j.Logger;
26
 
27
/**
28
 * The Class SVNCommitInfo represents changes commited to
29
 * a svn repository. The information is based on svnlook.
30
 *
31
 * 'A ' Object dem Projektarchiv hinzugefügt
32
 * 'D ' Objekt aus dem Projektarchiv gelöscht
33
 * 'U ' Dateiinhalt geändert
34
 * '_U' Eigenschaften eines Objektes geändert
35
 * 'UU' Dateiinhalt und Eigenschaften geändert
36
 *
37
 * @author Brian Rosenberger, bru(at)brutex.de
38
 * @since 0.1
39
 *
40
 */
41
public class SVNCommitInfo {
42
 
43
	/** The logger. */
44
	private final Logger logger = Logger.getLogger(SVNCommitInfo.class);
45
 
46
	/** The author. */
47
	private final String author;
48
 
49
	/** The commit message */
150 brianR 50
	private String logmessage;
149 brianR 51
 
52
	/** The date. */
53
	private final Date date;
54
 
55
	/** The Alist. */
56
	private final List<String> Alist = new ArrayList<String>(0);
57
 
58
	/** The Dlist. */
59
	private final List<String> Dlist = new ArrayList<String>(0);
60
 
61
	/** The Ulist. */
62
	private final List<String> Ulist = new ArrayList<String>(5);
63
 
64
	/** The _ ulist. */
65
	private final List<String> _Ulist = new ArrayList<String>(0);
66
 
67
	/** The U ulist. */
68
	private final List<String> UUlist = new ArrayList<String>(0);
69
 
70
	/** The issues. */
71
	private final List<String> issues = new ArrayList<String>(1);
72
 
73
	/** The txn. */
74
	private String txn="";
75
 
76
	/** The rev. */
77
	private String rev="";
78
 
79
 
80
	/**
81
	 * Instantiates a new SVN commit info.
82
	 *
83
	 * @param author the commiter
84
	 * @param date the commit date
85
	 * @param logmessage the commit message
86
	 */
87
	public SVNCommitInfo(String author, Date date, String logmessage) {
88
		this.author = author;
89
		this.date = date;
90
		this.logmessage = logmessage;
91
	}
92
 
93
	/**
94
	 * Adds the file info.
95
	 *
96
	 * @param t the t
97
	 * @param file the file
98
	 */
99
	public void addFileInfo(ChangeType t, String file) {
100
		switch (t) {
101
		case ADDED:
102
			Alist.add(file);
103
			break;
104
		case DELETED:
105
			Dlist.add(file);
106
			break;
107
		case UPDATED:
108
			Ulist.add(file);
109
			break;
110
		case METAUPDATE:
111
			_Ulist.add(file);
112
			break;
113
		case BOTHUPDATE:
114
			UUlist.add(file);
115
			break;
116
 
117
		default:
118
			break;
119
		}
120
	}
121
 
122
	/**
123
	 * Gets the author.
124
	 *
125
	 * @return the author
126
	 */
127
	public String getAuthor() {
128
		return author;
129
	}
130
 
131
	/**
132
	 * Gets the commit message.
133
	 *
134
	 * @return the commit message
135
	 */
136
	public String getLogmessage() {
137
		return logmessage;
138
	}
139
 
140
	/**
141
	 * Gets the commit date.
142
	 *
143
	 * @return the commit date
144
	 */
145
	public Date getDate() {
146
		return date;
147
	}
148
 
149
	/**
150
	 * Gets the svn transaction id.
151
	 *
152
	 * @return the txn
153
	 */
154
	public String getTxn() {
155
		return txn;
156
	}
157
 
158
	/**
159
	 * Sets the txn.
160
	 *
161
	 * @param txn the new txn
162
	 */
163
	public void setTxn(String txn) {
164
		this.txn = txn;
165
	}
166
 
167
	/**
168
	 * Gets the id. This is either the txn or revision.
169
	 *
170
	 * @return the id
171
	 */
172
	public String getId() {
173
		if(txn!=null) return txn;
174
		return rev;
175
	}
176
 
177
	/**
178
	 * Sets the rev.
179
	 *
180
	 * @param rev the new rev
181
	 */
182
	public void setRev(String rev) {
183
		this.rev = rev;
184
	}
185
 
186
	/**
187
	 * Gets the rev.
188
	 *
189
	 * @return the rev
190
	 */
191
	public String getRev() {
192
		return rev;
193
	}
194
	/*
195
	 * http://openbook.galileocomputing.de/javainsel9/javainsel_04_007.htm#mjd5b5d84cb3f1b5bcb7638ea9221a491f
196
	 */
197
	/**
198
	 * Parses the issues.
199
	 *
200
	 * @param patterns the patterns
150 brianR 201
	 * @param isRemoveIssues
149 brianR 202
	 */
150 brianR 203
	public void parseIssues(String[] patterns, boolean isRemoveIssues) {
149 brianR 204
		issues.clear(); //reset
205
		int count = 0;
206
		for(String p : patterns) {
207
			Pattern regex = Pattern.compile(p);
208
			Matcher matcher = regex.matcher(logmessage);
209
			logger.debug(String.format("Matching regex pattern '%s' against logmessage '%s'.", matcher.pattern().pattern(), logmessage));
210
			while( matcher.find()) {
211
				issues.add( matcher.group() );
212
				logger.debug("Found  issue '" + matcher.group() + "' in the logmessage.");
213
				count++;
214
			}
150 brianR 215
			if(isRemoveIssues) {
216
				logmessage = matcher.replaceAll("");
217
				logger.debug("Removing all matched issues from commit message");
218
			}
219
 
149 brianR 220
		}
221
		logger.debug("Found '" + count + "' issues in the logmessage.");
222
	}
223
 
224
	/**
225
	 * Gets the change file list as string.
226
	 *
227
	 * @return the change file list as string
228
	 */
229
	public String getChangeFileListAsString() {
230
		StringBuilder sb = new StringBuilder();
231
		for (Iterator<String> iterator = Alist.iterator(); iterator.hasNext();)
232
		{
233
			sb.append("A \t");
234
			sb.append(iterator.next());
235
			sb.append("\n");
236
		}
237
		for (Iterator<String> iterator = Dlist.iterator(); iterator.hasNext();)
238
		{
239
			sb.append("D \t");
240
			sb.append(iterator.next());
241
			sb.append("\n");
242
		}
243
		for (Iterator<String> iterator = Ulist.iterator(); iterator.hasNext();)
244
		{
245
			sb.append("U \t");
246
			sb.append(iterator.next());
247
			sb.append("\n");
248
		}
249
		for (Iterator<String> iterator = _Ulist.iterator(); iterator.hasNext();)
250
		{
251
			sb.append("_U\t");
252
			sb.append(iterator.next());
253
			sb.append("\n");
254
		}
255
		for (Iterator<String> iterator = UUlist.iterator(); iterator.hasNext();)
256
		{
257
			sb.append("UU\t");
258
			sb.append(iterator.next());
259
			sb.append("\n");
260
		}
261
 
262
		sb.append("Summary: " + (Ulist.size()+UUlist.size()+_Ulist.size()) + " files updated, ");
263
		sb.append(Alist.size() + " files added, " + Dlist.size() + " files deleted.");
264
		return sb.toString();
265
	}
266
 
267
	/**
268
	 * Gets the added files.
269
	 *
270
	 * @return the added files
271
	 */
272
	public List<String> getAddedFiles() {
273
		return Alist;
274
	}
275
 
276
	/**
277
	 * Gets the deleted files.
278
	 *
279
	 * @return the deleted files
280
	 */
281
	public List<String> getDeletedFiles() {
282
		return Dlist;
283
	}
284
 
285
	/**
286
	 * Gets the changed files.
287
	 *
288
	 * @return the changed files
289
	 */
290
	public List<String> getChangedFiles() {
291
		List<String> changed = new ArrayList<String>();
292
		changed.addAll(Ulist);
293
		changed.addAll(UUlist);
294
		changed.addAll(_Ulist);
295
		return changed;
296
	}
297
 
298
	/**
299
	 * Gets the issues.
300
	 *
301
	 * @return the issues
302
	 */
303
	public List<String> getIssues() {
304
		return issues;
305
	}
306
 
307
 
308
	/**
309
	 * The Enum ChangeType.
310
	 *
311
	 * @author Brian Rosenberger, bru(at)brutex.de
312
	 */
313
	public enum ChangeType {
314
 
315
		/** The added. */
316
		ADDED("A "),
317
 /** The deleted. */
318
 DELETED("D "),
319
 /** The updated. */
320
 UPDATED("U "),
321
 /** The metaupdate. */
322
 METAUPDATE("_U"),
323
 /** The bothupdate. */
324
 BOTHUPDATE("UU");
325
 
326
		/** The indicator. */
327
		private final String indicator;
328
 
329
		/**
330
		 * Instantiates a new change type.
331
		 *
332
		 * @param svn the svn
333
		 */
334
		private ChangeType(String svn) {
335
			this.indicator = svn;
336
		}
337
 
338
		/**
339
		 * Gets the enum.
340
		 *
341
		 * @param s the s
342
		 * @return the enum
343
		 */
344
		public static ChangeType getEnum(String s) {
345
			for(ChangeType e : ChangeType.values()) {
346
				if(s.equals(e.getIndicator())) return e;
347
			}
348
			throw new IllegalArgumentException("ChangeType enum for value '"+s+"' does not exist.");
349
		}
350
 
351
		/**
352
		 * Gets the indicator.
353
		 *
354
		 * @return the indicator
355
		 */
356
		public String getIndicator() {
357
			return this.indicator;
358
		}
359
	}
360
 
361
}