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