source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/perforce/P4Fstat.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 6.6 KB
Line 
1/*
2 * Copyright 2003-2005 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/*
18 * Portions of this software are based upon public domain software
19 * originally written at the National Center for Supercomputing Applications,
20 * University of Illinois, Urbana-Champaign.
21 */
22
23package org.apache.tools.ant.taskdefs.optional.perforce;
24
25
26import java.io.File;
27import java.util.Vector;
28import java.util.ArrayList;
29
30import org.apache.tools.ant.Project;
31import org.apache.tools.ant.BuildException;
32import org.apache.tools.ant.DirectoryScanner;
33import org.apache.tools.ant.types.FileSet;
34
35/**
36 * P4Fstat--find out which files are under Perforce control and which are not.
37 *
38 * <br><b>Example Usage:</b><br>
39 * <pre>
40 * &lt;project name=&quot;p4fstat&quot; default=&quot;p4fstat&quot;
41 * basedir=&quot;C:\dev\gnu&quot;&gt;
42 * &lt;target name=&quot;p4fstat&quot; &gt;
43 * &lt;p4fstat showfilter=&quot;all&quot;&gt;
44 * &lt;fileset dir=&quot;depot&quot; includes=&quot;**\/*&quot;/&gt;
45 * &lt;/p4fstat&gt;
46 * &lt;/target&gt;
47 * &lt;/project&gt;
48 * </pre>
49 *
50 * @ant.task category="scm"
51 */
52public class P4Fstat extends P4Base {
53
54 private int changelist;
55 private String addCmd = "";
56 private Vector filesets = new Vector();
57 private static final int DEFAULT_CMD_LENGTH = 300;
58 private int cmdLength = DEFAULT_CMD_LENGTH;
59 private static final int SHOW_ALL = 0;
60 private static final int SHOW_EXISTING = 1;
61 private static final int SHOW_NON_EXISTING = 2;
62 private int show = SHOW_NON_EXISTING;
63 private FStatP4OutputHandler handler;
64 private StringBuffer filelist;
65 private int fileNum = 0;
66 private int doneFileNum = 0;
67 private boolean debug = false;
68
69 private static final String EXISTING_HEADER
70 = "Following files exist in perforce";
71 private static final String NONEXISTING_HEADER
72 = "Following files do not exist in perforce";
73
74 /**
75 * Sets the filter that one wants applied.
76 * <table>
77 * <tr><th>Option</th><th>Meaning</th></tr>
78 * <tr><td>all</td><td>all files under Perforce control or not</td></tr>
79 * <tr><td>existing</td><td>only files under Perforce control</td></tr>
80 * <tr><td>non-existing</td><td>only files not under Perforce control or not</td></tr>
81 * </table>
82 * @param filter should be one of all|existing|non-existing.
83 */
84 public void setShowFilter(String filter) {
85 if (filter.equalsIgnoreCase("all")) {
86 show = SHOW_ALL;
87 } else if (filter.equalsIgnoreCase("existing")) {
88 show = SHOW_EXISTING;
89 } else if (filter.equalsIgnoreCase("non-existing")) {
90 show = SHOW_NON_EXISTING;
91 } else {
92 throw new BuildException("P4Fstat: ShowFilter should be one of: "
93 + "all, existing, non-existing");
94 }
95 }
96
97 /**
98 * Sets optionally a change list number.
99 * @param changelist change list that one wants information about.
100 * @throws BuildException if the change list number is negative.
101 */
102 public void setChangelist(int changelist) throws BuildException {
103 if (changelist <= 0) {
104 throw new BuildException("P4FStat: Changelist# should be a "
105 + "positive number");
106 }
107 this.changelist = changelist;
108 }
109
110 /**
111 * Adds a fileset to be examined by p4fstat.
112 * @param set the fileset to add.
113 */
114 public void addFileset(FileSet set) {
115 filesets.addElement(set);
116 }
117
118 /**
119 * Executes the p4fstat task.
120 * @throws BuildException if no files are specified.
121 */
122 public void execute() throws BuildException {
123 handler = new FStatP4OutputHandler(this);
124 if (P4View != null) {
125 addCmd = P4View;
126 }
127 P4CmdOpts = (changelist > 0) ? ("-c " + changelist) : "";
128
129 filelist = new StringBuffer();
130
131 for (int i = 0; i < filesets.size(); i++) {
132 FileSet fs = (FileSet) filesets.elementAt(i);
133 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
134
135 String[] srcFiles = ds.getIncludedFiles();
136 fileNum = srcFiles.length;
137
138 if (srcFiles != null) {
139 for (int j = 0; j < srcFiles.length; j++) {
140 File f = new File(ds.getBasedir(), srcFiles[j]);
141 filelist.append(" ").append('"').append(f.getAbsolutePath()).append('"');
142 doneFileNum++;
143 if (filelist.length() > cmdLength) {
144
145 execP4Fstat(filelist);
146 filelist = new StringBuffer();
147 }
148 }
149 if (filelist.length() > 0) {
150 execP4Fstat(filelist);
151 }
152 } else {
153 log("No files specified to query status on!", Project.MSG_WARN);
154 }
155 }
156 if (show == SHOW_ALL || show == SHOW_EXISTING) {
157 printRes(handler.getExisting(), EXISTING_HEADER);
158 }
159 if (show == SHOW_ALL || show == SHOW_NON_EXISTING) {
160 printRes(handler.getNonExisting(), NONEXISTING_HEADER);
161 }
162 }
163
164 /**
165 * Return the number of files seen.
166 * @return the number of files seen.
167 */
168 public int getLengthOfTask() {
169 return fileNum;
170 }
171
172 /**
173 * Return the number of passes to make.
174 * IS THIS BEING USED?
175 * @return number of passes (how many filesets).
176 */
177 int getPasses() {
178 return filesets.size();
179 }
180
181 private void printRes(ArrayList ar, String header) {
182 log(header, Project.MSG_INFO);
183 for (int i = 0; i < ar.size(); i++) {
184 log((String) ar.get(i), Project.MSG_INFO);
185 }
186 }
187
188 private void execP4Fstat(StringBuffer list) {
189 String l = list.substring(0);
190 if (debug) {
191 log("Executing fstat " + P4CmdOpts + " " + addCmd + l + "\n",
192 Project.MSG_INFO);
193 }
194 execP4Command("fstat " + P4CmdOpts + " " + addCmd + l, handler);
195 }
196
197}
Note: See TracBrowser for help on using the repository browser.