Search in sources :

Example 11 with SVNClientManager

use of org.tmatesoft.svn.core.wc.SVNClientManager in project sonarqube by SonarSource.

the class SvnScmProvider method computeChangedPaths.

static Set<Path> computeChangedPaths(Path projectBasedir, SVNClientManager clientManager) throws SVNException {
    SVNWCClient wcClient = clientManager.getWCClient();
    SVNInfo svnInfo = wcClient.doInfo(projectBasedir.toFile(), null);
    // SVN path of the repo root, for example: /C:/Users/JANOSG~1/AppData/Local/Temp/x/y
    Path svnRootPath = toPath(svnInfo.getRepositoryRootURL());
    // -> set it to "/" to avoid crashing when using Path.relativize later
    if (svnRootPath.equals(Paths.get(""))) {
        svnRootPath = Paths.get("/");
    }
    // SVN path of projectBasedir, for example: /C:/Users/JANOSG~1/AppData/Local/Temp/x/y/branches/b1
    Path svnProjectPath = toPath(svnInfo.getURL());
    // path of projectBasedir, as "absolute path within the SVN repo", for example: /branches/b1
    Path inRepoProjectPath = Paths.get("/").resolve(svnRootPath.relativize(svnProjectPath));
    // We inspect "svn log" from latest revision until copy-point.
    // The same path may appear in multiple commits, the ordering of changes and removals is important.
    Set<Path> paths = new HashSet<>();
    Set<Path> removed = new HashSet<>();
    SVNLogClient svnLogClient = clientManager.getLogClient();
    svnLogClient.doLog(new File[] { projectBasedir.toFile() }, null, null, null, true, true, 0, svnLogEntry -> svnLogEntry.getChangedPaths().values().forEach(entry -> {
        if (entry.getKind().equals(SVNNodeKind.FILE)) {
            Path path = projectBasedir.resolve(inRepoProjectPath.relativize(Paths.get(entry.getPath())));
            if (isModified(entry)) {
                // Skip if the path is removed in a more recent commit
                if (!removed.contains(path)) {
                    paths.add(path);
                }
            } else if (entry.getType() == SVNLogEntryPath.TYPE_DELETED) {
                removed.add(path);
            }
        }
    }));
    return paths;
}
Also used : Path(java.nio.file.Path) SVNLogEntryPath(org.tmatesoft.svn.core.SVNLogEntryPath) ScmProvider(org.sonar.api.batch.scm.ScmProvider) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) SVNDepth(org.tmatesoft.svn.core.SVNDepth) SvnScmSupport.newSvnClientManager(org.sonar.scm.svn.SvnScmSupport.newSvnClientManager) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager) HashSet(java.util.HashSet) SVNLogClient(org.tmatesoft.svn.core.wc.SVNLogClient) Loggers(org.sonar.api.utils.log.Loggers) Map(java.util.Map) Path(java.nio.file.Path) Logger(org.sonar.api.utils.log.Logger) SVNLogEntryPath(org.tmatesoft.svn.core.SVNLogEntryPath) MalformedURLException(java.net.MalformedURLException) SVNException(org.tmatesoft.svn.core.SVNException) SVNNodeKind(org.tmatesoft.svn.core.SVNNodeKind) Set(java.util.Set) Instant(java.time.Instant) File(java.io.File) SVNRevision(org.tmatesoft.svn.core.wc.SVNRevision) Paths(java.nio.file.Paths) SVNURL(org.tmatesoft.svn.core.SVNURL) SVNWCClient(org.tmatesoft.svn.core.wc.SVNWCClient) BlameCommand(org.sonar.api.batch.scm.BlameCommand) SVNInfo(org.tmatesoft.svn.core.wc.SVNInfo) CheckForNull(javax.annotation.CheckForNull) SVNDiffClient(org.tmatesoft.svn.core.wc.SVNDiffClient) SVNWCClient(org.tmatesoft.svn.core.wc.SVNWCClient) SVNInfo(org.tmatesoft.svn.core.wc.SVNInfo) SVNLogClient(org.tmatesoft.svn.core.wc.SVNLogClient) HashSet(java.util.HashSet)

Example 12 with SVNClientManager

use of org.tmatesoft.svn.core.wc.SVNClientManager in project omegat by omegat-org.

the class TeamTool method initTeamProject.

/**
 * Utility function to create a minimal project to serve as a base for a
 * team project. Will add/stage everything if invoked on a path already
 * containing a git working tree or svn checkout.
 *
 * @param dir
 *            Directory in which to create team project
 * @param srcLang
 *            Source language
 * @param trgLang
 *            Target language
 * @throws Exception
 *             If specified dir is not a directory, is not writeable, etc.
 */
