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 */
032public class GitConfigFileReader {
033    private File configDirectory;
034
035    public File getConfigDirectory() {
036        if (configDirectory == null) {
037
038            File confDir = new File(".git");
039
040            if (confDir.exists()) {
041                configDirectory = confDir;
042            } else {
043                confDir = new File(".");
044
045                if (confDir.exists()) {
046                    configDirectory = confDir;
047                }
048            }
049        }
050
051        return configDirectory;
052    }
053
054    public void setConfigDirectory(File configDirectory) {
055        this.configDirectory = configDirectory;
056    }
057
058    public String getProperty(String group, String propertyName) {
059        List<String> lines = getConfLines();
060
061        boolean inGroup = false;
062        for (Iterator<String> i = lines.iterator(); i.hasNext(); ) {
063            String line = i.next().trim();
064
065            if (!inGroup) {
066                if (("[" + group + "]").equals(line)) {
067                    inGroup = true;
068                }
069            } else {
070                if (line.startsWith("[") && line.endsWith("]")) {
071                    // a new group start
072                    return null;
073                }
074
075                if (line.startsWith("#")) {
076                    continue;
077                }
078                if (line.indexOf('=') < 0) {
079                    continue;
080                }
081
082                String property = line.substring(0, line.indexOf('=')).trim();
083
084                if (!property.equals(propertyName)) {
085                    continue;
086                }
087
088                String value = line.substring(line.indexOf('=') + 1);
089                return value.trim();
090            }
091        }
092
093        return null;
094    }
095
096    /**
097     * Load the git config file.
098     *
099     * @return the list of all lines
100     */
101    private List<String> getConfLines() {
102        List<String> lines = new ArrayList<>();
103
104        try {
105            if (getConfigDirectory().exists()) {
106                try (BufferedReader reader =
107                        new BufferedReader(new FileReader(new File(getConfigDirectory(), "config")))) {
108                    String line;
109                    while ((line = reader.readLine()) != null) {
110                        if (!line.startsWith("#") && !line.isEmpty()) {
111                            lines.add(line);
112                        }
113                    }
114                }
115            }
116        } catch (IOException e) {
117            lines.clear();
118        }
119
120        return lines;
121    }
122}