001    package org.apache.maven.scm.provider.git.gitexe.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    
022    import org.apache.maven.scm.ScmFile;
023    import org.apache.maven.scm.ScmFileStatus;
024    import org.apache.maven.scm.log.ScmLogger;
025    import org.apache.regexp.RE;
026    import org.apache.regexp.RESyntaxException;
027    import org.codehaus.plexus.util.StringUtils;
028    import org.codehaus.plexus.util.cli.StreamConsumer;
029    
030    import java.io.File;
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    /**
035     * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
036     * @version $Id: GitStatusConsumer.java 1375087 2012-08-20 16:10:26Z olamy $
037     */
038    public class GitStatusConsumer
039        implements StreamConsumer
040    {
041        /**
042         * The pattern used to match added file lines
043         */
044        private static final String ADDED_PATTERN = "^A[ M]* (.*)$";
045    
046        /**
047         * The pattern used to match modified file lines
048         */
049        private static final String MODIFIED_PATTERN = "^ *M[ M]* (.*)$";
050    
051        /**
052         * The pattern used to match deleted file lines
053         */
054        private static final String DELETED_PATTERN = "^ *D * (.*)$";
055    
056        /**
057         * @see #ADDED_PATTERN
058         */
059        private RE addedRegexp;
060    
061        /**
062         * @see #MODIFIED_PATTERN
063         */
064        private RE modifiedRegexp;
065    
066        /**
067         * @see #DELETED_PATTERN
068         */
069        private RE deletedRegexp;
070    
071        private ScmLogger logger;
072    
073        private File workingDirectory;
074    
075        private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
076    
077        // ----------------------------------------------------------------------
078        //
079        // ----------------------------------------------------------------------
080    
081        public GitStatusConsumer( ScmLogger logger, File workingDirectory )
082        {
083            this.logger = logger;
084            this.workingDirectory = workingDirectory;
085    
086            try
087            {
088                addedRegexp = new RE( ADDED_PATTERN );
089                modifiedRegexp = new RE( MODIFIED_PATTERN );
090                deletedRegexp = new RE( DELETED_PATTERN );
091            }
092            catch ( RESyntaxException ex )
093            {
094                throw new RuntimeException(
095                    "INTERNAL ERROR: Could not create regexp to parse git log file. This shouldn't happen. Something is probably wrong with the oro installation.",
096                    ex );
097            }
098        }
099    
100        // ----------------------------------------------------------------------
101        // StreamConsumer Implementation
102        // ----------------------------------------------------------------------
103    
104        /**
105         * {@inheritDoc}
106         */
107        public void consumeLine( String line )
108        {
109            if ( logger.isDebugEnabled() )
110            {
111                logger.debug( line );
112            }
113            if ( StringUtils.isEmpty( line ) )
114            {
115                return;
116            }
117    
118            ScmFileStatus status = null;
119    
120            String file = null;
121    
122            if ( addedRegexp.match( line ) )
123            {
124                status = ScmFileStatus.ADDED;
125                file = addedRegexp.getParen( 1 );
126            }
127            else if ( modifiedRegexp.match( line ) )
128            {
129                status = ScmFileStatus.MODIFIED;
130                file = modifiedRegexp.getParen( 1 );
131            }
132            else if ( deletedRegexp.match( line ) )
133            {
134                status = ScmFileStatus.DELETED;
135                file = deletedRegexp.getParen( 1 );
136            }
137    
138            // If the file isn't a file; don't add it.
139            if ( file != null && status != null )
140            {
141                if ( workingDirectory != null )
142                {
143                    if ( status == ScmFileStatus.DELETED )
144                    {
145                        if ( new File( workingDirectory, file ).isFile() )
146                        {
147                            return;
148                        }
149                    }
150                    else
151                    {
152                        if ( !new File( workingDirectory, file ).isFile() )
153                        {
154                            return;
155                        }
156                    }
157                }
158    
159                changedFiles.add( new ScmFile( file, status ) );
160            }
161    
162    
163        }
164    
165        public List<ScmFile> getChangedFiles()
166        {
167            return changedFiles;
168        }
169    }