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/>.
19 #include "AbstractCommand.h"
20 #include "CommandLineParser.h"
25 static const CommandLineOption options
[] = {
26 {"-v, --verbose", "give verbose output"},
27 {"-q, --quiet", "suppress informational output"},
28 {"--debug", "print debugging information"},
29 {"--standard-verbosity", "neither verbose nor quiet output"},
30 {"--repodir DIRECTORY", "specify the repository directory in which to run"},
31 {"--dry-run", "don't actually take the action"},
32 {"--disable", "disable this command"},
33 {"-h, --help", "shows brief description of command and its arguments"},
34 {"--use-pager", "use a pager to show the output screen by screen"},
35 {"--no-pager", "don't use the pager when showing the output"},
39 AbstractCommand::AbstractCommand(const QString
&name
)
40 : m_config(name
), m_dryRun(false), m_name(name
)
42 CommandLineParser::addOptionDefinitions(options
);
45 AbstractCommand::~AbstractCommand()
49 AbstractCommand::ReturnCodes
AbstractCommand::start()
51 CommandLineParser
*args
= CommandLineParser::instance();
53 if (m_config
.contains("quiet"))
54 Logger::setVerbosity(Logger::Quiet
);
55 else if (m_config
.contains("verbose"))
56 Logger::setVerbosity(Logger::Verbose
);
57 else if (m_config
.contains("standard-verbosity"))
58 Logger::setVerbosity(Logger::Chatty
);
59 else if (m_config
.contains("debug"))
60 Logger::setVerbosity(Logger::Debug
);
62 if (args
->contains("quiet"))
63 Logger::setVerbosity(Logger::Quiet
);
64 else if (args
->contains("verbose"))
65 Logger::setVerbosity(Logger::Verbose
);
66 else if (args
->contains("standard-verbosity"))
67 Logger::setVerbosity(Logger::Chatty
);
68 else if (args
->contains("debug"))
69 Logger::setVerbosity(Logger::Debug
);
71 if (args
->contains("disable")) {
72 Logger::info() << "vng failed: Command `" << m_name
<< "' disabled with --disable option!" << endl
;
75 if (m_config
.contains("disable")) {
76 Logger::info() << "vng failed: Command `" << m_name
<< "' disabled by configuration!" << endl
;
80 if (args
->undefinedOptions().count()) {
81 Logger::error() << "vng failed: ";
82 foreach(QString option
, args
->undefinedOptions()) {
83 Logger::error() << "unrecognized option `" << option
<< "'" << endl
;
85 return InvalidOptions
;
88 if (args
->contains("help")) {
89 args
->usage(name(), argumentDescription());
90 QString command
= commandDescription();
91 if (!command
.isEmpty()) {
92 Logger::standardOut() << endl
;
93 Logger::standardOut() << command
;
98 if (args
->parseErrors().count()) {
99 Logger::error() << "vng failed: ";
100 foreach(QString e
, args
->parseErrors()) {
101 Logger::error() << e
<< endl
;
103 return InvalidOptions
;
106 m_dryRun
= args
->contains("dry-run") || m_config
.contains("dry-run");
108 if (args
->contains("repodir")) {
109 m_repository
= QDir(args
->optionArgument("repodir"));
110 QDir test
= m_repository
;
111 if (! test
.cd(".git")) {
112 Logger::error() << "repository dir `" << args
->optionArgument("repodir") << "' is not a repository" << endl
;
113 return InvalidOptions
;
117 m_repository
= m_config
.repository();
121 QDir
AbstractCommand::repository() const
126 bool AbstractCommand::dryRun() const
131 QString
AbstractCommand::name() const
136 QStringList
AbstractCommand::rebasedArguments() const
138 return m_rebasedArguments
;
141 bool AbstractCommand::checkInRepository() const
143 if (! QDir(m_repository
.absolutePath() + "/.git").exists()) {
144 Logger::error() << "vng failed: Unable to `" << m_name
<< "' here\n\n";
145 Logger::error() << "You need to be in a repository directory to run this command.\n";
151 bool AbstractCommand::shouldUsePager() const
153 CommandLineParser
*args
= CommandLineParser::instance();
154 return m_config
.contains("use-pager") && !args
->contains("no-pager") || args
->contains("use-pager");
157 void AbstractCommand::moveToRoot()
159 QString currentDir
= QDir::current().absolutePath();
160 QString newRepoDir
= m_repository
.absolutePath();
161 QDir::setCurrent(newRepoDir
);
162 if (! m_rebasedArguments
.isEmpty())
164 CommandLineParser
*args
= CommandLineParser::instance();
165 if (currentDir
!= newRepoDir
&& currentDir
.startsWith(newRepoDir
)) {
166 QString diff
= currentDir
.mid(newRepoDir
.length() + 1);
167 // calcuate rebased paths.
169 foreach (QString arg
, args
->arguments()) {
172 continue; // first is the command name
174 if (arg
== ".") // work around git bug that 'dir/.' doesn't work while 'dir' does work
175 m_rebasedArguments
<< diff
;
177 m_rebasedArguments
<< diff
+'/'+ arg
;
181 m_rebasedArguments
= args
->arguments().mid(1);