1 16 17 package info.jtrac.util; 18 19 import java.util.Collection ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.TreeMap ; 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 40 public class SvnUtils { 41 42 public static SVNRepository getRepository(String url, String username, String 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 e) { 53 throw new RuntimeException (e); 54 } 55 if (nodeKind == SVNNodeKind.NONE) { 56 throw new RuntimeException ("There is no entry at '" + url + "'."); 57 } else if (nodeKind == SVNNodeKind.FILE) { 58 throw new RuntimeException ("The entry at '" + url + "' is a file while a directory was expected."); 59 } 60 return repository; 61 } 62 63 public static Map <String , Integer > getCommitsPerCommitter(SVNRepository repository) { 64 Collection <SVNLogEntry> svnLogEntries = null; 65 try { 66 svnLogEntries = repository.log(new String [] {""}, null, 0, repository.getLatestRevision(), true, true); 67 } catch (Exception e) { 68 throw new RuntimeException (e); 69 } 70 Date createdDate = null; 71 long now = new Date ().getTime(); 72 long commits = 0; 73 Map <String , Integer > commitsPerCommitter = new TreeMap <String , Integer >(); 74 Map <String , Integer > commitsPerFile = new TreeMap <String , Integer >(); 75 for (SVNLogEntry entry : svnLogEntries) { 76 if(entry.getAuthor() == null || entry.getDate() == null) { 77 continue; 79 } 80 if (createdDate == null) { 81 createdDate = entry.getDate(); 82 } 83 commits++; 84 long age = now - entry.getDate().getTime(); 85 String committer = trimName(entry.getAuthor()); 86 Integer commitsByThisCommitter = commitsPerCommitter.get(committer); 87 commitsPerCommitter.put(committer, (commitsByThisCommitter == null) ? 1 : commitsByThisCommitter + 1); 88 } 89 return commitsPerCommitter; 90 } 91 92 private static String trimName(String 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 |