Subversion Repositories XServices

Rev

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

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