1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.starteam;
|
---|
18 |
|
---|
19 | import com.starbase.starteam.Folder;
|
---|
20 | import com.starbase.starteam.Item;
|
---|
21 | import com.starbase.starteam.Status;
|
---|
22 | import com.starbase.starteam.View;
|
---|
23 | import com.starbase.starteam.ViewConfiguration;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.util.Enumeration;
|
---|
26 | import org.apache.tools.ant.BuildException;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Checks files into a StarTeam project.
|
---|
30 | * Optionally adds files and in the local tree that
|
---|
31 | * are not managed by the repository to its control.
|
---|
32 | * Created: Sat Dec 15 20:26:07 2001
|
---|
33 | *
|
---|
34 | * @version 1.0
|
---|
35 | *
|
---|
36 | * @ant.task name="stcheckin" category="scm" product="Starteam"
|
---|
37 | */
|
---|
38 | public class StarTeamCheckin extends TreeBasedTask {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructor for StarTeamCheckin.
|
---|
42 | */
|
---|
43 | public StarTeamCheckin() {
|
---|
44 | // we want this to have a false default, unlike for Checkin.
|
---|
45 | setRecursive(false);
|
---|
46 | }
|
---|
47 |
|
---|
48 | private boolean createFolders = true;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The comment which will be stored with the checkin.
|
---|
52 | */
|
---|
53 | private String comment = null;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * holder for the add Uncontrolled attribute. If true, all
|
---|
57 | * local files not in StarTeam will be added to the repository.
|
---|
58 | */
|
---|
59 | private boolean addUncontrolled = false;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Sets the value of createFolders
|
---|
63 | *
|
---|
64 | * @param argCreateFolders Value to assign to this.createFolders
|
---|
65 | */
|
---|
66 | public void setCreateFolders(boolean argCreateFolders) {
|
---|
67 | this.createFolders = argCreateFolders;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Get the comment attribute for this operation
|
---|
73 | * @return value of comment.
|
---|
74 | */
|
---|
75 | public String getComment() {
|
---|
76 | return this.comment;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Optional checkin comment to be saved with the file.
|
---|
81 | * @param comment Value to assign to comment.
|
---|
82 | */
|
---|
83 | public void setComment(String comment) {
|
---|
84 | this.comment = comment;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Get the value of addUncontrolled.
|
---|
89 | * @return value of addUncontrolled.
|
---|
90 | */
|
---|
91 | public boolean isAddUncontrolled() {
|
---|
92 | return this.addUncontrolled;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * if true, any files or folders NOT in StarTeam will be
|
---|
97 | * added to the repository. Defaults to "false".
|
---|
98 | * @param addUncontrolled Value to assign to addUncontrolled.
|
---|
99 | */
|
---|
100 | public void setAddUncontrolled(boolean addUncontrolled) {
|
---|
101 | this.addUncontrolled = addUncontrolled;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * This attribute tells whether unlocked files on checkin (so that
|
---|
106 | * other users may access them) checkout or to leave the checkout status
|
---|
107 | * alone (default).
|
---|
108 | * @see #setUnlocked(boolean)
|
---|
109 | */
|
---|
110 | private int lockStatus = Item.LockType.UNCHANGED;
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Set to do an unlocked checkout; optional, default is false;
|
---|
114 | * If true, file will be unlocked so that other users may
|
---|
115 | * change it. If false, lock status will not change.
|
---|
116 | * @param v true means do an unlocked checkout
|
---|
117 | * false means leave status alone.
|
---|
118 | */
|
---|
119 | public void setUnlocked(boolean v) {
|
---|
120 | if (v) {
|
---|
121 | this.lockStatus = Item.LockType.UNLOCKED;
|
---|
122 | } else {
|
---|
123 | this.lockStatus = Item.LockType.UNCHANGED;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Override of base-class abstract function creates an
|
---|
129 | * appropriately configured view. For checkins this is
|
---|
130 | * always the current or "tip" view.
|
---|
131 | *
|
---|
132 | * @param raw the unconfigured <code>View</code>
|
---|
133 | * @return the snapshot <code>View</code> appropriately configured.
|
---|
134 | */
|
---|
135 | protected View createSnapshotView(View raw) {
|
---|
136 | return new View(raw, ViewConfiguration.createTip());
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Implements base-class abstract function to define tests for
|
---|
141 | * any preconditons required by the task.
|
---|
142 | *
|
---|
143 | * @exception BuildException thrown if both rootLocalFolder
|
---|
144 | * and viewRootLocalFolder are defined
|
---|
145 | */
|
---|
146 | protected void testPreconditions() throws BuildException {
|
---|
147 | }
|
---|
148 | /**
|
---|
149 | * Implements base-class abstract function to emit to the log an
|
---|
150 | * entry describing the parameters that will be used by this operation.
|
---|
151 | *
|
---|
152 | * @param starteamrootFolder
|
---|
153 | * root folder in StarTeam for the operation
|
---|
154 | * @param targetrootFolder
|
---|
155 | * root local folder for the operation
|
---|
156 | * (whether specified by the user or not).
|
---|
157 | */
|
---|
158 | protected void logOperationDescription(
|
---|
159 | Folder starteamrootFolder, java.io.File targetrootFolder) {
|
---|
160 | log((this.isRecursive() ? "Recursive" : "Non-recursive")
|
---|
161 | + " Checkin from"
|
---|
162 | + (null == getRootLocalFolder() ? " (default): " : ": ")
|
---|
163 | + targetrootFolder.getAbsolutePath());
|
---|
164 |
|
---|
165 | log("Checking in to: " + starteamrootFolder.getFolderHierarchy());
|
---|
166 | logIncludes();
|
---|
167 | logExcludes();
|
---|
168 |
|
---|
169 | if (this.lockStatus == Item.LockType.UNLOCKED) {
|
---|
170 | log(" Items will be checked in unlocked.");
|
---|
171 | } else {
|
---|
172 | log(" Items will be checked in with no change in lock status.");
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (this.isForced()) {
|
---|
176 | log(" Items will be checked in in accordance with repository "
|
---|
177 | + "status and regardless of lock status.");
|
---|
178 | } else {
|
---|
179 | log(" Items will be checked in regardless of repository status "
|
---|
180 | + "only if locked.");
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Implements base-class abstract function to perform the checkout
|
---|
188 | * operation on the files in each folder of the tree.
|
---|
189 | *
|
---|
190 | * @param starteamFolder the StarTeam folder to which files
|
---|
191 | * will be checked in
|
---|
192 | * @param targetFolder local folder from which files will be checked in
|
---|
193 | * @exception BuildException if any error occurs
|
---|
194 | */
|
---|
195 | protected void visit(Folder starteamFolder, java.io.File targetFolder)
|
---|
196 | throws BuildException {
|
---|
197 | try {
|
---|
198 | if (null != getRootLocalFolder()) {
|
---|
199 | starteamFolder.setAlternatePathFragment(
|
---|
200 | targetFolder.getAbsolutePath());
|
---|
201 | }
|
---|
202 |
|
---|
203 | Folder[] foldersList = starteamFolder.getSubFolders();
|
---|
204 | Item[] stFiles = starteamFolder.getItems(getTypeNames().FILE);
|
---|
205 |
|
---|
206 | // note, it's important to scan the items BEFORE we make the
|
---|
207 | // UnmatchedFileMap because that creates a bunch of NEW
|
---|
208 | // folders and files (unattached to repository) and we
|
---|
209 | // don't want to include those in our traversal.
|
---|
210 |
|
---|
211 | UnmatchedFileMap ufm =
|
---|
212 | new CheckinMap().init(
|
---|
213 | targetFolder.getAbsoluteFile(), starteamFolder);
|
---|
214 |
|
---|
215 |
|
---|
216 | for (int i = 0, size = foldersList.length; i < size; i++) {
|
---|
217 | Folder stFolder = foldersList[i];
|
---|
218 | java.io.File subfolder =
|
---|
219 | new java.io.File(targetFolder, stFolder.getName());
|
---|
220 |
|
---|
221 | ufm.removeControlledItem(subfolder);
|
---|
222 |
|
---|
223 | if (isRecursive()) {
|
---|
224 | visit(stFolder, subfolder);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | for (int i = 0, size = stFiles.length; i < size; i++) {
|
---|
230 | com.starbase.starteam.File stFile =
|
---|
231 | (com.starbase.starteam.File) stFiles[i];
|
---|
232 | processFile(stFile);
|
---|
233 |
|
---|
234 | ufm.removeControlledItem(
|
---|
235 | new java.io.File(targetFolder, stFile.getName()));
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (this.addUncontrolled) {
|
---|
239 | ufm.processUncontrolledItems();
|
---|
240 | }
|
---|
241 |
|
---|
242 | } catch (IOException e) {
|
---|
243 | throw new BuildException(e);
|
---|
244 | }
|
---|
245 |
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * provides a string showing from and to full paths for logging
|
---|
250 | *
|
---|
251 | * @param remotefile the Star Team file being processed.
|
---|
252 | *
|
---|
253 | * @return a string showing from and to full paths
|
---|
254 | */
|
---|
255 | private String describeCheckin(com.starbase.starteam.File remotefile) {
|
---|
256 | StringBuffer sb = new StringBuffer();
|
---|
257 | sb.append(remotefile.getFullName())
|
---|
258 | .append(" --> ")
|
---|
259 | .append(getFullRepositoryPath(remotefile));
|
---|
260 | return sb.toString();
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Processes (checks-out) <code>stFiles</code>files from StarTeam folder.
|
---|
265 | *
|
---|
266 | * @param eachFile repository file to process
|
---|
267 | * @param targetFolder a java.io.File (Folder) to work
|
---|
268 | * @throws IOException when StarTeam API fails to work with files
|
---|
269 | */
|
---|
270 | private void processFile(com.starbase.starteam.File eachFile)
|
---|
271 | throws IOException {
|
---|
272 | String filename = eachFile.getName();
|
---|
273 |
|
---|
274 | // If the file doesn't pass the include/exclude tests, skip it.
|
---|
275 | if (!shouldProcess(filename)) {
|
---|
276 | log("Excluding " + getFullRepositoryPath(eachFile));
|
---|
277 | return;
|
---|
278 | }
|
---|
279 |
|
---|
280 | boolean checkin = true;
|
---|
281 | int fileStatus = (eachFile.getStatus());
|
---|
282 |
|
---|
283 | // We try to update the status once to give StarTeam
|
---|
284 | // another chance.
|
---|
285 |
|
---|
286 | if (fileStatus == Status.MERGE || fileStatus == Status.UNKNOWN) {
|
---|
287 | eachFile.updateStatus(true, true);
|
---|
288 | fileStatus = (eachFile.getStatus());
|
---|
289 | }
|
---|
290 |
|
---|
291 | if (fileStatus == Status.MODIFIED) {
|
---|
292 | log("Checking in: " + describeCheckin(eachFile));
|
---|
293 | } else if (fileStatus == Status.MISSING) {
|
---|
294 | log("Local file missing: " + describeCheckin(eachFile));
|
---|
295 | checkin = false;
|
---|
296 | } else {
|
---|
297 | if (isForced()) {
|
---|
298 | log("Forced checkin of " + describeCheckin(eachFile)
|
---|
299 | + " over status " + Status.name(fileStatus));
|
---|
300 | } else {
|
---|
301 | log("Skipping: " + getFullRepositoryPath(eachFile)
|
---|
302 | + " - status: " + Status.name(fileStatus));
|
---|
303 | checkin = false;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | if (checkin) {
|
---|
307 | eachFile.checkin(this.comment, this.lockStatus,
|
---|
308 | this.isForced(), true, true);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * handles the deletion of uncontrolled items
|
---|
314 | */
|
---|
315 | private class CheckinMap extends UnmatchedFileMap {
|
---|
316 | protected boolean isActive() {
|
---|
317 | return StarTeamCheckin.this.addUncontrolled;
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * This override adds all its members to the repository. It is assumed
|
---|
323 | * that this method will not be called until all the items in the
|
---|
324 | * corresponding folder have been processed, and that the internal map
|
---|
325 | * will contain only uncontrolled items.
|
---|
326 | */
|
---|
327 | void processUncontrolledItems() throws BuildException {
|
---|
328 | if (this.isActive()) {
|
---|
329 | Enumeration e = this.keys();
|
---|
330 | while (e.hasMoreElements()) {
|
---|
331 | java.io.File local = (java.io.File) e.nextElement();
|
---|
332 | Item remoteItem = (Item) this.get(local);
|
---|
333 | remoteItem.update();
|
---|
334 |
|
---|
335 | // once we find a folder that isn't in the repository,
|
---|
336 | // we know we can add it.
|
---|
337 | if (local.isDirectory()) {
|
---|
338 | Folder folder = (Folder) remoteItem;
|
---|
339 | log("Added uncontrolled folder "
|
---|
340 | + folder.getFolderHierarchy()
|
---|
341 | + " from " + local.getAbsoluteFile());
|
---|
342 | if (isRecursive()) {
|
---|
343 | UnmatchedFileMap submap =
|
---|
344 | new CheckinMap().init(local, folder);
|
---|
345 | submap.processUncontrolledItems();
|
---|
346 | }
|
---|
347 | } else {
|
---|
348 | com.starbase.starteam.File remoteFile =
|
---|
349 | (com.starbase.starteam.File) remoteItem;
|
---|
350 | log("Added uncontrolled file "
|
---|
351 | + TreeBasedTask.getFullRepositoryPath(remoteFile)
|
---|
352 | + " from " + local.getAbsoluteFile());
|
---|
353 |
|
---|
354 | }
|
---|
355 | }
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|