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