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