001package org.apache.maven.scm.provider.git;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.codehaus.plexus.util.IOUtil;
023import org.codehaus.plexus.util.StringUtils;
024
025import java.io.BufferedReader;
026import java.io.File;
027import java.io.FileReader;
028import java.io.IOException;
029import java.util.ArrayList;
030import java.util.Iterator;
031import java.util.List;
032
033/**
034 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
035 *
036 */
037public class GitConfigFileReader
038{
039    private File configDirectory;
040
041    public File getConfigDirectory()
042    {
043        if ( configDirectory == null )
044        {
045            
046            File confDir = new File( ".git" );
047            
048            if ( confDir.exists() ) 
049            {
050                configDirectory = confDir;
051            }
052            else 
053            {
054                confDir = new File( "." );
055                
056                if ( confDir.exists() ) 
057                {
058                    configDirectory = confDir;
059                }
060            }
061            
062        }
063
064        return configDirectory;
065    }
066
067    public void setConfigDirectory( File configDirectory )
068    {
069        this.configDirectory = configDirectory;
070    }
071
072    public String getProperty( String group, String propertyName )
073    {
074        List<String> lines = getConfLines();
075
076        boolean inGroup = false;
077        for ( Iterator<String> i = lines.iterator(); i.hasNext(); )
078        {
079            String line = i.next().trim();
080
081            if ( !inGroup )
082            {
083                if ( ( "[" + group + "]" ).equals( line ) )
084                {
085                    inGroup = true;
086                }
087            }
088            else
089            {
090                if ( line.startsWith( "[" ) && line.endsWith( "]" ) )
091                {
092                    //a new group start
093                    return null;
094                }
095
096                if ( line.startsWith( "#" ) )
097                {
098                    continue;
099                }
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     * Load the git config file
122     *
123     * @return the list of all lines
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}