View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.git.gitexe.command.remoteinfo;
20  
21  import java.util.HashMap;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
26  import org.apache.maven.scm.util.AbstractConsumer;
27  
28  /**
29   * @author Bertrand Paquet
30   */
31  public class GitRemoteInfoConsumer extends AbstractConsumer {
32  
33      /**
34       * The pattern used to match branches
35       */
36      private static final Pattern BRANCH_PATTERN = Pattern.compile("^(.*)\\s+refs/heads/(.*)");
37  
38      /**
39       * The pattern used to match tags
40       */
41      private static final Pattern TAGS_PATTERN = Pattern.compile("^(.*)\\s+refs/tags/(.*)");
42  
43      private final RemoteInfoScmResult remoteInfoScmResult;
44  
45      // ----------------------------------------------------------------------
46      //
47      // ----------------------------------------------------------------------
48  
49      public GitRemoteInfoConsumer(String commandLine) {
50          this.remoteInfoScmResult = new RemoteInfoScmResult(commandLine, new HashMap<>(), new HashMap<>());
51      }
52  
53      // ----------------------------------------------------------------------
54      // StreamConsumer Implementation
55      // ----------------------------------------------------------------------
56  
57      /**
58       * {@inheritDoc}
59       */
60      public void consumeLine(String line) {
61          if (logger.isDebugEnabled()) {
62              logger.debug(line);
63          }
64  
65          Matcher matcher = BRANCH_PATTERN.matcher(line);
66          if (matcher.matches()) {
67              remoteInfoScmResult.getBranches().put(matcher.group(2), matcher.group(1));
68          }
69  
70          matcher = TAGS_PATTERN.matcher(line);
71          if (matcher.matches()) {
72              remoteInfoScmResult.getTags().put(matcher.group(2), matcher.group(1));
73          }
74      }
75  
76      public RemoteInfoScmResult getRemoteInfoScmResult() {
77          return remoteInfoScmResult;
78      }
79  }