Last active
January 29, 2021 12:22
-
-
Save ErShakirAnsari/ae0006031ad6697c32bce7f783387089 to your computer and use it in GitHub Desktop.
Tutorial for svn kit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package svnkit; | |
import java.io.File; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import org.tmatesoft.svn.core.SVNCommitInfo; | |
import org.tmatesoft.svn.core.SVNDepth; | |
import org.tmatesoft.svn.core.SVNDirEntry; | |
import org.tmatesoft.svn.core.SVNException; | |
import org.tmatesoft.svn.core.SVNNodeKind; | |
import org.tmatesoft.svn.core.SVNProperties; | |
import org.tmatesoft.svn.core.SVNURL; | |
import org.tmatesoft.svn.core.io.SVNRepository; | |
import org.tmatesoft.svn.core.io.SVNRepositoryFactory; | |
import org.tmatesoft.svn.core.wc.ISVNOptions; | |
import org.tmatesoft.svn.core.wc.SVNClientManager; | |
import org.tmatesoft.svn.core.wc.SVNCommitClient; | |
import org.tmatesoft.svn.core.wc.SVNRevision; | |
import org.tmatesoft.svn.core.wc.SVNUpdateClient; | |
import org.tmatesoft.svn.core.wc.SVNWCUtil; | |
import org.tmatesoft.svn.core.wc2.SvnOperationFactory; | |
import org.tmatesoft.svn.core.wc2.SvnRemoteDelete; | |
import org.tmatesoft.svn.core.wc2.SvnTarget; | |
/** | |
* | |
* @author Shakir | |
* @date Jan 29, 2021 - 10:43:24 AM | |
*/ | |
public class SVNKit | |
{ | |
public static final int ENTRY_ALREADY_EXISTS = 150002; | |
public static void main(String[] args) throws Exception | |
{ | |
// exportFromSvn(getSVNClientManager()); | |
// commitToSvn(getSVNClientManager()); | |
delete(); | |
SVNRepository svnRepository = getRepo(); | |
listEntries(svnRepository, "trunk/test"); | |
long latestRevision = svnRepository.getLatestRevision(); | |
System.out.println("Repository latest revision: " + latestRevision); | |
} | |
public static SVNRepository getRepo() throws SVNException | |
{ | |
SVNURL url = SVNURL.parseURIEncoded("https://github.com/ErShakirAnsari/Notes.git"); | |
return SVNRepositoryFactory.create(url); | |
} | |
public static SVNClientManager getSVNClientManager() throws Exception | |
{ | |
SVNURL url = SVNURL.parseURIEncoded("https://github.com/ErShakirAnsari/Notes.git"); | |
SVNRepository repository = SVNRepositoryFactory.create(url); | |
ISVNOptions myOptions = SVNWCUtil.createDefaultOptions(true); | |
//provide svn username and password | |
//username = name used to connect to svn | |
//password = password used to connect to svn | |
// ISVNAuthenticationManager myAuthManager = SVNWCUtil.createDefaultAuthenticationManager("<username>", "<password>"); | |
// repository.setAuthenticationManager(myAuthManager); | |
//clientManager will be used to get different kind of svn clients instances to do different activities | |
//like update, commit, view diff etc. | |
// SVNClientManager clientManager = SVNClientManager.newInstance(myOptions, myAuthManager); | |
SVNClientManager clientManager = SVNClientManager.newInstance(myOptions); | |
System.out.println("SVNKit.getSVNClientManager() - " + clientManager); | |
//SVNLogClient sVNLogClient = clientManager.getLogClient(); | |
//clientManager.getChangelistClient(). | |
return clientManager; | |
} | |
public static void commitToSvn(SVNClientManager clientManager) throws SVNException | |
{ | |
SVNCommitClient commitClient = clientManager.getCommitClient(); | |
File fileToCheckin = new File("F:\\Main.java"); | |
boolean recursive = true; | |
boolean useGlobalIgnores = true; | |
// SVNURL svnurl = SVNURL.parseURIDecoded("https://github.com/ErShakirAnsari/Notes/trunk/test/Main-1.java"); | |
SVNURL svnurl = SVNURL.parseURIEncoded("https://github.com/ErShakirAnsari/Notes/trunk/test/Main-3.java"); | |
String comment = "testing svn kit integration"; | |
SVNProperties svnProperties = new SVNProperties(); | |
svnProperties.put("author", "Demo"); | |
// SVNCommitInfo importInfo = commitClient.doImport(fileToCheckin, svnurl,comment, recursive); | |
SVNCommitInfo importInfo = commitClient.doImport(fileToCheckin, svnurl, comment, svnProperties, useGlobalIgnores, recursive, SVNDepth.EMPTY); | |
System.out.println(importInfo.getNewRevision()); | |
} | |
public static void exportFromSvn(SVNClientManager clientManager) throws SVNException | |
{ | |
SVNUpdateClient updateClient = clientManager.getUpdateClient(); | |
SVNURL url = SVNURL.parseURIEncoded("https://github.com/ErShakirAnsari/Notes.git"); | |
//destination path | |
File dstPath = new File("E:\\svn"); | |
//the revision number which should be looked upon for the file path | |
SVNRevision pegRevision = SVNRevision.create(1); | |
//the revision number which is required to be exported. | |
SVNRevision revision = SVNRevision.create(100); | |
//if there is any special character for end of line (in the file) then it is required. For our use case, //it can be null, assuming there are no special characters. In this case the OS specific EoF style will //be assumed | |
String eolStyle = null; | |
//this would force the operation | |
boolean force = true; | |
//Till what extent under a directory, export is required, is determined by depth. INFINITY means the whole subtree of that directory will be exported | |
SVNDepth recursive = SVNDepth.INFINITY; | |
updateClient.doExport(url, dstPath, pegRevision, revision, eolStyle, force, recursive); | |
} | |
public static void listEntries(SVNRepository repository, String path) throws SVNException | |
{ | |
Collection entries = repository.getDir(path, -1, null, (Collection) null); | |
Iterator iterator = entries.iterator(); | |
while (iterator.hasNext()) | |
{ | |
SVNDirEntry entry = (SVNDirEntry) iterator.next(); | |
System.out.println("/" + (path.equals("") ? "" : path + "/") + entry.getName() | |
+ " ( author: '" + entry.getAuthor() + "'; revision: " + entry.getRevision() + "; date: " + entry.getDate() + ")" | |
+ "- size:" + entry.getSize() + " - msg:" + entry.getCommitMessage()); | |
if (entry.getKind() == SVNNodeKind.DIR) | |
{ | |
listEntries(repository, (path.equals("")) ? entry.getName() : path + "/" + entry.getName()); | |
} | |
} | |
} | |
public static void delete() throws SVNException | |
{ | |
final SvnOperationFactory svnOperationFactory = new SvnOperationFactory(); | |
try | |
{ | |
//svnOperationFactory.setAuthenticationManager(authenticationManager); | |
final SvnRemoteDelete remoteDelete = svnOperationFactory.createRemoteDelete(); | |
remoteDelete.setSingleTarget(SvnTarget.fromURL(SVNURL.parseURIEncoded("https://github.com/ErShakirAnsari/Notes/trunk/test/Main-3.java"))); | |
remoteDelete.setCommitMessage("Delete a file from the repository"); | |
final SVNCommitInfo commitInfo = remoteDelete.run(); | |
if (commitInfo != null) | |
{ | |
final long newRevision = commitInfo.getNewRevision(); | |
System.out.println("Removed a file, revision " + newRevision + " created"); | |
} | |
} finally | |
{ | |
svnOperationFactory.dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment