001package org.apache.maven.scm.provider.git.gitexe.command.remove;
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.apache.regexp.RE;
026import org.apache.regexp.RESyntaxException;
027import org.codehaus.plexus.util.cli.StreamConsumer;
028
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
034 * @author Olivier Lamy
035 *
036 */
037public class GitRemoveConsumer
038    implements StreamConsumer
039{
040    /**
041     * The pattern used to match deleted file lines
042     */
043    private static final String REMOVED_PATTERN = "^rm\\s'(.*)'";
044
045    private ScmLogger logger;
046
047    private List<ScmFile> removedFiles = new ArrayList<ScmFile>();
048
049    /**
050     * @see #REMOVED_PATTERN
051     */
052    private RE removedRegexp;
053
054    // ----------------------------------------------------------------------
055    //
056    // ----------------------------------------------------------------------
057
058    public GitRemoveConsumer( ScmLogger logger )
059    {
060        this.logger = logger;
061        try
062        {
063            removedRegexp = new RE( REMOVED_PATTERN );
064        }
065        catch ( RESyntaxException ex )
066        {
067            throw new RuntimeException(
068                "INTERNAL ERROR: Could not create regexp to parse git log file. This shouldn't happen. Something is probably wrong with the oro installation.",
069                ex );
070        }
071    }
072
073    // ----------------------------------------------------------------------
074    // StreamConsumer Implementation
075    // ----------------------------------------------------------------------
076
077    /**
078     * {@inheritDoc}
079     */
080    public void consumeLine( String line )
081    {
082        if ( line.length() <= 2 )
083        {
084            return;
085        }
086
087        if ( removedRegexp.match( line ) )
088        {
089            String file = removedRegexp.getParen( 1 );
090            removedFiles.add( new ScmFile( file, ScmFileStatus.DELETED ) );
091        }
092        else
093        {
094            if ( logger.isInfoEnabled() )
095            {
096                logger.info( "could not parse line: " + line );
097            }
098
099            return;
100        }
101    }
102
103    public List<ScmFile> getRemovedFiles()
104    {
105        return removedFiles;
106    }
107
108}