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 char *name
)
40 : m_config(name
), m_dryRun(false), m_name(QString::fromLatin1(name
))
42 CommandLineParser::addOptionDefinitions(options
);
45 AbstractCommand::~AbstractCommand()
49 AbstractCommand::ReturnCodes
AbstractCommand::start()
51 CommandLineParser
*args
= CommandLineParser::instance();
53 if (m_config
.contains(QLatin1String("quiet")))
54 Logger::setVerbosity(Logger::Quiet
);
55 else if (m_config
.contains(QLatin1String("verbose")))
56 Logger::setVerbosity(Logger::Verbose
);
57 else if (m_config
.contains(QLatin1String("standard-verbosity")))
58 Logger::setVerbosity(Logger::Chatty
);
59 else if (m_config
.contains(QLatin1String("debug")))
60 Logger::setVerbosity(Logger::Debug
);
62 if (args
->contains(QLatin1String("quiet")))
63 Logger::setVerbosity(Logger::Quiet
);
64 else if (args
->contains(QLatin1String("verbose")))
65 Logger::setVerbosity(Logger::Verbose
);
66 else if (args
->contains(QLatin1String("standard-verbosity")))
67 Logger::setVerbosity(Logger::Chatty
);
68 else if (args
->contains(QLatin1String("debug")))
69 Logger::setVerbosity(Logger::Debug
);
71 if (args
->contains(QLatin1String("disable"))) {
72 Logger::warn() << "vng failed: Command `" << m_name
<< "' disabled with --disable option!" << endl
;
75 if (m_config
.contains(QLatin1String("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(QLatin1String("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(QLatin1String("dry-run")) || m_config
.contains(QLatin1String("dry-run"));
112 if (args
->contains(QLatin1String("repodir"))) {
113 m_repository
= QDir(args
->optionArgument(QLatin1String("repodir")));
114 QDir test
= m_repository
;
115 if (! test
.cd(QLatin1String(".git"))) {
116 Logger::error() << "repository dir `" << args
->optionArgument(QLatin1String("repodir"))
117 << "' is not a repository" << endl
;
118 return InvalidOptions
;
121 m_repository
= m_config
.repository();
126 QDir
AbstractCommand::repository() const
131 bool AbstractCommand::dryRun() const
136 QString
AbstractCommand::name() const
141 QStringList
AbstractCommand::rebasedArguments() const
143 return m_rebasedArguments
;
146 bool AbstractCommand::checkInRepository() const
148 if (! QDir(m_repository
.absolutePath() + QLatin1String("/.git")).exists()) {
149 Logger::error() << "vng failed: Unable to `" << m_name
<< "' here\n\n";
150 Logger::error() << "You need to be in a repository directory to run this command.\n";
156 bool AbstractCommand::shouldUsePager() const
158 CommandLineParser
*args
= CommandLineParser::instance();
159 return (m_config
.contains(QLatin1String("use-pager")) && !args
->contains(QLatin1String("no-pager")))
160 || args
->contains(QLatin1String("use-pager"));
163 void AbstractCommand::moveToRoot(RebaseOptions options
)
165 QString currentDir
= QDir::current().absolutePath();
166 QString repoDir
= m_repository
.absolutePath();
167 QDir::setCurrent(repoDir
);
168 if (! m_rebasedArguments
.isEmpty())
170 CommandLineParser
*args
= CommandLineParser::instance();
171 if (currentDir
!= repoDir
&& currentDir
.startsWith(repoDir
)) {
172 QString diff
= currentDir
.mid(repoDir
.length() + 1);
173 // calcuate rebased paths.
175 foreach (QString arg
, args
->arguments()) {
178 continue; // first is the command name
181 if (arg
== QLatin1String(".")) // work around git bug that 'dir/.' doesn't work while 'dir' does work
183 else if (arg
.startsWith(QLatin1Char('/'))) //absolute path
186 target
= diff
+ QLatin1Char('/') + arg
;
187 if (options
& CheckFileSystem
) {
188 QFile
rebasedFile(target
);
189 if (rebasedFile
.exists()) {
190 m_rebasedArguments
<< target
;
192 m_rebasedArguments
<< arg
;
193 if (options
& PrintError
) {
196 Logger::error() << "Error, unknown file " << arg
<< endl
;
200 m_rebasedArguments
<< target
;
204 m_rebasedArguments
= args
->arguments().mid(1);
208 void AbstractCommand::pullConfigDataFrom(const AbstractCommand
*other
)
210 m_config
.pullConfigDataFrom(other
->m_config
);
211 m_repository
= other
->m_repository
;
212 m_dryRun
= other
->m_dryRun
;
213 m_rebasedArguments
= other
->m_rebasedArguments
;