Add CommitsCursor::setUserMatcher(bool use); and make UnRecord use it.
[vng.git] / AbstractCommand.cpp
blob8d06d80bb92c9d2e6bb1cb35f6636fd3a5e47cd9
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/>.
19 #include "AbstractCommand.h"
20 #include "CommandLineParser.h"
21 #include "Logger.h"
23 #include <QDebug>
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"},
36 CommandLineLastOption
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;
73 return Disabled;
75 if (m_config.contains("disable")) {
76 Logger::info() << "vng failed: Command `" << m_name << "' disabled by configuration!" << endl;
77 return Disabled;
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;
95 return Ok;
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;
116 else
117 m_repository = m_config.repository();
118 return run();
121 QDir AbstractCommand::repository() const
123 return m_repository;
126 bool AbstractCommand::dryRun() const
128 return m_dryRun;
131 QString AbstractCommand::name() const
133 return m_name;
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";
146 return false;
148 return true;
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())
163 return;
164 CommandLineParser *args = CommandLineParser::instance();
165 if (currentDir != newRepoDir && currentDir.startsWith(newRepoDir)) {
166 QString diff = currentDir.mid(newRepoDir.length() + 1);
167 // calcuate rebased paths.
168 bool first = true;
169 foreach (QString arg, args->arguments()) {
170 if (first) {
171 first = false;
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;
176 else
177 m_rebasedArguments << diff +'/'+ arg;
180 else
181 m_rebasedArguments = args->arguments().mid(1);