1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
99
100
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 }