Extend the commits cursor to have a selection type and thus make
[vng.git] / src / VngCommandLine.cpp
blob704675893d60d7f563022e1f3a77395c58a49f7f
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
4 * Copyright (C) 2002-2005 David Roundy
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "VngCommandLine.h"
20 #include "CommandLineParser.h"
21 #include "Logger.h"
23 // commands
24 #include "commands/Add.h"
25 #include "commands/AmendRecord.h"
26 #include "commands/Branches.h"
27 #include "commands/Changes.h"
28 #include "commands/Dist.h"
29 #include "commands/Initialize.h"
30 #include "commands/Move.h"
31 #include "commands/Push.h"
32 #include "commands/Record.h"
33 #include "commands/Remove.h"
34 #include "commands/Revert.h"
35 #include "commands/UnRecord.h"
36 #include "commands/UnRevert.h"
37 #include "commands/WhatsNew.h"
39 #include <QTextStream>
41 #define VERSION "vng version 0.26 (>2008-05-16)"
43 VngCommandLine::VngCommandLine(int argc, char **argv)
44 : QCoreApplication(argc, argv)
46 if (argc > 1)
47 m_command = QString(argv[1]);
48 if (m_command == "help" && argc > 2) { // 'help foo' -> 'foo -h'
49 argv[1] = argv[2];
50 argv[2] = new char[3];// this is a memory leak, do we care?
51 argv[2][0] = '-';
52 argv[2][1] = 'h';
53 argv[2][2] = 0;
54 m_command = QString(argv[1]);
57 CommandLineParser::init(argc, argv);
60 VngCommandLine::~VngCommandLine()
62 delete CommandLineParser::instance();
65 int VngCommandLine::run()
67 if (m_command == "--extended-help") {
68 printExtendedHelp();
69 return 0;
71 if (m_command == "--version" || m_command == "-v"
72 #if defined(Q_OS_WIN)
73 || m_command == "/V"
74 #endif
75 ) {
76 QTextStream out(stdout);
77 out << VERSION << endl;
78 return 0;
80 if (m_command.isEmpty()) {
81 printHelp();
82 return 0;
85 if (m_command == "--commands") { // for commandline completion.
86 Configuration config("");
87 QTextStream out(stdout);
88 out << "help" << endl;
89 out << "initialize" << endl;
90 out << "--help" << endl;
91 out << "--version" << endl;
92 out << "--extended--help" << endl;
93 if (QDir(config.repository().absolutePath() + "/.git").exists()) {
94 out << "add" << endl;
95 out << "amend-record" << endl;
96 out << "branches" << endl;
97 out << "changes" << endl;
98 out << "dist" << endl;
99 out << "log" << endl;
100 out << "move" << endl;
101 out << "mv" << endl;
102 out << "push" << endl;
103 out << "record" << endl;
104 out << "remove" << endl;
105 out << "revert" << endl;
106 out << "rm" << endl;
107 out << "unrecord" << endl;
108 out << "unrevert" << endl;
109 out << "whatsnew" << endl;
111 return 0;
114 // autoComplete command
115 QStringList commands;
116 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
117 commands << "add";
118 commands << "amend-record";
119 commands << "branches";
120 commands << "changes";
121 commands << "dist";
122 commands << "help";
123 commands << "initialize";
124 commands << "log";
125 commands << "move";
126 commands << "mv";
127 commands << "push";
128 commands << "record";
129 commands << "remove";
130 commands << "revert";
131 commands << "rm";
132 commands << "unrecord";
133 commands << "unrevert";
134 commands << "whatsnew";
136 QString command;
137 foreach(QString s, commands) {
138 if (m_command == s) { // perfect match
139 command = m_command;
140 break;
142 if (! command.isEmpty()) {
143 if (s.startsWith(m_command)) {
144 QTextStream out(stdout);
145 out << "vng failed: " << "Ambiguous command..." << endl;
146 return -1;
148 else
149 break; // found best match.
151 if (s.startsWith(m_command)) {
152 command = s;
153 continue;
156 if (command.isEmpty()) {
157 if (! m_command.isEmpty()) {
158 QTextStream out(stdout);
159 out << "vng failed: " << "Invalid command `" << m_command << "'\n\n";
161 printHelp();
162 return 0;
164 m_command = command;
166 AbstractCommand *ac = 0;
167 if (m_command == "whatsnew")
168 ac = new WhatsNew();
169 else if (m_command == "add")
170 ac = new Add();
171 else if (m_command == "amend-record")
172 ac = new AmendRecord();
173 else if (m_command == "branches")
174 ac = new Branches();
175 else if (m_command == "changes" || m_command == "log")
176 ac = new Changes();
177 else if (m_command == "initialize")
178 ac = new Initialize();
179 else if (m_command == "move" || m_command == "mv")
180 ac = new Move();
181 else if (m_command == "push")
182 ac = new Push();
183 else if (m_command == "remove" || m_command == "rm")
184 ac = new Remove();
185 else if (m_command == "revert")
186 ac = new Revert();
187 else if (m_command == "unrecord")
188 ac = new UnRecord();
189 else if (m_command == "unrevert")
190 ac = new UnRevert();
191 else if (m_command == "dist")
192 ac = new Dist();
193 else if (command == "help") {
194 printHelp();
195 return 0;
197 else if (m_command == "record")
198 ac = new Record();
200 Q_ASSERT(ac);
201 return ac->start();
204 void VngCommandLine::printHelp()
206 QTextStream out(stdout);
207 out << "Usage: vng COMMAND ...\n\n" << VERSION << endl;
208 out << "Commands:" << endl;
209 out << " help Display help for vng or a single commands." << endl;
210 out << "Changing and querying the working copy:" << endl;
211 Configuration config("");
212 out << " add Add one or more new files or directories." << endl;
213 out << " remove Remove one or more files or directories from the repository." << endl;
214 out << " mv Move/rename one or more files or directories." << endl;
215 out << " revert Revert to the recorded version (safe the first time only)." << endl;
216 out << " unrevert Undo the last revert (may fail if changes after the revert)." << endl;
217 out << " whatsnew Display unrecorded changes in the working copy." << endl;
218 out << "Copying changes between the working copy and the repository:" << endl;
219 out << " record Save changes in the working copy to the repository as a patch." << endl;
220 out << " unrecord Remove recorded patches without changing the working copy." << endl;
221 out << " amend-record Replace a patch with a better version before it leaves your repository." << endl;
222 out << "Copying patches between repositories with working copy update:" << endl;
223 out << " push Copy and apply patches from this repository to another one." << endl;
224 out << "Querying the repository:" << endl;
225 // out << " diff Create a diff between two versions of the repository." << endl;
226 // out << " branches " << endl;
227 out << " dist Create a distribution tarball." << endl;
228 out << " changes Gives a changelog-style summary of the repository history." << endl;
229 out << "Administrating repositories:" << endl;
230 out << " initialize Initialize a new source tree as a vng repository." << endl;
231 out << endl;
232 out << "Use 'vng --extended-help' for more detailed help." << endl;
233 out << "Use 'vng COMMAND --help' for help on a single command." << endl;
234 out << "Use 'vng --version' to see the vng version number." << endl;
237 void VngCommandLine::printExtendedHelp()
239 QTextStream out(stdout);
240 out << VERSION << endl;
241 out << "Usage: vng COMMAND ...\n\n";
242 out << "Extended Help:\n\n";
243 out << "A vng repository consists of:\n\n";
244 out << " - a set of PATCHES\n";
245 out << " - a WORKING directory\n\n";
246 out << "Here is a description of which of these components is altered by each\n";
247 out << "command, and how it is used or altered:\n\n";
248 out << " whatsnew Show the differences between WORKING and the \"recorded\"\n";
249 out << " version, that is, the result of applying all PATCHES in the\n";
250 out << " repository. This difference, we will call LOCAL CHANGES.\n\n";
251 out << " record Add a patch to PATCHES representing the LOCAL CHANGES.\n\n";
252 out << " unrecord Delete a patch from PATCHES, but *do not* alter WORKING.\n\n";
253 out << " revert Remove LOCAL CHANGES. Note that this command is interactive,\n";
254 out << " so you can use it to revert only some of these changes.\n\n";
255 out << " unrevert Undo the last revert operation.\n\n";
256 // out << " unpull Delete a patch from PATCHES and unapply it from WORKING.\n";
257 // out << " Note that this command works for any patch, not just one that\n";
258 // out << " was previously \"pull\"ed. If there are no LOCAL CHANGES,\n";
259 // out << " this command is equivalent to \"vng unrecord; vng revert\"\n\n";
260 // out << " rollback Create the inverse of a particular patch and add it to PATCHES,\n";
261 // out << " but DO NOT apply it to WORKING. Note that this command is the\n";
262 // out << " only way to wind up with a patch in PATCHES which has not been\n";
263 // out << " applied to WORKING.\n\n";
267 // a C main function
268 int main(int argc, char **argv)
270 VngCommandLine vng(argc, argv);
271 return vng.run();