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.commons.lang3.StringUtils;
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileStatus;
028import org.apache.maven.scm.util.AbstractConsumer;
029
030/**
031 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
032 *
033 */
034public class SvnStatusConsumer extends AbstractConsumer {
035    private final File workingDirectory;
036
037    private final List<ScmFile> changedFiles = new ArrayList<>();
038
039    // ----------------------------------------------------------------------
040    //
041    // ----------------------------------------------------------------------
042
043    public SvnStatusConsumer(File workingDirectory) {
044        this.workingDirectory = workingDirectory;
045    }
046
047    // ----------------------------------------------------------------------
048    // StreamConsumer Implementation
049    // ----------------------------------------------------------------------
050
051    /** {@inheritDoc} */
052    public void consumeLine(String line) {
053        if (logger.isDebugEnabled()) {
054            logger.debug(line);
055        }
056        if (StringUtils.isEmpty(line.trim())) {
057            return;
058        }
059
060        if (line.length() <= 7) {
061            if (logger.isWarnEnabled()) {
062                logger.warn("Unexpected input, the line must be at least seven characters long. Line: '" + line + "'.");
063            }
064
065            return;
066        }
067
068        String statusString = line.substring(0, 1);
069
070        String file = line.substring(7).trim();
071
072        ScmFileStatus status;
073
074        //  The first six columns in the output are each one character wide:
075        //    First column: Says if item was added, deleted, or otherwise changed
076        //      ' ' no modifications
077        //      'A' Added
078        //      'C' Conflicted
079        //      'D' Deleted
080        //      'I' Ignored
081        //      'M' Modified
082        //      'R' Replaced
083        //      'X' item is unversioned, but is used by an externals definition
084        //      '?' item is not under version control
085        //      '!' item is missing (removed by non-svn command) or incomplete
086        //      '~' versioned item obstructed by some item of a different kind
087        //    Second column: Modifications of a file's or directory's properties
088        //      ' ' no modifications
089        //      'C' Conflicted
090        //      'M' Modified
091        //    Third column: Whether the working copy directory is locked
092        //      ' ' not locked
093        //      'L' locked
094        //    Fourth column: Scheduled commit will contain addition-with-history
095        //      ' ' no history scheduled with commit
096        //      '+' history scheduled with commit
097        //    Fifth column: Whether the item is switched relative to its parent
098        //      ' ' normal
099        //      '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}