java獲取svn中的數據


實現的功能:

  1. 獲取一段時間內的倉庫版本號
  2. 獲取相應版本號的數據信息
  3. 根據作者的賬戶信息,獲取更新記錄

maven信息:

<!-- https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit -->
<dependency>
	<groupId>org.tmatesoft.svnkit</groupId>
	<artifactId>svnkit</artifactId>
	<version>1.9.3</version>
</dependency>
<dependency>
	<groupId>org.assertj</groupId>
	<artifactId>assertj-core</artifactId>
</dependency>

java代碼:

public class SVNUtil {

    private Random random = new Random();

    private static final Logger logger = LoggerFactory.getLogger(SVNUtil.class);
    private String userName = "";
    private String password = "";
    private String url = "";
    
    private ISVNAuthenticationManager authManager;
    private DefaultSVNOptions options; // svn的參數
    private SVNRepository repository; // 倉庫

    public static void main(String[] args) throws SVNException {
        SVNUtil svnUtil = new SVNUtil("season", "123456", "svn://10.10.11.100/project7");
        String author = "season";

        ZonedDateTime zonedDateTime = LocalDate.of(2020, 3, 1).atStartOfDay(ZoneId.systemDefault());
        Date oneMonthAgo = Date.from(zonedDateTime.toInstant());
        SVNLogEntry[] logEntries = svnUtil.listLogByTime(oneMonthAgo, Calendar.getInstance().getTime());

        // 獲取某一用戶提交的記錄
        List<SVNLogEntry> authorLogEntryList = Lists.newArrayList();
        for (SVNLogEntry entry: logEntries) {
            if (author.equals(entry.getAuthor())){
                authorLogEntryList.add(entry);
            }
        }

        // 將提交路徑放入set中
        Set<String> pathSet = Sets.newHashSet();
        for (SVNLogEntry entry: authorLogEntryList) {
            Map<String, SVNLogEntryPath> changedPaths = entry.getChangedPaths();
            Set<String> keySet = changedPaths.keySet();
            pathSet.addAll(keySet);
        }
    }
    
    /**
     * 構造方法
     */
    public SVNUtil(String user, String password, String url) {
        try {
            this.userName = user;
            this.password = password;
            this.url = url;
            init();
        } catch (SVNException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化
     */
    public void init() throws SVNException {
        logger.info("開始加載");
        authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, password.toCharArray());
        options = SVNWCUtil.createDefaultOptions(true);
        options.setDiffCommand("-x -w");
        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        repository.setAuthenticationManager(authManager);
        logger.info("init completed");
    }
    
    /**
     * 獲取一段時間內,所有的commit記錄
     */
    public SVNLogEntry[] listLogByTime(Date start, Date end) throws SVNException {
        long startRevision = repository.getDatedRevision(start); // 獲取某一個時間點的版本號,如果用作開始時間,應該版本號+1,因為我們想要獲取的是后一個版本
        long endRevision = repository.getDatedRevision(end);
        
        Collection<SVNLogEntry> logEntries = repository.log(new String[]{""}, null, startRevision+1, endRevision, true, true);
        SVNLogEntry[] svnLogEntries = logEntries.toArray(new SVNLogEntry[0]);
        return svnLogEntries;
    }
}

轉載自:
https://blog.csdn.net/weixin_41793807/article/details/82699305


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM