Move Matcher to its own class+file CommitsMatcher
[vng.git] / Configuration.cpp
blob549373087c43ca7842758e96c6356967ec105613
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "Configuration.h"
19 #include "Logger.h"
20 #include "GitRunner.h"
21 #include "AbstractCommand.h"
23 #include <QDebug>
24 #include <QProcess>
26 #ifndef Q_OS_WIN
27 #include <unistd.h>
28 #endif
30 Configuration::Configuration(const QString &section)
31 : m_repoDir("."),
32 m_section(section),
33 m_dirty(true),
34 m_emptyRepo(false),
35 m_fetchedBranches(false)
39 bool Configuration::contains(const QString & key) const
41 const_cast<Configuration*> (this)->readConfig();
42 return m_options.contains(key);
45 QDir Configuration::repository() const
47 const_cast<Configuration*> (this)->readConfig();
48 return m_repoDir;
51 void Configuration::readConfig()
53 if (!m_dirty)
54 return;
55 m_dirty = false;
56 QObject deleterParent;
58 QDir dir = QDir::current();
59 do {
60 QDir git = dir;
61 if (git.cd(".git")) {
62 m_repoDir = dir;
63 QDir refs(git.absoluteFilePath("refs/heads"));
64 m_emptyRepo = refs.count() == 2; // only '.' and '..'
65 break;
67 if (!dir.cdUp())
68 break;
69 } while(!dir.isRoot());
71 QString home = QDir::homePath();
72 QFile *config;
73 config = new QFile(home + "/.vng/config", &deleterParent);
74 if (! config->exists())
75 config = new QFile(home + "/.darcs/defaults", &deleterParent);
76 if (config->exists()) {
77 if (! config->open(QIODevice::ReadOnly)) {
78 Logger::error() << "Failed to open config file, is it readable?\n";
79 return;
82 char buf[1024];
83 while(true) {
84 qint64 lineLength = config->readLine(buf, sizeof(buf));
85 if (lineLength == -1)
86 break;
87 QString line = QString::fromUtf8(buf, lineLength);
88 QString option;
89 if (line.startsWith("ALL "))
90 option = line.mid(3).trimmed();
91 else if (line.length() > m_section.length() && line.startsWith(m_section))
92 option = line.mid(m_section.length()).trimmed();
93 if (! option.isEmpty()) {
94 const int index = option.indexOf(' ');
95 if (index > 0) {
96 m_options.insert(option.left(index).trimmed(), option.mid(index).trimmed());
98 else
99 m_options.insert(option, QString());
102 config->close();
106 QString Configuration::optionArgument(const QString &optionName, const QString &defaultValue) const
108 if (m_options.contains(optionName))
109 return m_options[optionName];
110 return defaultValue;
113 bool Configuration::colorTerm() const
115 #ifndef Q_OS_WIN
116 if (isatty(1))
117 return QString(getenv("TERM")) != QString("dumb") && !Logger::hasNonColorPager();
118 #endif
119 return false;
122 bool Configuration::isEmptyRepo() const
124 const_cast<Configuration*> (this)->readConfig();
125 return m_emptyRepo;
128 QList<Branch> Configuration::allBranches()
130 fetchBranches();
131 QList<Branch> answer;
132 answer += m_localBranches;
133 answer += m_remoteBranches;
134 return answer;
137 QList<Branch> Configuration::branches()
139 fetchBranches();
140 return m_localBranches;
143 QList<Branch> Configuration::remoteBranches()
145 fetchBranches();
146 return m_remoteBranches;
149 void Configuration::fetchBranches()
151 if (m_fetchedBranches)
152 return;
153 m_fetchedBranches = true;
155 QProcess git;
156 QStringList arguments;
157 arguments << "ls-remote" << ".";
158 GitRunner runner(git, arguments);
159 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
160 if (rc != AbstractCommand::Ok) {
161 return;
163 char buf[1024];
164 while(true) {
165 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
166 if (lineLength == -1)
167 break;
168 if (lineLength > 46) { // only take stuff that is in the 'refs' dir.
169 QString name(buf + 46);
170 name = name.trimmed(); // remove linefeed
171 Branch branch(name);
172 if (name.startsWith("remotes"))
173 m_remoteBranches.append(branch);
174 else
175 m_localBranches.append(branch);