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