Coverage Report - org.apache.maven.scm.provider.git.GitConfigFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
GitConfigFileReader
0 %
0/44
0 %
0/30
5,5
 
 1  
 package org.apache.maven.scm.provider.git;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 35  
  * @version $Id: GitConfigFileReader.java 1057016 2011-01-09 20:11:26Z olamy $
 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  
                     //a new group start
 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  
      * Load the git config file
 122  
      *
 123  
      * @return the list of all lines
 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  
 }