001 /*
002 * CvsRepository.java
003 * Copyright (c) 1998-2008, The University of Sheffield.
004 *
005 * This code is from the GATE project (http://gate.ac.uk/) and is free
006 * software licenced under the GNU General Public License version 3. It is
007 * distributed without any warranty. For more details see COPYING.txt in the
008 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
009 *
010 * Hamish Cunningham 27/Aug/2005
011 */
012
013 package gate.versioning.cmdline;
014
015 import java.io.*;
016 import java.util.*;
017
018 import gate.util.*;
019
020 /**
021 * CVS repository implementation.
022 * The public API of this class is documented on the
023 * {@link Repository} interface.
024 * @see gate.versioning.cmdline.Repository
025 */
026 public class CvsRepository extends AbstractRepository {
027
028 /** Returns "cvs". */
029 public String getCommandName() { return "cvs"; }
030
031 /** Get the pre-subcommand elements of the command (e.g. -d root). */
032 protected List getPreCommand() {
033 List preCommand = new ArrayList();
034 preCommand.add("-d");
035 preCommand.add(root);
036 return preCommand;
037 } // getPreCommand()
038
039 /** Get the post-subcommand elements of the command (e.g. root). */
040 protected List getPostCommand(String fileName, boolean noRoot) {
041 List postCommand = new ArrayList();
042 if(fileName != null) postCommand.add(fileName);
043 return postCommand;
044 } // getPostCommand()
045
046 /**
047 * Create a new repository filetree (i.e. not a new object but a new
048 * database/filesystem on disk).
049 * This method can be called before {@link #init} (so that we
050 * can create repositories without having an existing one).
051 * @param dirName the directory to work on, which will be created
052 * (should be an absolute path).
053 * @return boolean representing success or failure.
054 */
055 public boolean create(String dirName) throws GateException {
056 String[] command = new String[4];
057 command[0] = getCommandName();
058 command[1] = "-d";
059 command[2] = dirName;
060 command[3] = "init";
061
062 // create the new directory
063 File newRepDir = null;
064 newRepDir = new File(dirName);
065 boolean mkdirResult = newRepDir.mkdirs();
066 if(!mkdirResult)
067 throw new GateException(
068 "couldn't create dir for new repository: " + nl + newRepDir.getPath()
069 );
070
071 // adjust working directory for duration of command
072 File oldWorkingDir = workingDir;
073 workingDir = newRepDir;
074 boolean result = runCommand(command);
075 workingDir = oldWorkingDir;
076 return result;
077 } // create()
078
079 /**
080 * Import a directory.
081 * @param dirName the directory to import (this must exist).
082 * @return boolean representing success or failure.
083 * @throws GateException when the directory doesn't exist.
084 */
085 public boolean importDir(String dirName) throws GateException {
086 File dir = new File(dirName);
087 if(! dir.exists() || ! dir.isDirectory())
088 throw new GateException(dirName + " is not an existing directory");
089
090 List com = new ArrayList();
091 com.add(getCommandName());
092 com.addAll(getPreCommand()); // rootFlag + root where appropriate
093 com.add("import");
094 com.add("-m");
095 com.add("imported");
096 com.add(dir.getName());
097 com.add("import-tag1");
098 com.add("import-tag2");
099 String[] comArray = (String[]) com.toArray(new String[com.size()]);
100
101 // adjust working directory for duration of command
102 File oldWorkingDir = workingDir;
103 workingDir = dir;
104 boolean result = runCommand(comArray);
105 workingDir = oldWorkingDir;
106
107 return result;
108 } // importDir()
109
110 /**
111 * Use the repository's status command to figure out if the file has been
112 * locally modified.
113 * @param fileName the file or directory to work on (should be relative
114 * to the repository's working directory, and use "/" as a path separator).
115 * @return boolean giving status indication.
116 */
117 public boolean isModified(String fileName) {
118 String statusString = status(fileName);
119 if(statusString.indexOf("Status: Locally Modified") >= 0)
120 //if(statusString.contains("Status: Locally Modified"))
121 return true;
122 else
123 return false;
124 } // isModified()
125
126 /**
127 * Use the repository's status command to figure out if the file is
128 * out-of-date.
129 * @param fileName the file or directory to work on (should be relative
130 * to the repository's working directory, and use "/" as a path separator).
131 * @return boolean giving status indication.
132 */
133 public boolean isOutOfDate(String fileName) {
134 String statusString = status(fileName);
135 if( statusString.indexOf("Status: Needs Patch") >= 0 || statusString.indexOf("Status: Needs Merge") >= 0)
136 //if( statusString.contains("Status: Needs Patch") || statusString.contains("Status: Needs Merge"))
137 return true;
138 else
139 return false;
140 } // isOutOfDate()
141
142 /**
143 * Use the repository's status command to figure out if the file is
144 * unknown.
145 * @param fileName the file or directory to work on (should be relative
146 * to the repository's working directory, and use "/" as a path separator).
147 * @return boolean giving status indication.
148 */
149 public boolean isUnknown(String fileName) {
150 String statusString = status(fileName);
151 if(statusString.indexOf("Status: Unknown") >= 0)
152 //if(statusString.contains("Status: Unknown"))
153 return true;
154 else
155 return false;
156 } // isUnknown()
157
158 /**
159 * Check for the existence of a module (i.e. top-level directory).
160 * @param moduleName directory to look for.
161 * @return boolean giving status indication.
162 */
163 public boolean exists(String moduleName) {
164 List com = new ArrayList();
165 com.add(getCommandName());
166 com.addAll(getPreCommand());
167 com.add("co");
168 com.add("-d");
169 File tempDir;
170 String tempDirPath;
171 try {
172 tempDir = File.createTempFile("cvs-rep-exists-", null);
173 tempDirPath = tempDir.getAbsolutePath();
174 tempDir.delete();
175 tempDir = new File(tempDirPath);
176 } catch(IOException e) {
177 stderrOutput = "couldn't create temp file";
178 return false;
179 }
180 com.add(tempDir.getAbsolutePath());
181 com.add("-l");
182 com.add(moduleName);
183 String[] comArray = (String[]) com.toArray(new String[com.size()]);
184 boolean result = runCommand(comArray);
185 boolean status = tempDir.exists() && tempDir.isDirectory();
186
187 Files.rmdir(tempDir);
188 return status;
189 } // exists()
190
191 } // CvsRepository
|