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.util;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.apache.maven.scm.providers.gitlib.settings.Settings;
025import org.apache.maven.scm.providers.gitlib.settings.io.xpp3.GitXpp3Reader;
026import org.codehaus.plexus.util.ReaderFactory;
027import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
028
029/**
030 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
031 *
032 */
033public class GitUtil {
034    protected static final String GIT_SETTINGS_FILENAME = "git-settings.xml";
035
036    public static final File DEFAULT_SETTINGS_DIRECTORY = new File(System.getProperty("user.home"), ".scm");
037
038    private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
039
040    private static Settings settings;
041
042    private GitUtil() {
043        // no op
044    }
045
046    public static Settings getSettings() {
047        if (settings == null) {
048            settings = readSettings();
049        }
050        return settings;
051    }
052
053    public static Settings readSettings() {
054        File settingsFile = getSettingsFile();
055
056        if (settingsFile.exists()) {
057            GitXpp3Reader reader = new GitXpp3Reader();
058            try {
059                return reader.read(ReaderFactory.newXmlReader(settingsFile));
060            } catch (IOException e) {
061                // Nothing to do
062            } catch (XmlPullParserException e) {
063                String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
064
065                System.err.println(message);
066            }
067        }
068
069        return new Settings();
070    }
071
072    public static void setSettingsDirectory(File directory) {
073        settingsDirectory = directory;
074        settings = readSettings();
075    }
076
077    public static File getSettingsFile() {
078        return new File(settingsDirectory, GIT_SETTINGS_FILENAME);
079    }
080}