001package org.apache.maven.scm.provider.git.gitexe.command.remoteinfo;
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.command.remoteinfo.RemoteInfoScmResult;
023import org.apache.maven.scm.log.ScmLogger;
024import org.codehaus.plexus.util.cli.StreamConsumer;
025
026import java.util.HashMap;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030/**
031 * @author Bertrand Paquet
032 */
033public class GitRemoteInfoConsumer
034    implements StreamConsumer
035{
036
037    /**
038     * The pattern used to match branches
039     */
040    private static final Pattern BRANCH_PATTERN = Pattern.compile( "^(.*)\\s+refs/heads/(.*)" );
041
042    /**
043     * The pattern used to match tags
044     */
045    private static final Pattern TAGS_PATTERN = Pattern.compile( "^(.*)\\s+refs/tags/(.*)" );
046
047    private ScmLogger logger;
048
049    private RemoteInfoScmResult remoteInfoScmResult;
050
051    // ----------------------------------------------------------------------
052    //
053    // ----------------------------------------------------------------------
054
055    public GitRemoteInfoConsumer( ScmLogger logger, String commandLine )
056    {
057        this.logger = logger;
058        this.remoteInfoScmResult =
059            new RemoteInfoScmResult( commandLine, new HashMap<String, String>(), new HashMap<String, String>() );
060    }
061
062    // ----------------------------------------------------------------------
063    // StreamConsumer Implementation
064    // ----------------------------------------------------------------------
065
066    /**
067     * {@inheritDoc}
068     */
069    public void consumeLine( String line )
070    {
071        if ( logger.isDebugEnabled() )
072        {
073            logger.debug( line );
074        }
075        
076        Matcher matcher = BRANCH_PATTERN.matcher( line );
077        if ( matcher.matches() )
078        {
079            remoteInfoScmResult.getBranches().put( matcher.group( 2 ), matcher.group( 1 ) );
080        }
081        
082        matcher = TAGS_PATTERN.matcher( line );
083        if ( matcher.matches() )
084        {
085            remoteInfoScmResult.getTags().put( matcher.group( 2 ), matcher.group( 1 ) );
086        }
087
088    }
089
090    public RemoteInfoScmResult getRemoteInfoScmResult()
091    {
092        return remoteInfoScmResult;
093    }
094
095}