View Javadoc

1   package org.apache.maven.scm.provider.git.gitexe.command.update;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.log.ScmLogger;
23  import org.apache.maven.scm.util.AbstractConsumer;
24  import org.apache.regexp.RE;
25  import org.apache.regexp.RESyntaxException;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  /**
29   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
30   *
31   */
32  public class GitLatestRevisionCommandConsumer
33      extends AbstractConsumer
34  {
35  
36      /**
37       * The pattern used to match git log latest revision lines
38       */
39      private static final String LATESTREV_PATTERN = "^commit \\s*(.*)";
40  
41      /**
42       * The regular expression used to match git log latest revision lines
43       */
44      private RE latestRevRegexp;
45  
46      private String latestRevision;
47  
48      public GitLatestRevisionCommandConsumer( ScmLogger logger )
49      {
50          super( logger );
51  
52          try
53          {
54              latestRevRegexp = new RE( LATESTREV_PATTERN );
55          }
56          catch ( RESyntaxException ex )
57          {
58              throw new RuntimeException( "INTERNAL ERROR: Could not create regexp to parse git log file. This shouldn't happen. "
59                                          + "Something is probably wrong with the oro installation.",
60                                          ex );
61          }
62  
63      }
64  
65      /** {@inheritDoc} */
66      public void consumeLine( String line )
67      {
68          if ( getLogger().isDebugEnabled() )
69          {
70              getLogger().debug( "GitLatestRevisionCommandConsumer consumeLine : " + line );
71          }
72          if ( line == null || StringUtils.isEmpty( line ) )
73          {
74              return;
75          }
76  
77          processGetLatestRevision( line );
78      }
79  
80      public String getLatestRevision()
81      {
82          return latestRevision;
83      }
84  
85      /**
86       * Process the current input line for the latest revision
87       *
88       * @param line A line of text from the git log output
89       */
90      private void processGetLatestRevision( String line )
91      {
92          if ( !latestRevRegexp.match( line ) )
93          {
94              return;
95          }
96  
97          latestRevision = latestRevRegexp.getParen( 1 );
98      }
99  
100 }