001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.gitexe.command.remoteinfo;
020
021import java.util.HashMap;
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
026import org.apache.maven.scm.util.AbstractConsumer;
027
028/**
029 * @author Bertrand Paquet
030 */
031public class GitRemoteInfoConsumer extends AbstractConsumer {
032
033    /**
034     * The pattern used to match branches
035     */
036    private static final Pattern BRANCH_PATTERN = Pattern.compile("^(.*)\\s+refs/heads/(.*)");
037
038    /**
039     * The pattern used to match tags
040     */
041    private static final Pattern TAGS_PATTERN = Pattern.compile("^(.*)\\s+refs/tags/(.*)");
042
043    private final RemoteInfoScmResult remoteInfoScmResult;
044
045    // ----------------------------------------------------------------------
046    //
047    // ----------------------------------------------------------------------
048
049    public GitRemoteInfoConsumer(String commandLine) {
050        this.remoteInfoScmResult = new RemoteInfoScmResult(commandLine, new HashMap<>(), new HashMap<>());
051    }
052
053    // ----------------------------------------------------------------------
054    // StreamConsumer Implementation
055    // ----------------------------------------------------------------------
056
057    /**
058     * {@inheritDoc}
059     */
060    public void consumeLine(String line) {
061        if (logger.isDebugEnabled()) {
062            logger.debug(line);
063        }
064
065        Matcher matcher = BRANCH_PATTERN.matcher(line);
066        if (matcher.matches()) {
067            remoteInfoScmResult.getBranches().put(matcher.group(2), matcher.group(1));
068        }
069
070        matcher = TAGS_PATTERN.matcher(line);
071        if (matcher.matches()) {
072            remoteInfoScmResult.getTags().put(matcher.group(2), matcher.group(1));
073        }
074    }
075
076    public RemoteInfoScmResult getRemoteInfoScmResult() {
077        return remoteInfoScmResult;
078    }
079}