Skip to content

Instantly share code, notes, and snippets.

@vs
Created September 27, 2013 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vs/6728389 to your computer and use it in GitHub Desktop.
Save vs/6728389 to your computer and use it in GitHub Desktop.
package org.tmatesoft.translator.client;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc2.ISvnObjectReceiver;
import org.tmatesoft.svn.core.wc2.SvnLog;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
import org.tmatesoft.svn.core.wc2.SvnRevisionRange;
import org.tmatesoft.svn.core.wc2.SvnTarget;
import org.tmatesoft.svn.core.wc2.admin.SvnRepositoryGetFileSize;
public class LargeFilesScanner {
private static final int MB = 1024 * 1024;
private static final long FILE_SIZE_THRESHOLD = 10 * MB;
// private static final Pattern FILE_PATTERN = Pattern.compile("^.+\\.doc$");
private static final Pattern FILE_PATTERN = Pattern.compile("^.+$");
public static void main(String[] args) throws SVNException, IOException {
if (args.length < 1) {
throw new IllegalArgumentException("Subversion repository path is missing");
}
final LargeFilesScanner scanner = new LargeFilesScanner(new File(args[0]));
scanner.run();
}
private final File repositoryRoot;
public LargeFilesScanner(File repositoryRoot) {
this.repositoryRoot = repositoryRoot;
}
private void run() throws SVNException, IOException {
System.err.println("Started");
final long startTime = System.currentTimeMillis();
final SvnOperationFactory factory = new SvnOperationFactory();
final SvnLog log = factory.createLog();
log.setSingleTarget(SvnTarget.fromURL(SVNURL.fromFile(repositoryRoot)));
log.setRevision(SVNRevision.HEAD);
final List<SvnRevisionRange> ranges = new ArrayList<SvnRevisionRange>();
ranges.add(SvnRevisionRange.create(SVNRevision.create(0), SVNRevision.HEAD));
log.setRevisionRanges(ranges);
log.setDiscoverChangedPaths(true);
log.setStopOnCopy(false);
log.setUseMergeHistory(false);
log.setReceiver(new ISvnObjectReceiver<SVNLogEntry>() {
public void receive(SvnTarget target, SVNLogEntry logEntry) throws SVNException {
if (logEntry == null) {
return;
}
System.err.printf("r%d: ", logEntry.getRevision());
int counter = 0;
for (SVNLogEntryPath changedEntry : logEntry.getChangedPaths().values()) {
if (changedEntry == null) {
continue;
}
if (changedEntry.getType() == SVNLogEntryPath.TYPE_DELETED) {
continue;
}
if (changedEntry.getKind() != SVNNodeKind.FILE) {
continue;
}
if (!pathMatches(changedEntry)) {
continue;
}
final SvnRepositoryGetFileSize getRepositoryFileSize = factory.createGetRepositoryFileSize();
getRepositoryFileSize.setRepositoryRoot(repositoryRoot);
getRepositoryFileSize.setPath(changedEntry.getPath());
getRepositoryFileSize.setRevision(SVNRevision.create(logEntry.getRevision()));
final Long size = getRepositoryFileSize.run();
if (size < FILE_SIZE_THRESHOLD) {
continue;
}
try {
record(logEntry.getRevision(), changedEntry);
counter++;
} catch (IOException e) {
System.err.printf("Failed to process r%d %s\n", logEntry.getRevision(), changedEntry);
}
}
System.err.printf("%d entries found\n", counter);
}
});
log.run();
final SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.err.printf("Done; time spent: %s\n", dateFormat.format(new Date(System.currentTimeMillis() - startTime)));
}
private boolean pathMatches(SVNLogEntryPath changedEntry) {
if (FILE_PATTERN.matcher(changedEntry.getPath()).matches()) {
return true;
}
if (changedEntry.getCopyPath() != null && FILE_PATTERN.matcher(changedEntry.getCopyPath()).matches()) {
return true;
}
return false;
}
private void record(long revision, SVNLogEntryPath changedEntry) throws IOException {
System.out.printf("r%d %s\n", revision, changedEntry.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment