KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > util > SvnUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package info.jtrac.util;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.TreeMap JavaDoc;
26 import org.tmatesoft.svn.core.SVNLogEntry;
27 import org.tmatesoft.svn.core.SVNNodeKind;
28 import org.tmatesoft.svn.core.SVNURL;
29 import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
30 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
31 import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
32 import org.tmatesoft.svn.core.io.SVNRepository;
33 import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
34 import org.tmatesoft.svn.core.wc.SVNWCUtil;
35
36 /**
37  * Utilities that talk to and get data from a Subversion repository
38  * using the JavaSvn library
39  */

40 public class SvnUtils {
41     
42     public static SVNRepository getRepository(String JavaDoc url, String JavaDoc username, String JavaDoc password) {
43         DAVRepositoryFactory.setup();
44         SVNRepositoryFactoryImpl.setup();
45         SVNRepository repository = null;
46         SVNNodeKind nodeKind = null;
47         try {
48             repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
49             ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
50             repository.setAuthenticationManager(authManager);
51             nodeKind = repository.checkPath("", -1);
52         } catch (Exception JavaDoc e) {
53             throw new RuntimeException JavaDoc(e);
54         }
55         if (nodeKind == SVNNodeKind.NONE) {
56             throw new RuntimeException JavaDoc("There is no entry at '" + url + "'.");
57         } else if (nodeKind == SVNNodeKind.FILE) {
58             throw new RuntimeException JavaDoc("The entry at '" + url + "' is a file while a directory was expected.");
59         }
60         return repository;
61     }
62     
63     public static Map JavaDoc<String JavaDoc, Integer JavaDoc> getCommitsPerCommitter(SVNRepository repository) {
64         Collection JavaDoc<SVNLogEntry> svnLogEntries = null;
65         try {
66             svnLogEntries = repository.log(new String JavaDoc[] {""}, null, 0, repository.getLatestRevision(), true, true);
67         } catch (Exception JavaDoc e) {
68             throw new RuntimeException JavaDoc(e);
69         }
70         Date JavaDoc createdDate = null;
71         long now = new Date JavaDoc().getTime();
72         long commits = 0;
73         Map JavaDoc<String JavaDoc, Integer JavaDoc> commitsPerCommitter = new TreeMap JavaDoc<String JavaDoc, Integer JavaDoc>();
74         Map JavaDoc<String JavaDoc, Integer JavaDoc> commitsPerFile = new TreeMap JavaDoc<String JavaDoc, Integer JavaDoc>();
75         for (SVNLogEntry entry : svnLogEntries) {
76             if(entry.getAuthor() == null || entry.getDate() == null) {
77                 // skip invalid log entry
78
continue;
79             }
80             if (createdDate == null) {
81                 createdDate = entry.getDate();
82             }
83             commits++;
84             long age = now - entry.getDate().getTime();
85             String JavaDoc committer = trimName(entry.getAuthor());
86             Integer JavaDoc commitsByThisCommitter = commitsPerCommitter.get(committer);
87             commitsPerCommitter.put(committer, (commitsByThisCommitter == null) ? 1 : commitsByThisCommitter + 1);
88         }
89         return commitsPerCommitter;
90     }
91     
92     private static String JavaDoc trimName(String JavaDoc in) {
93         int pos = in.indexOf('\\');
94         if (pos != -1) {
95             return in.substring(pos + 1).toLowerCase();
96         }
97         return in.toLowerCase();
98     }
99     
100 }
101
Popular Tags