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