r342 - in trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs: . util
Author: tchemit Date: 2008-04-04 20:10:16 +0000 (Fri, 04 Apr 2008) New Revision: 342 Added: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSHandler.java trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/VCSHelper.java Removed: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/AbstractVCSHandler.java trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSHelper.java Log: move to util package all helper classes and abstract implementations Deleted: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/AbstractVCSHandler.java =================================================================== --- trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/AbstractVCSHandler.java 2008-04-04 19:37:59 UTC (rev 341) +++ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/AbstractVCSHandler.java 2008-04-04 20:10:16 UTC (rev 342) @@ -1,207 +0,0 @@ -/* *##% -* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, -* Benjamin Poussin, Tony Chemit -* -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*##%*/ -package org.codelutin.vcs; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codelutin.util.FileUtil; -import org.codelutin.util.ListenerSet; -import org.codelutin.vcs.VCSHandlerEvent.Type; - -import java.io.File; -import java.io.FileFilter; -import java.io.FilenameFilter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.EnumSet; - -/** - * abstract VCSHandler base with usefull methods that does not need vcs specific code. - * - * @author chemit - */ -public abstract class AbstractVCSHandler implements VCSHandler { - - /** to use log facility, just put in your code: log.info(\"...\"); */ - static protected final Log log = LogFactory.getLog(AbstractVCSHandler.class); - - /** - * FilenameFilter to detect all files in a vcs working copy all files and directories that must be handled by vcs - * - * @see org.codelutin.vcs.VCSHandler#getVersionnableFilenameFilter() - */ - protected final FilenameFilter versionnableFilenameFilter; - protected final FileFilter versionnableFileFilter; - protected final String confLocalDirName; - protected final String confLocalEntriesFilename; - protected final VCSConfig config; - - protected ListenerSet<VCSHandlerEventListener> listeners = new ListenerSet<VCSHandlerEventListener>(); - - protected AbstractVCSHandler(VCSConfig config, final String vCSConfLocalDirName, String vCSConfLocalEntriesFilename) { - this.config = config; - this.confLocalDirName = vCSConfLocalDirName; - this.confLocalEntriesFilename = vCSConfLocalEntriesFilename; - this.versionnableFilenameFilter = new FilenameFilter() { - public boolean accept(File dir, String name) { - return !vCSConfLocalDirName.equals(name) && !"CVS".equals(name); - } - }; - this.versionnableFileFilter = new FileFilter() { - public boolean accept(File dir) { - return dir.isFile() || (!vCSConfLocalDirName.equals(dir.getName()) && !"CVS".equals(dir.getName())); - } - }; - fireInit(); - } - - public VCSConfig getConfig() { - return config; - } - - public File getLocalDatabasePath() { - return config.getLocalDatabasePath(); - } - - public void deleteWorkingCopy() { - if (getLocalDatabasePath().exists()) { - FileUtil.deleteRecursively(getLocalDatabasePath()); - getLocalDatabasePath().delete(); - } - } - - public FilenameFilter getVersionnableFilenameFilter() { - return versionnableFilenameFilter; - } - - public FileFilter getVersionnableFileFilter() { - return versionnableFileFilter; - } - - public String getConfLocalDirname() { - return confLocalDirName; - } - - public String getConfLocalEntriesFilename() { - return confLocalEntriesFilename; - } - - public boolean isVersionnableFile(File file) { - assertFileExists(file, "file is empty or non existant"); - return !confLocalDirName.equalsIgnoreCase(file.getName()); - } - - public List<String> getLocalStorageNames(File directory) { - List<String> result = new ArrayList<String>(); - if (directory.exists()) { - getFiles0(directory, result, ""); - } - Collections.sort(result); - return result; - } - - public void addVCSHandlerEventListener(VCSHandlerEventListener l) { - listeners.add(l); - } - - public void removeVCSHandlerEventListener(VCSHandlerEventListener l) { - listeners.remove(l); - } - - public void open() { - fireOpen(); - } - - public void close() { - fireClose(); - } - - protected void fireInit() { - VCSHandlerEvent e = new VCSHandlerEvent(this, Type.INIT); - for (VCSHandlerEventListener l : listeners) { - l.init(e); - } - } - - protected void fireOpen() { - VCSHandlerEvent e = new VCSHandlerEvent(this, Type.OPEN); - for (VCSHandlerEventListener l : listeners) { - l.open(e); - } - } - - protected void fireClose() { - VCSHandlerEvent e = new VCSHandlerEvent(this, Type.CLOSE); - for (VCSHandlerEventListener l : listeners) { - l.close(e); - } - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - close(); - } - - protected void getFiles0(File directory, List<String> result, String prefix) { - //TODO put the hardcored value in a property!!! - if (directory.getName().equals("data")) return; - final String[] strings = directory.list(versionnableFilenameFilter); - boolean first = prefix.equals(""); - final String newPrefix = (first ? "" : prefix + File.separator); - - for (String filename : strings) { - final File dir = new File(directory, filename); - if (dir.isDirectory()) - getFiles0(dir, result, newPrefix + dir.getName()); - else result.add(newPrefix + dir.getName()); - } - } - - protected void assertFileExists(File file, String msg) { - if (file == null || !file.exists()) - throw new VCSRuntimeException(msg + " (file passed : [" + file + "])"); - } - - /** - * Obtain the set of permitted vcs actions. - * - * @return the EnumSet of authorized action according to current vcs config - */ - public EnumSet<VCSAction> getAuthorizedActions() { - boolean canWrite = hasWriteAccess(); - EnumSet<VCSAction> result = EnumSet.noneOf(VCSAction.class); - for (VCSAction vcsAction : VCSAction.values()) { - if (!vcsAction.isWrite() || canWrite) { - result.add(vcsAction); - } - } - return result; - } - - public boolean isConnected() { - return getConfig().isConnected(); - } - - public boolean hasWriteAccess() { - return !getConfig().isReadOnly(); - } -} Deleted: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSHelper.java =================================================================== --- trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSHelper.java 2008-04-04 19:37:59 UTC (rev 341) +++ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSHelper.java 2008-04-04 20:10:16 UTC (rev 342) @@ -1,191 +0,0 @@ -/* *##% -* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, -* Benjamin Poussin, Tony Chemit -* -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*##%*/ -package org.codelutin.vcs; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import static org.codelutin.i18n.I18n._; - -import java.io.File; - -@org.codelutin.i18n.I18nable -public class VCSHelper { - - private static final String LOCAL_SEP = File.separator; - private static final String LOCAL_SEP_PATTERN = "\\".equals(LOCAL_SEP) ? - LOCAL_SEP + LOCAL_SEP : LOCAL_SEP; - private static final String REMOTE_SEP = "/"; - private static final String REMOTE_SEP_PATTERN = "/"; - - - /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(VCSHelper.class); - - public static boolean isFileInWorkingCopy(File parent, String name, VCSHandler handler, boolean underVCS) { - if (parent == null || name == null) - throw new RuntimeException(VCSHelper.class.getName() + - "#isFileInWorkingCopy can not be invoked with some " + - "null parameter but was. [" + parent + "," + name + "," + - handler + "]"); - return isFileInWorkingCopy(new File(parent, name), handler, underVCS); - } - - public static boolean isFileInWorkingCopy(File file, VCSHandler handler, boolean underVCS) { - if (file == null || handler == null) - throw new RuntimeException(VCSHelper.class.getName() + - "#isFileInWorkingCopy can not be invoked with some " + - "null parameter but was. [" + file + "," + handler + "]"); - String rootPath = handler.getConfig().getLocalDatabasePath().getAbsolutePath(); - String localPath = file.getAbsolutePath(); - if (localPath.startsWith(rootPath) && !underVCS) { - return true; - } - final File realDir = file.isDirectory() ? file : file.getParentFile(); - return isFileInCheckedDir(realDir, handler); - } - - public static boolean isFileInCheckedDir(File file, VCSHandler handler) { - if (file == null || handler == null) - throw new RuntimeException(VCSHelper.class.getName() + - "#isFileInCheckedDir can not be invoked with some " + - "null parameter but was. [" + file + "," + handler + "]"); - final File realDir = file.isDirectory() ? file : file.getParentFile(); - return (new File(realDir, handler.getConfLocalDirname()).exists()); - - } - - public static void assertFileInWC(File file, VCSHandler handler) { - if (!isFileInWorkingCopy(file, handler, false)) - throw new VCSRuntimeException("the file [" + file + - "] is not in the working copy [" + - handler.getLocalDatabasePath() + "]"); - } - - public static String getRemoteRelativePath(File f, VCSHandler handler) { - if (!isFileInWorkingCopy(f, handler, false)) return null; - //System.out.println("file on vcs working copy : "+f); - String rootPath = handler.getConfig().getLocalDatabasePath().getAbsolutePath(); - String localPath = f.getAbsolutePath(); - if (!localPath.startsWith(rootPath)) return null; - localPath = localPath.substring(rootPath.length() + 1); - return convertToRemoteName(localPath); - } - - public static String getLocalRelativePath(String remoteRelativePath, VCSHandler handler) { - String localPath = convertToLocalName(remoteRelativePath); - final File file = new File(handler.getConfig().getLocalDatabasePath(), localPath); - if (!isFileInWorkingCopy(file, handler, false)) return null; - return localPath; - } - - public static String convertToRemoteName(String txt) { - return txt.replaceAll(LOCAL_SEP_PATTERN, REMOTE_SEP); - } - - public static String convertToLocalName(String txt) { - return txt.replaceAll(REMOTE_SEP_PATTERN, LOCAL_SEP); - } - - public static void doCheckoutDir(VCSHandler handler, File... dirs) { - for (File dir : dirs) { - try { - if (!dir.exists() || !isFileInWorkingCopy(dir, handler, true)) { - handler.checkoutOnlyTheDirectory(dir, null); - } - } catch (Exception eee) { - log.warn(_("lutinvcs.error.checkout.dir", dir), eee); - break; - } - } - } - - /** - * @param typeRepo the type of repo to used - * @param remotePath the unclean remote path to use - * @return the remote path from the old one according to given type repo - */ - public static String getRemotePath(VCSTypeRepo typeRepo, String remotePath) { - String result = cleanRemotePath(remotePath); - switch (typeRepo) { - case BRANCH: - case TAG: - result = remotePath + '/' + typeRepo.getPath(); - break; - case HEAD: - result = remotePath; - break; - } - return result; - } - - /** - * @param typeRepo the type of repo to use - * @param version the version to use - * @return the remoteDatabase according to type of repo and version - */ - public static String getRemoteDatabase(VCSTypeRepo typeRepo, String version) { - String result = null; - switch (typeRepo) { - case BRANCH: - case TAG: - result = version; - break; - case HEAD: - result = VCSTypeRepo.HEAD.getPath(); - break; - } - return result; - } - - /** - * to clean a remote path from typeRepo suffix. - * <p/> - * For example, having a url svn://XXX/trunk then remove /trunk. - * <p/> - * Or if having svn://XXX/branches/YYY then remove /branches/YYY - * Or if having svn://XXX/tags/YYY then remove /tags/YYY - * - * @param remotePath the remote path to clean - * @return the cleaned remote path - */ - public static String cleanRemotePath(String remotePath) { - int pos = remotePath.indexOf(VCSTypeRepo.BRANCH.getPath()); - if (pos > -1) { - // found a branch - remotePath = remotePath.substring(0, pos - 1); - } else { - pos = remotePath.indexOf(VCSTypeRepo.HEAD.getPath()); - if (pos > -1) { - // found a branch - remotePath = remotePath.substring(0, pos - 1); - } else { - pos = remotePath.indexOf(VCSTypeRepo.TAG.getPath()); - if (pos > -1) { - // found a tag - remotePath = remotePath.substring(0, pos - 1); - } else { - // remote path was clean - } - } - } - return remotePath; - } - -} \ No newline at end of file Copied: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSHandler.java (from rev 334, trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/AbstractVCSHandler.java) =================================================================== --- trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSHandler.java (rev 0) +++ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/AbstractVCSHandler.java 2008-04-04 20:10:16 UTC (rev 342) @@ -0,0 +1,213 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ +package org.codelutin.vcs.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.FileUtil; +import org.codelutin.util.ListenerSet; +import org.codelutin.vcs.VCSHandlerEvent.Type; +import org.codelutin.vcs.VCSHandler; +import org.codelutin.vcs.VCSConfig; +import org.codelutin.vcs.VCSHandlerEventListener; +import org.codelutin.vcs.VCSHandlerEvent; +import org.codelutin.vcs.VCSRuntimeException; +import org.codelutin.vcs.VCSAction; + +import java.io.File; +import java.io.FileFilter; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.EnumSet; + +/** + * abstract VCSHandler base with usefull methods that does not need vcs specific code. + * + * @author chemit + */ +public abstract class AbstractVCSHandler implements VCSHandler { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static protected final Log log = LogFactory.getLog(AbstractVCSHandler.class); + + /** + * FilenameFilter to detect all files in a vcs working copy all files and directories that must be handled by vcs + * + * @see org.codelutin.vcs.VCSHandler#getVersionnableFilenameFilter() + */ + protected final FilenameFilter versionnableFilenameFilter; + protected final FileFilter versionnableFileFilter; + protected final String confLocalDirName; + protected final String confLocalEntriesFilename; + protected final VCSConfig config; + + protected ListenerSet<VCSHandlerEventListener> listeners = new ListenerSet<VCSHandlerEventListener>(); + + protected AbstractVCSHandler(VCSConfig config, final String vCSConfLocalDirName, String vCSConfLocalEntriesFilename) { + this.config = config; + this.confLocalDirName = vCSConfLocalDirName; + this.confLocalEntriesFilename = vCSConfLocalEntriesFilename; + this.versionnableFilenameFilter = new FilenameFilter() { + public boolean accept(File dir, String name) { + return !vCSConfLocalDirName.equals(name) && !"CVS".equals(name); + } + }; + this.versionnableFileFilter = new FileFilter() { + public boolean accept(File dir) { + return dir.isFile() || (!vCSConfLocalDirName.equals(dir.getName()) && !"CVS".equals(dir.getName())); + } + }; + fireInit(); + } + + public VCSConfig getConfig() { + return config; + } + + public File getLocalDatabasePath() { + return config.getLocalDatabasePath(); + } + + public void deleteWorkingCopy() { + if (getLocalDatabasePath().exists()) { + FileUtil.deleteRecursively(getLocalDatabasePath()); + getLocalDatabasePath().delete(); + } + } + + public FilenameFilter getVersionnableFilenameFilter() { + return versionnableFilenameFilter; + } + + public FileFilter getVersionnableFileFilter() { + return versionnableFileFilter; + } + + public String getConfLocalDirname() { + return confLocalDirName; + } + + public String getConfLocalEntriesFilename() { + return confLocalEntriesFilename; + } + + public boolean isVersionnableFile(File file) { + assertFileExists(file, "file is empty or non existant"); + return !confLocalDirName.equalsIgnoreCase(file.getName()); + } + + public List<String> getLocalStorageNames(File directory) { + List<String> result = new ArrayList<String>(); + if (directory.exists()) { + getFiles0(directory, result, ""); + } + Collections.sort(result); + return result; + } + + public void addVCSHandlerEventListener(VCSHandlerEventListener l) { + listeners.add(l); + } + + public void removeVCSHandlerEventListener(VCSHandlerEventListener l) { + listeners.remove(l); + } + + public void open() { + fireOpen(); + } + + public void close() { + fireClose(); + } + + protected void fireInit() { + VCSHandlerEvent e = new VCSHandlerEvent(this, Type.INIT); + for (VCSHandlerEventListener l : listeners) { + l.init(e); + } + } + + protected void fireOpen() { + VCSHandlerEvent e = new VCSHandlerEvent(this, Type.OPEN); + for (VCSHandlerEventListener l : listeners) { + l.open(e); + } + } + + protected void fireClose() { + VCSHandlerEvent e = new VCSHandlerEvent(this, Type.CLOSE); + for (VCSHandlerEventListener l : listeners) { + l.close(e); + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + close(); + } + + protected void getFiles0(File directory, List<String> result, String prefix) { + //TODO put the hardcored value in a property!!! + if (directory.getName().equals("data")) return; + final String[] strings = directory.list(versionnableFilenameFilter); + boolean first = prefix.equals(""); + final String newPrefix = (first ? "" : prefix + File.separator); + + for (String filename : strings) { + final File dir = new File(directory, filename); + if (dir.isDirectory()) + getFiles0(dir, result, newPrefix + dir.getName()); + else result.add(newPrefix + dir.getName()); + } + } + + protected void assertFileExists(File file, String msg) { + if (file == null || !file.exists()) + throw new VCSRuntimeException(msg + " (file passed : [" + file + "])"); + } + + /** + * Obtain the set of permitted vcs actions. + * + * @return the EnumSet of authorized action according to current vcs config + */ + public EnumSet<VCSAction> getAuthorizedActions() { + boolean canWrite = hasWriteAccess(); + EnumSet<VCSAction> result = EnumSet.noneOf(VCSAction.class); + for (VCSAction vcsAction : VCSAction.values()) { + if (!vcsAction.isWrite() || canWrite) { + result.add(vcsAction); + } + } + return result; + } + + public boolean isConnected() { + return getConfig().isConnected(); + } + + public boolean hasWriteAccess() { + return !getConfig().isReadOnly(); + } +} Copied: trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/VCSHelper.java (from rev 334, trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/VCSHelper.java) =================================================================== --- trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/VCSHelper.java (rev 0) +++ trunk/lutinvcs/lutinvcs-api/src/main/java/org/codelutin/vcs/util/VCSHelper.java 2008-04-04 20:10:16 UTC (rev 342) @@ -0,0 +1,194 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ +package org.codelutin.vcs.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import static org.codelutin.i18n.I18n._; +import org.codelutin.vcs.VCSHandler; +import org.codelutin.vcs.VCSRuntimeException; +import org.codelutin.vcs.VCSTypeRepo; + +import java.io.File; + +@org.codelutin.i18n.I18nable +public class VCSHelper { + + private static final String LOCAL_SEP = File.separator; + private static final String LOCAL_SEP_PATTERN = "\\".equals(LOCAL_SEP) ? + LOCAL_SEP + LOCAL_SEP : LOCAL_SEP; + private static final String REMOTE_SEP = "/"; + private static final String REMOTE_SEP_PATTERN = "/"; + + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(VCSHelper.class); + + public static boolean isFileInWorkingCopy(File parent, String name, VCSHandler handler, boolean underVCS) { + if (parent == null || name == null) + throw new RuntimeException(VCSHelper.class.getName() + + "#isFileInWorkingCopy can not be invoked with some " + + "null parameter but was. [" + parent + "," + name + "," + + handler + "]"); + return isFileInWorkingCopy(new File(parent, name), handler, underVCS); + } + + public static boolean isFileInWorkingCopy(File file, VCSHandler handler, boolean underVCS) { + if (file == null || handler == null) + throw new RuntimeException(VCSHelper.class.getName() + + "#isFileInWorkingCopy can not be invoked with some " + + "null parameter but was. [" + file + "," + handler + "]"); + String rootPath = handler.getConfig().getLocalDatabasePath().getAbsolutePath(); + String localPath = file.getAbsolutePath(); + if (localPath.startsWith(rootPath) && !underVCS) { + return true; + } + final File realDir = file.isDirectory() ? file : file.getParentFile(); + return isFileInCheckedDir(realDir, handler); + } + + public static boolean isFileInCheckedDir(File file, VCSHandler handler) { + if (file == null || handler == null) + throw new RuntimeException(VCSHelper.class.getName() + + "#isFileInCheckedDir can not be invoked with some " + + "null parameter but was. [" + file + "," + handler + "]"); + final File realDir = file.isDirectory() ? file : file.getParentFile(); + return (new File(realDir, handler.getConfLocalDirname()).exists()); + + } + + public static void assertFileInWC(File file, VCSHandler handler) { + if (!isFileInWorkingCopy(file, handler, false)) + throw new VCSRuntimeException("the file [" + file + + "] is not in the working copy [" + + handler.getLocalDatabasePath() + "]"); + } + + public static String getRemoteRelativePath(File f, VCSHandler handler) { + if (!isFileInWorkingCopy(f, handler, false)) return null; + //System.out.println("file on vcs working copy : "+f); + String rootPath = handler.getConfig().getLocalDatabasePath().getAbsolutePath(); + String localPath = f.getAbsolutePath(); + if (!localPath.startsWith(rootPath)) return null; + localPath = localPath.substring(rootPath.length() + 1); + return convertToRemoteName(localPath); + } + + public static String getLocalRelativePath(String remoteRelativePath, VCSHandler handler) { + String localPath = convertToLocalName(remoteRelativePath); + final File file = new File(handler.getConfig().getLocalDatabasePath(), localPath); + if (!isFileInWorkingCopy(file, handler, false)) return null; + return localPath; + } + + public static String convertToRemoteName(String txt) { + return txt.replaceAll(LOCAL_SEP_PATTERN, REMOTE_SEP); + } + + public static String convertToLocalName(String txt) { + return txt.replaceAll(REMOTE_SEP_PATTERN, LOCAL_SEP); + } + + public static void doCheckoutDir(VCSHandler handler, File... dirs) { + for (File dir : dirs) { + try { + if (!dir.exists() || !isFileInWorkingCopy(dir, handler, true)) { + handler.checkoutOnlyTheDirectory(dir, null); + } + } catch (Exception eee) { + log.warn(_("lutinvcs.error.checkout.dir", dir), eee); + break; + } + } + } + + /** + * @param typeRepo the type of repo to used + * @param remotePath the unclean remote path to use + * @return the remote path from the old one according to given type repo + */ + public static String getRemotePath(VCSTypeRepo typeRepo, String remotePath) { + String result = cleanRemotePath(remotePath); + switch (typeRepo) { + case BRANCH: + case TAG: + result = remotePath + '/' + typeRepo.getPath(); + break; + case HEAD: + result = remotePath; + break; + } + return result; + } + + /** + * @param typeRepo the type of repo to use + * @param version the version to use + * @return the remoteDatabase according to type of repo and version + */ + public static String getRemoteDatabase(VCSTypeRepo typeRepo, String version) { + String result = null; + switch (typeRepo) { + case BRANCH: + case TAG: + result = version; + break; + case HEAD: + result = VCSTypeRepo.HEAD.getPath(); + break; + } + return result; + } + + /** + * to clean a remote path from typeRepo suffix. + * <p/> + * For example, having a url svn://XXX/trunk then remove /trunk. + * <p/> + * Or if having svn://XXX/branches/YYY then remove /branches/YYY + * Or if having svn://XXX/tags/YYY then remove /tags/YYY + * + * @param remotePath the remote path to clean + * @return the cleaned remote path + */ + public static String cleanRemotePath(String remotePath) { + int pos = remotePath.indexOf(VCSTypeRepo.BRANCH.getPath()); + if (pos > -1) { + // found a branch + remotePath = remotePath.substring(0, pos - 1); + } else { + pos = remotePath.indexOf(VCSTypeRepo.HEAD.getPath()); + if (pos > -1) { + // found a branch + remotePath = remotePath.substring(0, pos - 1); + } else { + pos = remotePath.indexOf(VCSTypeRepo.TAG.getPath()); + if (pos > -1) { + // found a tag + remotePath = remotePath.substring(0, pos - 1); + } else { + // remote path was clean + } + } + } + return remotePath; + } + +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org