Don't allow to unrecord past the branch point
[vng.git] / AbstractCommand.cpp
blobd49be18485b5471b75221bf1e62efd7df9089c52
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();
119 QString currentDir = QDir::current().absolutePath();
120 QString newRepoDir = m_repository.absolutePath();
121 QDir::setCurrent(newRepoDir);
122 if (currentDir != newRepoDir && currentDir.startsWith(newRepoDir)) {
123 QString diff = currentDir.mid(newRepoDir.length() + 1);
124 // calcuate rebased paths.
125 bool first = true;
126 foreach (QString arg, args->arguments()) {
127 if (first) {
128 first = false;
129 continue; // first is the command name
131 if (arg == ".") // work around git bug that 'dir/.' doesn't work while 'dir' does work
132 m_rebasedArguments << diff;
133 else
134 m_rebasedArguments << diff +'/'+ arg;
137 else
138 m_rebasedArguments = args->arguments().mid(1);
140 return run();
143 QDir AbstractCommand::repository() const
145 return m_repository;
148 bool AbstractCommand::dryRun() const
150 return m_dryRun;
153 QString AbstractCommand::name() const
155 return m_name;
158 QStringList AbstractCommand::rebasedArguments() const
160 return m_rebasedArguments;
163 bool AbstractCommand::checkInRepository() const
165 if (! QDir(m_repository.absolutePath() + "/.git").exists()) {
166 Logger::error() << "vng failed: Unable to `" << m_name << "' here\n\n";
167 Logger::error() << "You need to be in a repository directory to run this command.\n";
168 return false;
170 return true;
173 bool AbstractCommand::shouldUsePager() const
175 CommandLineParser *args = CommandLineParser::instance();
176 return m_config.contains("use-pager") && !args->contains("no-pager") || args->contains("use-pager");