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"
20 #include "GitRunner.h"
21 #include "AbstractCommand.h"
30 Configuration::Configuration(const QString
§ion
)
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();
51 void Configuration::readConfig()
56 QObject deleterParent
;
58 QDir dir
= QDir::current();
63 QDir
refs(git
.absoluteFilePath("refs/heads"));
64 m_emptyRepo
= refs
.count() == 2; // only '.' and '..'
69 } while(!dir
.isRoot());
71 QString home
= QDir::homePath();
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";
84 qint64 lineLength
= config
->readLine(buf
, sizeof(buf
));
87 QString line
= QString::fromUtf8(buf
, lineLength
);
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(' ');
96 m_options
.insert(option
.left(index
).trimmed(), option
.mid(index
).trimmed());
99 m_options
.insert(option
, QString());
106 QString
Configuration::optionArgument(const QString
&optionName
, const QString
&defaultValue
) const
108 if (m_options
.contains(optionName
))
109 return m_options
[optionName
];
113 bool Configuration::colorTerm() const
117 return QString(getenv("TERM")) != QString("dumb") && !Logger::hasNonColorPager();
122 bool Configuration::isEmptyRepo() const
124 const_cast<Configuration
*> (this)->readConfig();
128 QList
<Branch
> Configuration::allBranches()
131 QList
<Branch
> answer
;
132 answer
+= m_localBranches
;
133 answer
+= m_remoteBranches
;
137 QList
<Branch
> Configuration::branches()
140 return m_localBranches
;
143 QList
<Branch
> Configuration::remoteBranches()
146 return m_remoteBranches
;
149 void Configuration::fetchBranches()
151 if (m_fetchedBranches
)
153 m_fetchedBranches
= true;
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
) {
165 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
166 if (lineLength
== -1)
168 if (lineLength
> 46) { // only take stuff that is in the 'refs' dir.
169 QString
name(buf
+ 46);
170 name
= name
.trimmed(); // remove linefeed
172 if (name
.startsWith("remotes"))
173 m_remoteBranches
.append(branch
);
175 m_localBranches
.append(branch
);