001package org.apache.maven.scm.provider.git.gitexe.command.update;
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.log.ScmLogger;
023import org.apache.maven.scm.util.AbstractConsumer;
024import org.apache.regexp.RE;
025import org.apache.regexp.RESyntaxException;
026import org.codehaus.plexus.util.StringUtils;
027
028/**
029 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
030 *
031 */
032public class GitLatestRevisionCommandConsumer
033    extends AbstractConsumer
034{
035
036    /**
037     * The pattern used to match git log latest revision lines
038     */
039    private static final String LATESTREV_PATTERN = "^commit \\s*(.*)";
040
041    /**
042     * The regular expression used to match git log latest revision lines
043     */
044    private RE latestRevRegexp;
045
046    private String latestRevision;
047
048    public GitLatestRevisionCommandConsumer( ScmLogger logger )
049    {
050        super( logger );
051
052        try
053        {
054            latestRevRegexp = new RE( LATESTREV_PATTERN );
055        }
056        catch ( RESyntaxException ex )
057        {
058            throw new RuntimeException( "INTERNAL ERROR: Could not create regexp to parse git log file. This shouldn't happen. "
059                                        + "Something is probably wrong with the oro installation.",
060                                        ex );
061        }
062
063    }
064
065    /** {@inheritDoc} */
066    public void consumeLine( String line )
067    {
068        if ( getLogger().isDebugEnabled() )
069        {
070            getLogger().debug( "GitLatestRevisionCommandConsumer consumeLine : " + line );
071        }
072        if ( line == null || StringUtils.isEmpty( line ) )
073        {
074            return;
075        }
076
077        processGetLatestRevision( line );
078    }
079
080    public String getLatestRevision()
081    {
082        return latestRevision;
083    }
084
085    /**
086     * Process the current input line for the latest revision
087     *
088     * @param line A line of text from the git log output
089     */
090    private void processGetLatestRevision( String line )
091    {
092        if ( !latestRevRegexp.match( line ) )
093        {
094            return;
095        }
096
097        latestRevision = latestRevRegexp.getParen( 1 );
098    }
099
100}