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