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  public class SvnCheckOutConsumer extends AbstractFileCheckingConsumer {
35      private static final String CHECKED_OUT_REVISION_TOKEN = "Checked out revision";
36  
37      private final List<ScmFile> files = new ArrayList<>();
38  
39      public SvnCheckOutConsumer(File workingDirectory) {
40          super(workingDirectory);
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      protected void parseLine(String line) {
47          String statusString = line.substring(0, 1);
48  
49          String file = line.substring(3).trim();
50          // [SCM-368]
51          if (file.startsWith(getWorkingDirectory().getAbsolutePath())) {
52              file = StringUtils.substring(
53                      file, getWorkingDirectory().getAbsolutePath().length() + 1);
54          }
55  
56          ScmFileStatus status;
57  
58          if (line.startsWith(CHECKED_OUT_REVISION_TOKEN)) {
59              String revisionString = line.substring(CHECKED_OUT_REVISION_TOKEN.length() + 1, line.length() - 1);
60  
61              revision = parseInt(revisionString);
62  
63              return;
64          } else if (statusString.equals("A")) {
65              status = ScmFileStatus.ADDED;
66          } else if (statusString.equals("U")) {
67              status = ScmFileStatus.UPDATED;
68          } else {
69              // Do nothing
70  
71              return;
72          }
73  
74          addFile(new ScmFile(file, status));
75      }
76  
77      // ----------------------------------------------------------------------
78      //
79      // ----------------------------------------------------------------------
80  
81      public List<ScmFile> getCheckedOutFiles() {
82          return getFiles();
83      }
84  
85      protected void addFile(ScmFile file) {
86          files.add(file);
87      }
88  
89      protected List<ScmFile> getFiles() {
90          List<ScmFile> onlyFiles = new ArrayList<>();
91          for (ScmFile file : files) {
92              // second part is for svn 1.7 as the co output is now relative not a full path as for svn 1.7-
93              if (!(!file.getStatus().equals(ScmFileStatus.DELETED)
94                              && !new File(getWorkingDirectory(), file.getPath()).isFile())
95                      || !(!file.getStatus().equals(ScmFileStatus.DELETED)
96                              && !new File(getWorkingDirectory().getParent(), file.getPath()).isFile())) {
97                  onlyFiles.add(file);
98              }
99          }
100 
101         return onlyFiles;
102     }
103 }