View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command.checkout;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileStatus;
28  import org.apache.maven.scm.provider.svn.svnexe.command.AbstractFileCheckingConsumer;
29  
30  /**
31   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
32   * @author Olivier Lamy
33   *
34   */
35  public class SvnCheckOutConsumer extends AbstractFileCheckingConsumer {
36      private static final String CHECKED_OUT_REVISION_TOKEN = "Checked out revision";
37  
38      private final List<ScmFile> files = new ArrayList<>();
39  
40      public SvnCheckOutConsumer(File workingDirectory) {
41          super(workingDirectory);
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      protected void parseLine(String line) {
48          String statusString = line.substring(0, 1);
49  
50          String file = line.substring(3).trim();
51          // [SCM-368]
52          if (file.startsWith(getWorkingDirectory().getAbsolutePath())) {
53              file = StringUtils.substring(
54                      file, getWorkingDirectory().getAbsolutePath().length() + 1);
55          }
56  
57          ScmFileStatus status;
58  
59          if (line.startsWith(CHECKED_OUT_REVISION_TOKEN)) {
60              String revisionString = line.substring(CHECKED_OUT_REVISION_TOKEN.length() + 1, line.length() - 1);
61  
62              revision = parseInt(revisionString);
63  
64              return;
65          } else if (statusString.equals("A")) {
66              status = ScmFileStatus.ADDED;
67          } else if (statusString.equals("U")) {
68              status = ScmFileStatus.UPDATED;
69          } else {
70              // Do nothing
71  
72              return;
73          }
74  
75          addFile(new ScmFile(file, status));
76      }
77  
78      // ----------------------------------------------------------------------
79      //
80      // ----------------------------------------------------------------------
81  
82      public List<ScmFile> getCheckedOutFiles() {
83          return getFiles();
84      }
85  
86      protected void addFile(ScmFile file) {
87          files.add(file);
88      }
89  
90      protected List<ScmFile> getFiles() {
91          List<ScmFile> onlyFiles = new ArrayList<>();
92          for (ScmFile file : files) {
93              // second part is for svn 1.7 as the co output is now relative not a full path as for svn 1.7-
94              if (!(!file.getStatus().equals(ScmFileStatus.DELETED)
95                              && !new File(getWorkingDirectory(), file.getPath()).isFile())
96                      || !(!file.getStatus().equals(ScmFileStatus.DELETED)
97                              && !new File(getWorkingDirectory().getParent(), file.getPath()).isFile())) {
98                  onlyFiles.add(file);
99              }
100         }
101 
102         return onlyFiles;
103     }
104 }