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