001package org.apache.maven.scm.provider.svn.svnexe.command.status;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmFile;
023import org.apache.maven.scm.ScmFileStatus;
024import org.apache.maven.scm.log.ScmLogger;
025import org.codehaus.plexus.util.StringUtils;
026import org.codehaus.plexus.util.cli.StreamConsumer;
027
028import java.io.File;
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
034 *
035 */
036public class SvnStatusConsumer
037    implements StreamConsumer
038{
039    private ScmLogger logger;
040
041    private File workingDirectory;
042
043    private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
044
045    // ----------------------------------------------------------------------
046    //
047    // ----------------------------------------------------------------------
048
049    public SvnStatusConsumer( ScmLogger logger, File workingDirectory )
050    {
051        this.logger = logger;
052
053        this.workingDirectory = workingDirectory;
054    }
055
056    // ----------------------------------------------------------------------
057    // StreamConsumer Implementation
058    // ----------------------------------------------------------------------
059
060    /** {@inheritDoc} */
061    public void consumeLine( String line )
062    {
063        if ( logger.isDebugEnabled() )
064        {
065            logger.debug( line );
066        }
067        if ( StringUtils.isEmpty( line.trim() ) )
068        {
069            return;
070        }
071
072        if ( line.length() <= 7 )
073        {
074            if ( logger.isWarnEnabled() )
075            {
076                logger.warn( "Unexpected input, the line must be at least seven characters long. Line: '" + line + "'." );
077            }
078
079            return;
080        }
081
082        String statusString = line.substring( 0, 1 );
083
084        String file = line.substring( 7 ).trim();
085
086        ScmFileStatus status;
087
088        //  The first six columns in the output are each one character wide:
089        //    First column: Says if item was added, deleted, or otherwise changed
090        //      ' ' no modifications
091        //      'A' Added
092        //      'C' Conflicted
093        //      'D' Deleted
094        //      'I' Ignored
095        //      'M' Modified
096        //      'R' Replaced
097        //      'X' item is unversioned, but is used by an externals definition
098        //      '?' item is not under version control
099        //      '!' item is missing (removed by non-svn command) or incomplete
100        //      '~' versioned item obstructed by some item of a different kind
101        //    Second column: Modifications of a file's or directory's properties
102        //      ' ' no modifications
103        //      'C' Conflicted
104        //      'M' Modified
105        //    Third column: Whether the working copy directory is locked
106        //      ' ' not locked
107        //      'L' locked
108        //    Fourth column: Scheduled commit will contain addition-with-history
109        //      ' ' no history scheduled with commit
110        //      '+' history scheduled with commit
111        //    Fifth column: Whether the item is switched relative to its parent
112        //      ' ' normal
113        //      'S' switched
114        //    Sixth column: Repository lock token
115        //      (without -u)
116        //      ' ' no lock token
117        //      'K' lock token present
118        //      (with -u)
119        //      ' ' not locked in repository, no lock token
120        //      'K' locked in repository, lock toKen present
121        //      'O' locked in repository, lock token in some Other working copy
122        //      'T' locked in repository, lock token present but sTolen
123        //      'B' not locked in repository, lock token present but Broken
124        //
125        //  The out-of-date information appears in the eighth column (with -u):
126        //      '*' a newer revision exists on the server
127        //      ' ' the working copy is up to date
128        if ( statusString.equals( "A" ) )
129        {
130            status = ScmFileStatus.ADDED;
131        }
132        else if ( statusString.equals( "M" ) || statusString.equals( "R" ) || statusString.equals( "~" ) )
133        {
134            status = ScmFileStatus.MODIFIED;
135        }
136        else if ( statusString.equals( "D" ) )
137        {
138            status = ScmFileStatus.DELETED;
139        }
140        else if ( statusString.equals( "?" ) )
141        {
142            status = ScmFileStatus.UNKNOWN;
143        }
144        else if ( statusString.equals( "!" ) )
145        {
146            status = ScmFileStatus.MISSING;
147        }
148        else if ( statusString.equals( "C" ) )
149        {
150            status = ScmFileStatus.CONFLICT;
151        }
152        else if ( statusString.equals( "L" ) )
153        {
154            status = ScmFileStatus.LOCKED;
155        }
156        else if ( statusString.equals( "X" ) )
157        {
158            //skip svn:external entries
159            return;
160        }
161        else if ( statusString.equals( "I" ) )
162        {
163            //skip svn:external entries
164            return;
165        }
166        else
167        {
168            //Parse the second column
169            statusString = line.substring( 1, 1 );
170
171            if ( statusString.equals( "M" ) )
172            {
173                status = ScmFileStatus.MODIFIED;
174            }
175            else if ( statusString.equals( "C" ) )
176            {
177                status = ScmFileStatus.CONFLICT;
178            }
179            else
180            {
181                //The line isn't a status line, ie something like 'Performing status on external item at...'
182                //or a status defined in next columns
183                return;
184            }
185        }
186
187        // If the file isn't a file; don't add it.
188        if ( !status.equals( ScmFileStatus.DELETED ) && !new File( workingDirectory, file ).isFile() )
189        {
190            return;
191        }
192
193        changedFiles.add( new ScmFile( file, status ) );
194    }
195
196    public List<ScmFile> getChangedFiles()
197    {
198        return changedFiles;
199    }
200}