Move the code to allow diffs to be fetched multithreaded
[vng.git] / VngCommandLine.cpp
blob88e6e78f9fcf45d1f825f7eaf4e8d15e18bfba9a
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/Branches.h"
26 #include "commands/Changes.h"
27 #include "commands/Initialize.h"
28 #include "commands/Move.h"
29 #include "commands/Push.h"
30 #include "commands/Record.h"
31 #include "commands/Remove.h"
32 #include "commands/Revert.h"
33 #include "commands/UnRevert.h"
34 #include "commands/UnRecord.h"
35 #include "commands/WhatsNew.h"
37 #include <QTextStream>
39 #define VERSION "vng version 0.25 (>2008-05-02)"
41 VngCommandLine::VngCommandLine(int argc, char **argv)
42 : QCoreApplication(argc, argv)
44 CommandLineParser::init(argc, argv);
45 if (argc > 1)
46 m_command = QString(argv[1]);
49 VngCommandLine::~VngCommandLine()
51 delete CommandLineParser::instance();
54 int VngCommandLine::run()
56 if (m_command == "--extended-help") {
57 printExtendedHelp();
58 return 0;
60 if (m_command == "--version" || m_command == "-v"
61 #if defined(Q_OS_WIN)
62 || m_command == "/V"
63 #endif
64 ) {
65 QTextStream out(stdout);
66 out << VERSION << endl;
67 return 0;
69 if (m_command.isEmpty()) {
70 printHelp();
71 return 0;
74 if (m_command == "--commands") { // for commandline completion.
75 Configuration config("");
76 QTextStream out(stdout);
77 out << "help" << endl;
78 out << "--help" << endl;
79 out << "--version" << endl;
80 out << "--extended--help" << endl;
81 if (QDir(config.repository().absolutePath() + "/.git").exists()) {
82 out << "add" << endl;
83 out << "branches" << endl;
84 out << "changes" << endl;
85 out << "move" << endl;
86 out << "mv" << endl;
87 out << "push" << endl;
88 out << "record" << endl;
89 out << "remove" << endl;
90 out << "revert" << endl;
91 out << "rm" << endl;
92 out << "unrecord" << endl;
93 out << "unrevert" << endl;
94 out << "whatsnew" << endl;
96 return 0;
99 // autoComplete command
100 QStringList commands;
101 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
102 commands << "add";
103 commands << "branches";
104 commands << "changes";
105 commands << "help";
106 commands << "initialize";
107 commands << "log";
108 commands << "move";
109 commands << "mv";
110 commands << "push";
111 commands << "record";
112 commands << "remove";
113 commands << "revert";
114 commands << "rm";
115 commands << "unrecord";
116 commands << "unrevert";
117 commands << "whatsnew";
119 QString command;
120 foreach(QString s, commands) {
121 if (m_command == s) { // perfect match
122 command = m_command;
123 break;
125 if (! command.isEmpty()) {
126 if (s.startsWith(m_command)) {
127 QTextStream out(stdout);
128 out << "vng failed: " << "Ambiguous command..." << endl;
129 return -1;
131 else
132 break; // found best match.
134 if (s.startsWith(m_command)) {
135 command = s;
136 continue;
139 if (command.isEmpty()) {
140 if (! m_command.isEmpty()) {
141 QTextStream out(stdout);
142 out << "vng failed: " << "Invalid command `" << m_command << "'\n\n";
144 printHelp();
145 return 0;
147 m_command = command;
149 AbstractCommand *ac = 0;
150 if (m_command == "whatsnew")
151 ac = new WhatsNew();
152 else if (m_command == "add")
153 ac = new Add();
154 else if (m_command == "branches")
155 ac = new Branches();
156 else if (m_command == "changes" || m_command == "log")
157 ac = new Changes();
158 else if (m_command == "initialize")
159 ac = new Initialize();
160 else if (m_command == "move" || m_command == "mv")
161 ac = new Move();
162 else if (m_command == "push")
163 ac = new Push();
164 else if (m_command == "remove" || m_command == "rm")
165 ac = new Remove();
166 else if (m_command == "revert")
167 ac = new Revert();
168 else if (m_command == "unrecord")
169 ac = new UnRecord();
170 else if (m_command == "unrevert")
171 ac = new UnRevert();
172 else if (command == "help") {
173 printHelp();
174 return 0;
176 else if (m_command == "record")
177 ac = new Record();
179 Q_ASSERT(ac);
180 return ac->start();
183 void VngCommandLine::printHelp()
185 QTextStream out(stdout);
186 out << "Usage: vng COMMAND ...\n\n" << VERSION << endl;
187 out << "Commands:" << endl;
188 out << " help Display help for vng or a single commands." << endl;
189 out << "Changing and querying the working copy:" << endl;
190 out << " add Add one or more new files or directories." << endl;
191 out << " remove Remove one or more files or directories from the repository." << endl;
192 out << " mv Move/rename one or more files or directories." << endl;
193 out << " revert Revert to the recorded version (safe the first time only)." << endl;
194 out << " unrevert Undo the last revert (may fail if changes after the revert)." << endl;
195 out << " whatsnew Display unrecorded changes in the working copy." << endl;
196 out << "Copying changes between the working copy and the repository:" << endl;
197 out << " record Save changes in the working copy to the repository as a patch." << endl;
198 out << " unrecord Remove recorded patches without changing the working copy." << endl;
199 out << "Copying patches between repositories with working copy update:" << endl;
200 out << " push Copy and apply patches from this repository to another one." << endl;
201 out << "Querying the repository:" << endl;
202 // out << " diff Create a diff between two versions of the repository." << endl;
203 // out << " branches " << endl;
204 out << " changes Gives a changelog-style summary of the repository history." << endl;
205 out << "Administrating repositories:" << endl;
206 out << " initialize Initialize a new source tree as a vng repository." << endl;
207 out << endl;
208 out << "Use 'vng --extended-help' for more detailed help." << endl;
209 out << "Use 'vng COMMAND --help' for help on a single command." << endl;
210 out << "Use 'vng --version' to see the vng version number." << endl;
213 void VngCommandLine::printExtendedHelp()
215 QTextStream out(stdout);
216 out << VERSION << endl;
217 out << "Usage: vng COMMAND ...\n\n";
218 out << "Extended Help:\n\n";
219 out << "A vng repository consists of:\n\n";
220 out << " - a set of PATCHES\n";
221 out << " - a WORKING directory\n\n";
222 out << "Here is a description of which of these components is altered by each\n";
223 out << "command, and how it is used or altered:\n\n";
224 out << " whatsnew Show the differences between WORKING and the \"recorded\"\n";
225 out << " version, that is, the result of applying all PATCHES in the\n";
226 out << " repository. This difference, we will call LOCAL CHANGES.\n\n";
227 out << " record Add a patch to PATCHES representing the LOCAL CHANGES.\n\n";
228 out << " unrecord Delete a patch from PATCHES, but *do not* alter WORKING.\n\n";
229 out << " revert Remove LOCAL CHANGES. Note that this command is interactive,\n";
230 out << " so you can use it to revert only some of these changes.\n\n";
231 out << " unrevert Undo the last revert operation.\n\n";
232 // out << " unpull Delete a patch from PATCHES and unapply it from WORKING.\n";
233 // out << " Note that this command works for any patch, not just one that\n";
234 // out << " was previously \"pull\"ed. If there are no LOCAL CHANGES,\n";
235 // out << " this command is equivalent to \"vng unrecord; vng revert\"\n\n";
236 // out << " rollback Create the inverse of a particular patch and add it to PATCHES,\n";
237 // out << " but DO NOT apply it to WORKING. Note that this command is the\n";
238 // out << " only way to wind up with a patch in PATCHES which has not been\n";
239 // out << " applied to WORKING.\n\n";
243 // a C main function
244 int main(int argc, char **argv)
246 VngCommandLine vng(argc, argv);
247 return vng.run();