1 19 20 package org.netbeans.modules.subversion.ui.update; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.*; 25 26 import org.netbeans.modules.subversion.*; 27 import org.netbeans.modules.subversion.Subversion; 28 import org.netbeans.modules.subversion.client.SvnClient; 29 import org.netbeans.modules.subversion.client.SvnProgressSupport; 30 import org.netbeans.modules.subversion.ui.actions.ContextAction; 31 import org.netbeans.modules.subversion.util.*; 32 import org.netbeans.modules.subversion.util.Context; 33 import org.netbeans.modules.versioning.util.Utils; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.FileStateInvalidException; 38 import org.openide.nodes.Node; 39 import org.tigris.subversion.svnclientadapter.ISVNInfo; 40 import org.tigris.subversion.svnclientadapter.SVNClientException; 41 import org.tigris.subversion.svnclientadapter.SVNRevision; 42 import org.tigris.subversion.svnclientadapter.SVNUrl; 43 44 49 public class RevertModificationsAction extends ContextAction { 50 51 52 public RevertModificationsAction() { 53 } 54 55 protected String getBaseName(Node[] activatedNodes) { 56 return "CTL_MenuItem_Revert"; } 58 59 protected int getFileEnabledStatus() { 60 return FileInformation.STATUS_VERSIONED & ~FileInformation.STATUS_VERSIONED_NEWINREPOSITORY; 61 } 62 63 protected int getDirectoryEnabledStatus() { 64 return FileInformation.STATUS_VERSIONED & ~FileInformation.STATUS_VERSIONED_NEWINREPOSITORY; 65 } 66 67 protected void performContextAction(final Node[] nodes) { 68 final Context ctx = getContext(nodes); 69 final File root = ctx.getRootFiles()[0]; 70 final SVNUrl url = SvnUtils.getRepositoryRootUrl(root); 71 final RepositoryFile repositoryFile = new RepositoryFile(url, url, SVNRevision.HEAD); 72 73 final RevertModifications revertModifications = new RevertModifications(repositoryFile); 74 if(!revertModifications.showDialog()) { 75 return; 76 } 77 78 ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) { 79 public void perform() { 80 performRevert(ctx, revertModifications, this); 81 } 82 }; 83 support.start(createRequestProcessor(nodes)); 84 } 85 86 87 public static void performRevert(Context ctx, RevertModifications revertModifications, SvnProgressSupport support) { 88 SvnClient client; 89 try { 90 client = Subversion.getInstance().getClient(ctx, support); 91 } catch (SVNClientException ex) { 92 ErrorManager.getDefault().notify(ex); 93 return; 94 } 95 96 File files[] = ctx.getFiles(); 97 File [][] split = Utils.splitFlatOthers(files); 98 for (int c = 0; c<split.length; c++) { 99 if(support.isCanceled()) { 100 return; 101 } 102 files = split[c]; 103 boolean recursive = c == 1; 104 if (recursive == false) { 105 files = SvnUtils.flatten(files, FileInformation.STATUS_REVERTIBLE_CHANGE); 106 } 107 108 try { 109 RevertModifications.RevisionInterval revisions = revertModifications.getRevisionInterval(); 110 if(revisions != null) { 111 for (int i= 0; i<files.length; i++) { 112 if(support.isCanceled()) { 113 return; 114 } 115 SVNUrl url = SvnUtils.getRepositoryUrl(files[i]); 116 revisions = recountStartRevision(client, url, revisions); 117 client.merge(url, revisions.endRevision, url, revisions.startRevision, files[i], false, recursive); 118 } 119 } else { 120 if(support.isCanceled()) { 121 return; 122 } 123 if(files.length > 0 ) { 124 client.revert(files, recursive); 125 } 126 } 127 } catch (SVNClientException ex) { 128 support.annotate(ex); 129 } 130 } 131 132 if(support.isCanceled()) { 133 return; 134 } 135 136 if(revertModifications.revertNewFiles()) { 137 File [] newfiles = Subversion.getInstance().getStatusCache().listFiles(ctx.getRootFiles(), FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY); 138 for (int i = 0; i < newfiles.length; i++) { 139 FileObject fo = FileUtil.toFileObject(newfiles[i]); 140 try { 141 if(fo != null) { 142 fo.delete(); 143 } 144 } 145 catch (IOException ex) { 146 ErrorManager.getDefault().notify(ex); 147 } 148 } 149 } 150 } 151 152 158 private static void refreshRecursively(File file) { 159 File [] files = file.listFiles(); 160 if (files != null) { 161 for (File child : files) { 162 refreshRecursively(child); 163 } 164 } 165 FileStatusCache cache = Subversion.getInstance().getStatusCache(); 166 cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 167 } 168 169 private static void addFileSystem(Set filesystems, File file) { 170 FileObject fo; 171 for (;;) { 172 fo = FileUtil.toFileObject(file); 173 if (fo != null) break; 174 file = file.getParentFile(); 175 if (file == null) return; 176 } 177 try { 178 filesystems.add(fo.getFileSystem()); 179 } catch (FileStateInvalidException e) { 180 } 182 } 183 184 private static RevertModifications.RevisionInterval recountStartRevision(SvnClient client, SVNUrl repository, RevertModifications.RevisionInterval ret) throws SVNClientException { 185 if(ret.startRevision.equals(SVNRevision.HEAD)) { 186 ISVNInfo info = client.getInfo(repository); 187 ret.startRevision = info.getRevision(); 188 } 189 long start = Long.parseLong(ret.startRevision.toString()); 190 if(start > 0) { 191 start = start - 1; 192 } 193 ret.startRevision = new SVNRevision.Number(start); 194 return ret; 195 } 196 197 } 198 | Popular Tags |