001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.util;
018:
019: import java.util.Collection;
020: import java.util.Date;
021: import java.util.Map;
022: import java.util.TreeMap;
023: import org.tmatesoft.svn.core.SVNLogEntry;
024: import org.tmatesoft.svn.core.SVNNodeKind;
025: import org.tmatesoft.svn.core.SVNURL;
026: import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
027: import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
028: import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
029: import org.tmatesoft.svn.core.io.SVNRepository;
030: import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
031: import org.tmatesoft.svn.core.wc.SVNWCUtil;
032:
033: /**
034: * Utilities that talk to and get data from a Subversion repository
035: * using the JavaSvn library
036: */
037: public class SvnUtils {
038:
039: public static SVNRepository getRepository(String url,
040: String username, String password) {
041: DAVRepositoryFactory.setup();
042: SVNRepositoryFactoryImpl.setup();
043: SVNRepository repository = null;
044: SVNNodeKind nodeKind = null;
045: try {
046: repository = SVNRepositoryFactory.create(SVNURL
047: .parseURIEncoded(url));
048: ISVNAuthenticationManager authManager = SVNWCUtil
049: .createDefaultAuthenticationManager(username,
050: password);
051: repository.setAuthenticationManager(authManager);
052: nodeKind = repository.checkPath("", -1);
053: } catch (Exception e) {
054: throw new RuntimeException(e);
055: }
056: if (nodeKind == SVNNodeKind.NONE) {
057: throw new RuntimeException("There is no entry at '" + url
058: + "'.");
059: } else if (nodeKind == SVNNodeKind.FILE) {
060: throw new RuntimeException("The entry at '" + url
061: + "' is a file while a directory was expected.");
062: }
063: return repository;
064: }
065:
066: public static Map<String, Integer> getCommitsPerCommitter(
067: SVNRepository repository) {
068: Collection<SVNLogEntry> svnLogEntries = null;
069: try {
070: svnLogEntries = repository.log(new String[] { "" }, null,
071: 0, repository.getLatestRevision(), true, true);
072: } catch (Exception e) {
073: throw new RuntimeException(e);
074: }
075: Date createdDate = null;
076: long now = new Date().getTime();
077: long commits = 0;
078: Map<String, Integer> commitsPerCommitter = new TreeMap<String, Integer>();
079: Map<String, Integer> commitsPerFile = new TreeMap<String, Integer>();
080: for (SVNLogEntry entry : svnLogEntries) {
081: if (entry.getAuthor() == null || entry.getDate() == null) {
082: // skip invalid log entry
083: continue;
084: }
085: if (createdDate == null) {
086: createdDate = entry.getDate();
087: }
088: commits++;
089: long age = now - entry.getDate().getTime();
090: String committer = trimName(entry.getAuthor());
091: Integer commitsByThisCommitter = commitsPerCommitter
092: .get(committer);
093: commitsPerCommitter.put(committer,
094: (commitsByThisCommitter == null) ? 1
095: : commitsByThisCommitter + 1);
096: }
097: return commitsPerCommitter;
098: }
099:
100: private static String trimName(String in) {
101: int pos = in.indexOf('\\');
102: if (pos != -1) {
103: return in.substring(pos + 1).toLowerCase();
104: }
105: return in.toLowerCase();
106: }
107:
108: }
|