View Javadoc
1   package org.apache.maven.scm.provider.svn.svnexe.command.status;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmFile;
23  import org.apache.maven.scm.ScmFileStatus;
24  import org.apache.maven.scm.util.AbstractConsumer;
25  import org.codehaus.plexus.util.StringUtils;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   *
34   */
35  public class SvnStatusConsumer
36          extends AbstractConsumer
37  {
38      private final File workingDirectory;
39  
40      private final List<ScmFile> changedFiles = new ArrayList<>();
41  
42      // ----------------------------------------------------------------------
43      //
44      // ----------------------------------------------------------------------
45  
46      public SvnStatusConsumer( File workingDirectory )
47      {
48          this.workingDirectory = workingDirectory;
49      }
50  
51      // ----------------------------------------------------------------------
52      // StreamConsumer Implementation
53      // ----------------------------------------------------------------------
54  
55      /** {@inheritDoc} */
56      public void consumeLine( String line )
57      {
58          if ( logger.isDebugEnabled() )
59          {
60              logger.debug( line );
61          }
62          if ( StringUtils.isEmpty( line.trim() ) )
63          {
64              return;
65          }
66  
67          if ( line.length() <= 7 )
68          {
69              if ( logger.isWarnEnabled() )
70              {
71                  logger.warn( "Unexpected input, the line must be at least seven characters long. Line: '"
72                               + line + "'." );
73              }
74  
75              return;
76          }
77  
78          String statusString = line.substring( 0, 1 );
79  
80          String file = line.substring( 7 ).trim();
81  
82          ScmFileStatus status;
83  
84          //  The first six columns in the output are each one character wide:
85          //    First column: Says if item was added, deleted, or otherwise changed
86          //      ' ' no modifications
87          //      'A' Added
88          //      'C' Conflicted
89          //      'D' Deleted
90          //      'I' Ignored
91          //      'M' Modified
92          //      'R' Replaced
93          //      'X' item is unversioned, but is used by an externals definition
94          //      '?' item is not under version control
95          //      '!' item is missing (removed by non-svn command) or incomplete
96          //      '~' versioned item obstructed by some item of a different kind
97          //    Second column: Modifications of a file's or directory's properties
98          //      ' ' no modifications
99          //      'C' Conflicted
100         //      'M' Modified
101         //    Third column: Whether the working copy directory is locked
102         //      ' ' not locked
103         //      'L' locked
104         //    Fourth column: Scheduled commit will contain addition-with-history
105         //      ' ' no history scheduled with commit
106         //      '+' history scheduled with commit
107         //    Fifth column: Whether the item is switched relative to its parent
108         //      ' ' normal
109         //      'S' switched
110         //    Sixth column: Repository lock token
111         //      (without -u)
112         //      ' ' no lock token
113         //      'K' lock token present
114         //      (with -u)
115         //      ' ' not locked in repository, no lock token
116         //      'K' locked in repository, lock toKen present
117         //      'O' locked in repository, lock token in some Other working copy
118         //      'T' locked in repository, lock token present but sTolen
119         //      'B' not locked in repository, lock token present but Broken
120         //
121         //  The out-of-date information appears in the eighth column (with -u):
122         //      '*' a newer revision exists on the server
123         //      ' ' the working copy is up to date
124         if ( statusString.equals( "A" ) )
125         {
126             status = ScmFileStatus.ADDED;
127         }
128         else if ( statusString.equals( "M" ) || statusString.equals( "R" ) || statusString.equals( "~" ) )
129         {
130             status = ScmFileStatus.MODIFIED;
131         }
132         else if ( statusString.equals( "D" ) )
133         {
134             status = ScmFileStatus.DELETED;
135         }
136         else if ( statusString.equals( "?" ) )
137         {
138             status = ScmFileStatus.UNKNOWN;
139         }
140         else if ( statusString.equals( "!" ) )
141         {
142             status = ScmFileStatus.MISSING;
143         }
144         else if ( statusString.equals( "C" ) )
145         {
146             status = ScmFileStatus.CONFLICT;
147         }
148         else if ( statusString.equals( "L" ) )
149         {
150             status = ScmFileStatus.LOCKED;
151         }
152         else if ( statusString.equals( "X" ) )
153         {
154             //skip svn:external entries
155             return;
156         }
157         else if ( statusString.equals( "I" ) )
158         {
159             //skip svn:external entries
160             return;
161         }
162         else
163         {
164             //Parse the second column
165             statusString = line.substring( 1, 1 );
166 
167             //The line isn't a status line, ie something like 'Performing status on external item at...'
168             //or a status defined in next columns
169             return;
170         }
171 
172         // If the file isn't a file; don't add it.
173         if ( !status.equals( ScmFileStatus.DELETED ) && !new File( workingDirectory, file ).isFile() )
174         {
175             return;
176         }
177 
178         changedFiles.add( new ScmFile( file, status ) );
179     }
180 
181     public List<ScmFile> getChangedFiles()
182     {
183         return changedFiles;
184     }
185 }