/*-------------------------------------------------------------------------- * Copyright 2008 utgenome.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *--------------------------------------------------------------------------*/ //-------------------------------------- // utgb-shell Project // // SVNAdd.java // Since: Jul 7, 2008 // // $URL$ // $Author$ //-------------------------------------- package org.utgenome.shell; import java.io.File; import org.tmatesoft.svn.core.SVNDepth; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNPropertyValue; import org.tmatesoft.svn.core.wc.SVNClientManager; import org.tmatesoft.svn.core.wc.SVNWCClient; import org.xerial.util.StringUtil; import org.xerial.util.cui.OptionParser; import org.xerial.util.cui.OptionParserException; import org.xerial.util.log.Logger; /** * Subcommand to add the current project to the subversion repository * * @author leo * */ public class SVNAdd extends UTGBShellSubCommandBase { private static Logger _logger = Logger.getLogger(SVNAdd.class); private enum Opt { REPO_PATH } private OptionParser optionParser = new OptionParser(); public SVNAdd() { // optionParser.addOptionWithArgument(Opt.REPO_PATH, "r", "repo_path", "PATH", "Subversion repository path to // add"); } private static String[] addTargetFiles = { "README", "pom.xml" }; private static String[] addTargetDirs = { "config", "eclipse", "src" }; private static String[] ignoreTargetFiles = { "target", "gwt-home", "www", ".project", ".classpath" }; SVNClientManager svnClientManager = null; SVNWCClient workingCopyClient = null; @Override public void execute(String[] args) throws UTGBShellException { try { optionParser.parse(args); if (!isInProjectRoot()) throw new UTGBShellException("must be in the project root"); svnClientManager = SVNClientManager.newInstance(); workingCopyClient = svnClientManager.getWCClient(); // svn add the current directory svnAddDir(globalOption.projectDir, false); // svn add for (String target : addTargetDirs) { // recursively add the folder contents to the version management svnAddDir(target, true); } for (String target : addTargetFiles) { svnAddFile(target); } // svn:ignore String ignoreTarget = StringUtil.join(ignoreTargetFiles, "\n"); svnIgnore(".", ignoreTarget, false); // db folder svnAddDir("db", false); svnIgnore("db", "*.db", false); // add tomcat folder and ignore work files svnAddDir("tomcat", false); svnAddDir("tomcat/conf", true); svnAddDir("tomcat/webapps", true); svnIgnore("tomcat", "work", false); } catch (OptionParserException e) { throw new UTGBShellException(e); } catch (SVNException e) { throw new UTGBShellException(e); } } public void svnAddDir(String dir, boolean isRecursive) throws SVNException { assert (workingCopyClient != null); _logger.info("svn add " + dir); File f = new File(dir); boolean createDir = !f.exists(); // workingCopyClient.doAdd(f, false, createDir, false, isRecursive); // File, force, mkdir, // climbUnversionedParents, recursive workingCopyClient.doAdd(f, true, createDir, false, SVNDepth.fromRecurse(isRecursive), false, true); } public void svnAddFile(String file) throws SVNException { assert (workingCopyClient != null); _logger.info("svn add " + file); File f = new File(file); // workingCopyClient.doAdd(new File(file), false, false, false, false); workingCopyClient.doAdd(new File(file), true, false, false, SVNDepth.fromRecurse(false), false, false); } public void svnIgnore(String file, String ignoreTargets, boolean isRecursive) throws SVNException { assert (workingCopyClient != null); _logger.info("set svn:ignore on directory =" + file + ", targets = " + ignoreTargets); // workingCopyClient.doSetProperty(new File(file), "svn:ignore", ignoreTargets, false, isRecursive, null); // // path, propName, propValue, force, recursive, handler workingCopyClient.doSetProperty(new File(file), "svn:ignore", SVNPropertyValue.create(ignoreTargets), false, SVNDepth.fromRecurse(isRecursive), null, null); } @Override public String name() { return "svn-add"; } public String getDetailedDescription() { return loadUsage("help-svnadd.txt"); } public String getOneLinerDescription() { return "add the current project to the SVN repostiory"; } public String getOptionList() { return optionParser.helpMessage(); } }