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.status;
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.util.AbstractConsumer;
29  
30  /**
31   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
32   *
33   */
34  public class SvnStatusConsumer extends AbstractConsumer {
35      private final File workingDirectory;
36  
37      private final List<ScmFile> changedFiles = new ArrayList<>();
38  
39      // ----------------------------------------------------------------------
40      //
41      // ----------------------------------------------------------------------
42  
43      public SvnStatusConsumer(File workingDirectory) {
44          this.workingDirectory = workingDirectory;
45      }
46  
47      // ----------------------------------------------------------------------
48      // StreamConsumer Implementation
49      // ----------------------------------------------------------------------
50  
51      /** {@inheritDoc} */
52      public void consumeLine(String line) {
53          if (logger.isDebugEnabled()) {
54              logger.debug(line);
55          }
56          if (StringUtils.isEmpty(line.trim())) {
57              return;
58          }
59  
60          if (line.length() <= 7) {
61              if (logger.isWarnEnabled()) {
62                  logger.warn("Unexpected input, the line must be at least seven characters long. Line: '" + line + "'.");
63              }
64  
65              return;
66          }
67  
68          String statusString = line.substring(0, 1);
69  
70          String file = line.substring(7).trim();
71  
72          ScmFileStatus status;
73  
74          //  The first six columns in the output are each one character wide:
75          //    First column: Says if item was added, deleted, or otherwise changed
76          //      ' ' no modifications
77          //      'A' Added
78          //      'C' Conflicted
79          //      'D' Deleted
80          //      'I' Ignored
81          //      'M' Modified
82          //      'R' Replaced
83          //      'X' item is unversioned, but is used by an externals definition
84          //      '?' item is not under version control
85          //      '!' item is missing (removed by non-svn command) or incomplete
86          //      '~' versioned item obstructed by some item of a different kind
87          //    Second column: Modifications of a file's or directory's properties
88          //      ' ' no modifications
89          //      'C' Conflicted
90          //      'M' Modified
91          //    Third column: Whether the working copy directory is locked
92          //      ' ' not locked
93          //      'L' locked
94          //    Fourth column: Scheduled commit will contain addition-with-history
95          //      ' ' no history scheduled with commit
96          //      '+' history scheduled with commit
97          //    Fifth column: Whether the item is switched relative to its parent
98          //      ' ' normal
99          //      'S' switched
100         //    Sixth column: Repository lock token
101         //      (without -u)
102         //      ' ' no lock token
103         //      'K' lock token present
104         //      (with -u)
105         //      ' ' not locked in repository, no lock token
106         //      'K' locked in repository, lock toKen present
107         //      'O' locked in repository, lock token in some Other working copy
108         //      'T' locked in repository, lock token present but sTolen
109         //      'B' not locked in repository, lock token present but Broken
110         //
111         //  The out-of-date information appears in the eighth column (with -u):
112         //      '*' a newer revision exists on the server
113         //      ' ' the working copy is up to date
114         if (statusString.equals("A")) {
115             status = ScmFileStatus.ADDED;
116         } else if (statusString.equals("M") || statusString.equals("R") || statusString.equals("~")) {
117             status = ScmFileStatus.MODIFIED;
118         } else if (statusString.equals("D")) {
119             status = ScmFileStatus.DELETED;
120         } else if (statusString.equals("?")) {
121             status = ScmFileStatus.UNKNOWN;
122         } else if (statusString.equals("!")) {
123             status = ScmFileStatus.MISSING;
124         } else if (statusString.equals("C")) {
125             status = ScmFileStatus.CONFLICT;
126         } else if (statusString.equals("L")) {
127             status = ScmFileStatus.LOCKED;
128         } else if (statusString.equals("X")) {
129             // skip svn:external entries
130             return;
131         } else if (statusString.equals("I")) {
132             // skip svn:external entries
133             return;
134         } else {
135             // Parse the second column
136             statusString = line.substring(1, 1);
137 
138             // The line isn't a status line, ie something like 'Performing status on external item at...'
139             // or a status defined in next columns
140             return;
141         }
142 
143         // If the file isn't a file; don't add it.
144         if (!status.equals(ScmFileStatus.DELETED) && !new File(workingDirectory, file).isFile()) {
145             return;
146         }
147 
148         changedFiles.add(new ScmFile(file, status));
149     }
150 
151     public List<ScmFile> getChangedFiles() {
152         return changedFiles;
153     }
154 }