public static void initTeamProject(File dir, String srcLang, String trgLang) throws Exception {
    if (!dir.isDirectory()) {
        throw new IllegalArgumentException("Specified dir is not a directory: " + dir.getPath());
    }
    if (!dir.canWrite()) {
        throw new IOException("Specified dir is not writeable: " + dir.getPath());
    }
    // Create project properties
    ProjectProperties props = new ProjectProperties(dir);
    props.setSourceLanguage(srcLang);
    props.setTargetLanguage(trgLang);
    // Set default tokenizers
    props.setSourceTokenizer(PluginUtils.getTokenizerClassForLanguage(new Language(srcLang)));
    props.setTargetTokenizer(PluginUtils.getTokenizerClassForLanguage(new Language(trgLang)));
    // Create project internal directories
    props.autocreateDirectories();
    // Create version-controlled glossary file
    GlossaryManager.createNewWritableGlossaryFile(props.getWritableGlossaryFile().getAsFile());
    ProjectFileStorage.writeProjectFile(props);
    // Create empty project TM
    new ProjectTMX(props.getSourceLanguage(), props.getTargetLanguage(), props.isSentenceSegmentingEnabled(), null, null).save(props, new File(props.getProjectInternal(), OConsts.STATUS_EXTENSION).getPath(), false);
    // and set EOL handling correctly for cross-platform work
    if (new File(dir, ".svn").isDirectory()) {
        SVNClientManager mgr = SVNClientManager.newInstance();
        mgr.getWCClient().doSetProperty(dir, "svn:auto-props", SVNPropertyValue.create("*.txt = svn:eol-style=native\n*.tmx = svn:eol-style=native\n"), false, SVNDepth.EMPTY, null, null);
        mgr.getWCClient().doAdd(dir.listFiles(f -> !f.getName().startsWith(".")), false, false, true, SVNDepth.fromRecurse(true), false, false, false, true);
    } else if (new File(dir, ".git").isDirectory()) {
        try (BufferedWriter writer = Files.newBufferedWriter(new File(dir, ".gitattributes").toPath())) {
            writer.write("* text=auto\n");
            writer.write("*.tmx text\n");
            writer.write("*.txt text\n");
        }
        Git.open(dir).add().addFilepattern(".").call();
    }
    System.out.println(StringUtil.format(OStrings.getString("TEAM_TOOL_INIT_COMPLETE"), srcLang, trgLang));
}
Also used : ProjectTMX(org.omegat.core.data.ProjectTMX) OConsts(org.omegat.util.OConsts) Arrays(java.util.Arrays) ProjectProperties(org.omegat.core.data.ProjectProperties) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) CLIParameters(org.omegat.CLIParameters) Log(org.omegat.util.Log) SVNDepth(org.tmatesoft.svn.core.SVNDepth) IOException(java.io.IOException) Language(org.omegat.util.Language) PluginUtils(org.omegat.filters2.master.PluginUtils) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager) File(java.io.File) Level(java.util.logging.Level) StringUtil(org.omegat.util.StringUtil) ProjectTMX(org.omegat.core.data.ProjectTMX) OStrings(org.omegat.util.OStrings) SVNPropertyValue(org.tmatesoft.svn.core.SVNPropertyValue) ProjectFileStorage(org.omegat.util.ProjectFileStorage) Git(org.eclipse.jgit.api.Git) Collections(java.util.Collections) GlossaryManager(org.omegat.gui.glossary.GlossaryManager) Preferences(org.omegat.util.Preferences) Language(org.omegat.util.Language) IOException(java.io.IOException) ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager) BufferedWriter(java.io.BufferedWriter)

Example 13 with SVNClientManager

use of org.tmatesoft.svn.core.wc.SVNClientManager in project omegat by omegat-org.

the class SVNRemoteRepository2 method isSVNRepository.

/**
 * Determines whether or not the supplied URL represents a valid Subversion repository.
 *
 * <p>
 * Does the equivalent of <code>svn info <i>url</i></code>.
 *
 * @param url
 *            URL of supposed remote repository
 * @return true if repository appears to be valid, false otherwise
 */
public static boolean isSVNRepository(String url) {
    // Heuristics to save some waiting time
    try {
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        SVNClientManager ourClientManager = SVNClientManager.newInstance(options, (ISVNAuthenticationManager) null);
        ourClientManager.getWCClient().doInfo(SVNURL.parseURIEncoded(SVNEncodingUtil.autoURIEncode(url)), SVNRevision.HEAD, SVNRevision.HEAD);
    } catch (SVNAuthenticationException ex) {
        // https://twitter.com/amadlonkay/status/699716236372889600
        return true;
    } catch (SVNException ex) {
        return false;
    }
    return true;
}
Also used : ISVNOptions(org.tmatesoft.svn.core.wc.ISVNOptions) SVNClientManager(org.tmatesoft.svn.core.wc.SVNClientManager)

Aggregations

SVNClientManager (org.tmatesoft.svn.core.wc.SVNClientManager)13 File (java.io.File)7 SVNLogClient (org.tmatesoft.svn.core.wc.SVNLogClient)5 SVNRevision (org.tmatesoft.svn.core.wc.SVNRevision)4 ZipFile (java.util.zip.ZipFile)3 Test (org.junit.Test)3 InputFile (org.sonar.api.batch.fs.InputFile)3 SVNException (org.tmatesoft.svn.core.SVNException)3 SVNURL (org.tmatesoft.svn.core.SVNURL)3 SVNInfo (org.tmatesoft.svn.core.wc.SVNInfo)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 CheckForNull (javax.annotation.CheckForNull)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)2 SVNDepth (org.tmatesoft.svn.core.SVNDepth)2 ISVNOptions (org.tmatesoft.svn.core.wc.ISVNOptions)2 SVNStatus (org.tmatesoft.svn.core.wc.SVNStatus)2 SVNStatusClient (org.tmatesoft.svn.core.wc.SVNStatusClient)2