package com.svnlog; import org.tmatesoft.svn.core.*; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; import org.tmatesoft.svn.core.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.SVNWCUtil; import java.text.SimpleDateFormat; import java.util.*; public class SVNLogFetcher { private String url; private String username; private String password; private SVNRepository repository; private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public SVNLogFetcher(String url, String username, String password) throws SVNException { this.url = url; this.username = username; this.password = password; SVNRepositoryFactoryImpl.setup(); this.repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray()); repository.setAuthenticationManager(authManager); } public List fetchLogs(long startRevision, long endRevision) throws SVNException { return fetchLogs(startRevision, endRevision, null); } public List fetchLogs(long startRevision, long endRevision, String filterUser) throws SVNException { List entries = new ArrayList<>(); if (startRevision < 0) { startRevision = repository.getLatestRevision(); } if (endRevision < 0) { endRevision = repository.getLatestRevision(); } if (startRevision > endRevision) { long temp = startRevision; startRevision = endRevision; endRevision = temp; } Collection logEntries = repository.log(new String[]{""}, null, startRevision, endRevision, true, true); for (SVNLogEntry logEntry : logEntries) { String author = logEntry.getAuthor(); // 如果设置了用户名过滤器,则跳过不匹配的记录(包含匹配,不区分大小写) if (filterUser != null && !filterUser.isEmpty() && (author == null || !author.toLowerCase().contains(filterUser.toLowerCase()))) { continue; } LogEntry entry = new LogEntry(); entry.setRevision(logEntry.getRevision()); entry.setAuthor(author != null ? author : "(无作者)"); entry.setDate(logEntry.getDate()); entry.setMessage(logEntry.getMessage() != null ? logEntry.getMessage().trim() : ""); // 获取变更的文件路径 if (logEntry.getChangedPaths() != null) { List paths = new ArrayList<>(); for (Map.Entry pathEntry : logEntry.getChangedPaths().entrySet()) { paths.add(pathEntry.getKey()); } entry.setChangedPaths(paths.toArray(new String[0])); } entries.add(entry); } // 按版本号降序排序 entries.sort((e1, e2) -> Long.compare(e2.getRevision(), e1.getRevision())); return entries; } public long getLatestRevision() throws SVNException { return repository.getLatestRevision(); } public String formatDate(Date date) { return dateFormat.format(date); } public void testConnection() throws SVNException { repository.testConnection(); } }