[JAVA] 자바 SVN 이력 / get SVN history example / get svn change log example

[JAVA] 자바 SVN 이력 / get SVN history example / get svn change log example 

자바 코드로 SVN 히스토리를 가져오는 방법.

구글링이 쉽지 않았으나 결국 찾아냈다.

1. 라이브러리 다운로드

라이브러리는 svnkit-1.9.3.jar 파일을 사용하였다.

아래 주소에서 다운로드받을 수 있다.

https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit/1.9.3

2. SVN Change Log 가져오기 예제

public static void printSVNChangeLog() throws Exception {

        String url = “svn://실제_접속가능한_SVN주소를_입력하기”;
        // String svnUser = “”;
        // String svnPassword = “”;
       
        SVNURL svnUrl = SVNURL.parseURIDecoded(url);
        SVNRepository svnRepo = SVNRepositoryFactory.create(svnUrl);

        // BasicAuthenticationManager authManager = new BasicAuthenticationManager(svnUser, svnPassword);
        // svnRepo.setAuthenticationManager(authManager);

        long latestRevision = svnRepo.getLatestRevision();
        System.out.println(“latestRevision : ” + latestRevision);

        long startRevision = latestRevision;
        long endRevision = latestRevision;

        Collection<SVNLogEntry> logEntries = null;
        logEntries = svnRepo.log(new String[] {“”}, null, startRevision, endRevision, true, true);

        Iterator entries = logEntries.iterator();
        while (entries.hasNext()) {
            SVNLogEntry logEntry = (SVNLogEntry) entries.next();
            if (logEntry == null) {
                continue;
            }
           
            System.out.println(“———————————————“);
            System.out.println(“revision: ” + logEntry.getRevision());
            System.out.println(“author: ” + logEntry.getAuthor());
            System.out.println(“date: ” + logEntry.getDate());
            System.out.println(“log message: ” + logEntry.getMessage());
           
            if (logEntry.getChangedPaths() == null || logEntry.getChangedPaths().size() == 0) {
                continue;
            }

            System.out.println();
            System.out.println(“changed paths:”);
            Set changedPathsSet = logEntry.getChangedPaths().keySet();
            Iterator changedPaths = changedPathsSet.iterator();
            while (changedPaths.hasNext()) {
                SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry.getChangedPaths().get(changedPaths.next());
               
                if (entryPath.getCopyPath() != null) {
                    System.out.println(” ” + entryPath.getType() + ” ” + entryPath.getPath() + “(from ” + entryPath.getCopyPath() + ” revision ” + entryPath.getCopyRevision() + “)”);
                } else {
                    System.out.println(” ” + entryPath.getType() + ” ” + entryPath.getPath());
                }
            }
        }
    }

특정 SVN 주소에 대해서 startRevision 부터 endRevision 까지 커밋 이력을 출력한다.

예를 들어 startRevision = 1 이고 endRevision = 10 이면 1부터 10을 순서대로 모두 출력한다.

현재 코드는 가장 최신 리비전 이력만 출력한다.

위 코드에서는 권한 문제가 발생하지 않아서 SVN 유저 이름과 패스워드를 입력하지 않았는데(해당 부분 주석처리함),

필요하다면 주석처리를 풀면 된다.

결과는 아래와 같이 나온다.

아래 주소를 참고하여 작성했다.

참고페이지) https://wiki.svnkit.com/Printing_Out_Repository_History

3. SVN 특정 리비전의 파일 내용 가져오기 예제

public static void getSVNFileContentToFile(SVNRepository svnRepo, long revision, String path) {
        FileOutputStream outputStream = null;
        File file = null;
       
        try {
            file = new File(“temp.txt”);
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            } else {
                file.createNewFile();
            }
           
            outputStream = new FileOutputStream(file);
            // svnRepo.getFile(path, SVNRevision.HEAD.getNumber(), new SVNProperties(), outputStream);
            svnRepo.getFile(path, revision, new SVNProperties(), outputStream);
           
        } catch (Exception e) {
            e.printStackTrace();
           
        } finally {
            flush(outputStream);
            close(outputStream);
        }
    }
   
   
    public static void getSVNFileContent(SVNRepository svnRepo, long revision, String path) {
        ByteArrayOutputStream outputStream = null;
       
        try {
            outputStream = new ByteArrayOutputStream();
            // svnRepo.getFile(path, SVNRevision.HEAD.getNumber(), new SVNProperties(), outputStream);
            svnRepo.getFile(path, revision, new SVNProperties(), outputStream);
           
            System.out.println(outputStream.toString());
           
           
        } catch (Exception e) {
            e.printStackTrace();
           
        } finally {
            flush(outputStream);
            close(outputStream);
        }
    }
   
   
    public static void flush(OutputStream outputStream) {
        try {
            if (outputStream != null) {
                outputStream.flush();
            }
        } catch (Exception e) {
            // ignore
        }
    }
   
   
    public static void close(OutputStream outputStream) {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (Exception e) {
            // ignore
        }
    }

특정 리버전에 해당하는 파일 내용을 가져온다.

파일로 저장하려면 getSVNFileContentToFile 메서드를 사용하면 된다. 코드상 temp.txt 에 내용을 쓴다.

스트링으로 저장하려면 getSVNFileContent 메서드를 사용하면 된다.