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