View Javadoc

1   package org.apache.maven.scm.provider.git;
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.codehaus.plexus.util.IOUtil;
23  import org.codehaus.plexus.util.StringUtils;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
35   *
36   */
37  public class GitConfigFileReader
38  {
39      private File configDirectory;
40  
41      public File getConfigDirectory()
42      {
43          if ( configDirectory == null )
44          {
45              
46              File confDir = new File( ".git" );
47              
48              if ( confDir.exists() ) 
49              {
50                  configDirectory = confDir;
51              }
52              else 
53              {
54                  confDir = new File( "." );
55                  
56                  if ( confDir.exists() ) 
57                  {
58                      configDirectory = confDir;
59                  }
60              }
61              
62          }
63  
64          return configDirectory;
65      }
66  
67      public void setConfigDirectory( File configDirectory )
68      {
69          this.configDirectory = configDirectory;
70      }
71  
72      public String getProperty( String group, String propertyName )
73      {
74          List<String> lines = getConfLines();
75  
76          boolean inGroup = false;
77          for ( Iterator<String> i = lines.iterator(); i.hasNext(); )
78          {
79              String line = i.next().trim();
80  
81              if ( !inGroup )
82              {
83                  if ( ( "[" + group + "]" ).equals( line ) )
84                  {
85                      inGroup = true;
86                  }
87              }
88              else
89              {
90                  if ( line.startsWith( "[" ) && line.endsWith( "]" ) )
91                  {
92                      //a new group start
93                      return null;
94                  }
95  
96                  if ( line.startsWith( "#" ) )
97                  {
98                      continue;
99                  }
100                 if ( line.indexOf( '=' ) < 0 )
101                 {
102                     continue;
103                 }
104 
105                 String property = line.substring( 0, line.indexOf( '=' ) ).trim();
106 
107                 if ( !property.equals( propertyName ) )
108                 {
109                     continue;
110                 }
111 
112                 String value = line.substring( line.indexOf( '=' ) + 1 );
113                 return value.trim();
114             }
115         }
116 
117         return null;
118     }
119 
120     /**
121      * Load the git config file
122      *
123      * @return the list of all lines
124      */
125     private List<String> getConfLines()
126     {
127         List<String> lines = new ArrayList<String>();
128 
129         BufferedReader reader = null;
130 
131         try
132         {
133             if ( getConfigDirectory().exists() )
134             {
135                 reader = new BufferedReader( new FileReader( new File( getConfigDirectory(), "config" ) ) );
136                 String line;
137                 while ( ( line = reader.readLine() ) != null )
138                 {
139                     if ( !line.startsWith( "#" ) && StringUtils.isNotEmpty( line ) )
140                     {
141                         lines.add( line );
142                     }
143                 }
144             }
145         }
146         catch ( IOException e )
147         {
148             lines.clear();
149         }
150         finally
151         {
152             IOUtil.close( reader );
153             reader = null;
154         }
155 
156         return lines;
157     }
158 }