001    package 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    
022    import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
023    import org.apache.maven.scm.log.ScmLogger;
024    import org.apache.regexp.RE;
025    import org.apache.regexp.RESyntaxException;
026    import org.codehaus.plexus.util.cli.StreamConsumer;
027    
028    import java.util.HashMap;
029    
030    /**
031     * @author Bertrand Paquet
032     */
033    public class GitRemoteInfoConsumer
034        implements StreamConsumer
035    {
036    
037        /**
038         * The pattern used to match branches
039         */
040        private static final String BRANCH_PATTERN = "^(.*)\\s+refs/heads/(.*)";
041    
042        /**
043         * The pattern used to match tags
044         */
045        private static final String TAGS_PATTERN = "^(.*)\\s+refs/tags/(.*)";
046    
047        private ScmLogger logger;
048    
049        private RemoteInfoScmResult remoteInfoScmResult;
050    
051        private RE branchRegexp;
052    
053        private RE tagRegexp;
054    
055        // ----------------------------------------------------------------------
056        //
057        // ----------------------------------------------------------------------
058    
059        public GitRemoteInfoConsumer( ScmLogger logger, String commandLine )
060        {
061            this.logger = logger;
062            this.remoteInfoScmResult =
063                new RemoteInfoScmResult( commandLine, new HashMap<String, String>(), new HashMap<String, String>() );
064    
065            try
066            {
067                this.branchRegexp = new RE( BRANCH_PATTERN );
068                this.tagRegexp = new RE( TAGS_PATTERN );
069            }
070            catch ( RESyntaxException ex )
071            {
072                throw new RuntimeException(
073                    "INTERNAL ERROR: Could not create regexp to parse git ls-remote file. This shouldn't happen. Something is probably wrong with the oro installation.",
074                    ex );
075            }
076        }
077    
078        // ----------------------------------------------------------------------
079        // StreamConsumer Implementation
080        // ----------------------------------------------------------------------
081    
082        /**
083         * {@inheritDoc}
084         */
085        public void consumeLine( String line )
086        {
087            if ( logger.isDebugEnabled() )
088            {
089                logger.debug( line );
090            }
091            if ( branchRegexp.match( line ) )
092            {
093                remoteInfoScmResult.getBranches().put( branchRegexp.getParen( 2 ), branchRegexp.getParen( 1 ) );
094            }
095            if ( tagRegexp.match( line ) )
096            {
097                remoteInfoScmResult.getTags().put( tagRegexp.getParen( 2 ), tagRegexp.getParen( 1 ) );
098            }
099    
100        }
101    
102        public RemoteInfoScmResult getRemoteInfoScmResult()
103        {
104            return remoteInfoScmResult;
105        }
106    
107    }