February 8, 2014

SVNKit - Check if the given file has the executable flag set

Earlier today I had to code up with code checking if the given SVN url points to a file that has the executable flag set. As Google was not able to find any complete solution using the SVNkit library, here it is:
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.util.SVNEncodingUtil;
import org.tmatesoft.svn.core.internal.util.SVNURLUtil;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;

public class SvnClient {
  private String username;
  private String password;
  
  public SvnClient(String username, String password) {
    super();
    this.username = username;
    this.password = password;
  }

  protected SVNRepository connectToRepo(String url) {
    // For using over http:// and https://
    DAVRepositoryFactory.setup();
        
    try {
      final SVNURL svnUrl = SVNURL.parseURIEncoded(url);

      final SVNRepository repository = SVNRepositoryFactory
        .create(svnUrl);
      
      final ISVNAuthenticationManager authManager = 
        new BasicAuthenticationManager(username, password);
      repository.setAuthenticationManager(authManager);
          
      repository.testConnection();
          
      return repository;
    } catch (SVNException e) {
      throw new RuntimeException("Error while connecting to SVN, url "
        + url, e); 
    }        
  }

  public boolean hasExecutableFlagSet(String url, long revision) {
    final SVNRepository repository = connectToRepo(url);
    try {     
      SVNURL repositoryRoot = repository.getRepositoryRoot(true);
      final String fileRelativeURL = "/" + 
        SVNURLUtil.getRelativeURL(repositoryRoot, 
          SVNURL.parseURIEncoded(url), false);
      
      final SVNProperties properties = new SVNProperties();
            
      repository.getFile(fileRelativeURL, revision, properties, null);

      String value = properties.getStringValue(
        SVNProperty.EXECUTABLE);
      
      return value != null;
    } catch(SVNException e) {
      throw new RuntimeException("Error while checking if object "
        + "has the executable flag set, "
        + "url " + url + ", revision " + revision, e);
    } finally {
      repository.closeSession();
    }
  }
  
}

No comments:

Post a Comment