1 19 20 package org.netbeans.modules.subversion; 21 22 import java.io.*; 23 import org.netbeans.modules.subversion.client.*; 24 import org.netbeans.modules.subversion.ui.diff.Setup; 25 import org.netbeans.modules.subversion.util.*; 26 import org.netbeans.modules.subversion.util.FileUtils; 27 import org.openide.filesystems.FileUtil; 28 import org.tigris.subversion.svnclientadapter.*; 29 30 36 public class VersionsCache { 37 38 private static VersionsCache instance; 39 40 41 private VersionsCache() { 42 } 43 44 public static synchronized VersionsCache getInstance() { 45 if (instance == null) { 46 instance = new VersionsCache(); 47 } 48 return instance; 49 } 50 51 59 public File getFileRevision(File base, String revision) throws IOException { 60 if (Setup.REVISION_BASE.equals(revision)) { 61 try { 62 File svnDir = getMetadataDir(base.getParentFile()); 63 if (svnDir == null) return null; 64 File svnBase = new File(svnDir, "text-base/" + base.getName() + ".svn-base"); 65 if (!svnBase.exists()) return null; 66 File expanded = new File(svnDir, "text-base/" + base.getName() + ".netbeans-base"); 67 if (expanded.canRead() && svnBase.isFile() && expanded.lastModified() > svnBase.lastModified()) { 68 return expanded; 69 } 70 SvnClient client = Subversion.getInstance().getClient(base); 71 InputStream in = client.getContent(base, SVNRevision.BASE); 72 expanded = FileUtil.normalizeFile(expanded); 73 expanded.deleteOnExit(); 74 FileUtils.copyStreamToFile(new BufferedInputStream(in), expanded); 75 return expanded; 76 } catch (SVNClientException e) { 77 return null; 78 } 79 } else if (Setup.REVISION_PRISTINE.equals(revision)) { 80 String name = base.getName(); 81 File svnDir = getMetadataDir(base.getParentFile()); 82 if (svnDir != null) { 83 File text_base = new File(svnDir, "text-base"); File pristine = new File(text_base, name + ".svn-base"); if (pristine.isFile()) { 86 return pristine; 87 } else { 88 return null; 89 } 90 } else { 91 return null; 92 } 93 } else if (Setup.REVISION_CURRENT.equals(revision)) { 94 return base; 95 } else { 96 SVNRevision svnrevision; 97 if (Setup.REVISION_HEAD.equals(revision)) { 98 svnrevision = SVNRevision.HEAD; 99 } else { 100 svnrevision = new SVNRevision.Number(Long.parseLong(revision)); 101 } 102 try { 103 SvnClient client = Subversion.getInstance().getClient(base); 104 FileStatusCache cache = Subversion.getInstance().getStatusCache(); 105 InputStream in; 106 if ((cache.getStatus(base).getStatus() & FileInformation.STATUS_VERSIONED) != 0) { 107 in = client.getContent(base, svnrevision); 108 } else { 109 SVNUrl url = SvnUtils.getRepositoryUrl(base); 110 if (url != null) { 111 url = url.appendPath("@" + revision); 112 in = client.getContent(url, svnrevision); 113 } else { 114 in = new ByteArrayInputStream(org.openide.util.NbBundle.getMessage(VersionsCache.class, "MSG_UnknownURL").getBytes()); } 116 } 117 File tmp = File.createTempFile("nb-svn", base.getName()); tmp = FileUtil.normalizeFile(tmp); 120 tmp.deleteOnExit(); FileUtils.copyStreamToFile(new BufferedInputStream(in), tmp); 122 return tmp; 123 } catch (SVNClientException ex) { 124 IOException ioex = new IOException("Can not load: " + base.getAbsolutePath() + " in revision: " + revision); ioex.initCause(ex); 126 throw ioex; 127 } 128 } 129 130 } 140 141 private File getMetadataDir(File dir) { 142 File svnDir = new File(dir, ".svn"); if (!svnDir.isDirectory()) { 144 svnDir = new File(dir, "_svn"); if (!svnDir.isDirectory()) { 146 return null; 147 } 148 } 149 return svnDir; 150 } 151 } 152 | Popular Tags |