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::warn() << "vng failed: Command `" << m_name
<< "' disabled with --disable option!" << endl
;
75 if (m_config
.contains("disable")) {
76 Logger::warn() << "vng failed: Command `" << m_name
<< "' disabled by configuration!" << endl
;
80 if (args
->undefinedOptions().count()) {
81 Logger::error() << "vng failed: ";
83 foreach(QString option
, args
->undefinedOptions()) {
85 Logger::error() << " : ";
86 Logger::error() << "unrecognized option or missing argument for; `" << option
<< "'" << endl
;
89 return InvalidOptions
;
92 if (args
->contains("help")) {
93 args
->usage(name(), argumentDescription());
94 QString command
= commandDescription();
95 if (!command
.isEmpty()) {
96 Logger::standardOut() << endl
;
97 Logger::standardOut() << command
;
102 if (args
->parseErrors().count()) {
103 Logger::error() << "vng failed: ";
104 foreach(QString e
, args
->parseErrors()) {
105 Logger::error() << e
<< endl
;
107 return InvalidOptions
;
110 m_dryRun
= args
->contains("dry-run") || m_config
.contains("dry-run");
112 if (args
->contains("repodir")) {
113 m_repository
= QDir(args
->optionArgument("repodir"));
114 QDir test
= m_repository
;
115 if (! test
.cd(".git")) {
116 Logger::error() << "repository dir `" << args
->optionArgument("repodir") << "' is not a repository" << endl
;
117 return InvalidOptions
;
121 m_repository
= m_config
.repository();
125 QDir
AbstractCommand::repository() const
130 bool AbstractCommand::dryRun() const
135 QString
AbstractCommand::name() const
140 QStringList
AbstractCommand::rebasedArguments() const
142 return m_rebasedArguments
;
145 bool AbstractCommand::checkInRepository() const
147 if (! QDir(m_repository
.absolutePath() + "/.git").exists()) {
148 Logger::error() << "vng failed: Unable to `" << m_name
<< "' here\n\n";
149 Logger::error() << "You need to be in a repository directory to run this command.\n";
155 bool AbstractCommand::shouldUsePager() const
157 CommandLineParser
*args
= CommandLineParser::instance();
158 return (m_config
.contains("use-pager") && !args
->contains("no-pager")) || args
->contains("use-pager");
161 void AbstractCommand::moveToRoot(RebaseOptions options
)
163 QString currentDir
= QDir::current().absolutePath();
164 QString repoDir
= m_repository
.absolutePath();
165 QDir::setCurrent(repoDir
);
166 if (! m_rebasedArguments
.isEmpty())
168 CommandLineParser
*args
= CommandLineParser::instance();
169 if (currentDir
!= repoDir
&& currentDir
.startsWith(repoDir
)) {
170 QString diff
= currentDir
.mid(repoDir
.length() + 1);
171 // calcuate rebased paths.
173 foreach (QString arg
, args
->arguments()) {
176 continue; // first is the command name
179 if (arg
== ".") // work around git bug that 'dir/.' doesn't work while 'dir' does work
181 else if (arg
.startsWith('/')) //absolute path
184 target
= diff
+'/'+ arg
;
185 if (options
& CheckFileSystem
) {
186 QFile
rebasedFile(target
);
187 if (rebasedFile
.exists())
188 m_rebasedArguments
<< target
;
190 m_rebasedArguments
<< arg
;
191 if (options
& PrintError
) {
194 Logger::error() << "Error, unknown file " << arg
<< endl
;
199 m_rebasedArguments
<< target
;
203 m_rebasedArguments
= args
->arguments().mid(1);