Add a 'diff' command that basically does the same as WhatsNew -u
[vng.git] / src / VngCommandLine.cpp
blob95ad1bb6f94ada48cfb6c44807addab41f6c0b58
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/Diff.h"
29 #include "commands/Dist.h"
30 #include "commands/Initialize.h"
31 #include "commands/Move.h"
32 #include "commands/Push.h"
33 #include "commands/Record.h"
34 #include "commands/Remove.h"
35 #include "commands/Revert.h"
36 #include "commands/UnRecord.h"
37 #include "commands/UnRevert.h"
38 #include "commands/WhatsNew.h"
40 #include <QTextStream>
42 #define VERSION "vng version 0.27 (>2008-10-10)"
44 VngCommandLine::VngCommandLine(int argc, char **argv)
45 : QCoreApplication(argc, argv)
47 if (argc > 1)
48 m_command = QString(argv[1]);
49 if (m_command == "help" && argc > 2) { // 'help foo' -> 'foo -h'
50 argv[1] = argv[2];
51 argv[2] = new char[3];// this is a memory leak, do we care?
52 argv[2][0] = '-';
53 argv[2][1] = 'h';
54 argv[2][2] = 0;
55 m_command = QString(argv[1]);
58 CommandLineParser::init(argc, argv);
61 VngCommandLine::~VngCommandLine()
63 delete CommandLineParser::instance();
66 int VngCommandLine::run()
68 if (m_command == "--extended-help") {
69 printExtendedHelp();
70 return 0;
72 if (m_command == "--version" || m_command == "-v"
73 #if defined(Q_OS_WIN)
74 || m_command == "/V"
75 #endif
76 ) {
77 QTextStream out(stdout);
78 out << VERSION << endl;
79 return 0;
81 if (m_command.isEmpty()) {
82 printHelp();
83 return 0;
86 if (m_command == "--commands") { // for commandline completion.
87 Configuration config("");
88 QTextStream out(stdout);
89 out << "help" << endl;
90 out << "initialize" << endl;
91 out << "--help" << endl;
92 out << "--version" << endl;
93 out << "--extended--help" << endl;
94 if (QDir(config.repository().absolutePath() + "/.git").exists()) {
95 out << "add" << endl;
96 out << "amend-record" << endl;
97 out << "branches" << endl;
98 out << "changes" << endl;
99 out << "diff" << endl;
100 out << "dist" << endl;
101 out << "log" << endl;
102 out << "move" << endl;
103 out << "mv" << endl;
104 out << "push" << endl;
105 out << "record" << endl;
106 out << "remove" << endl;
107 out << "revert" << endl;
108 out << "rm" << endl;
109 out << "unrecord" << endl;
110 out << "unrevert" << endl;
111 out << "whatsnew" << endl;
113 return 0;
116 // autoComplete command
117 QStringList commands;
118 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
119 commands << "add";
120 commands << "amend-record";
121 commands << "branches";
122 commands << "changes";
123 commands << "diff";
124 commands << "dist";
125 commands << "help";
126 commands << "initialize";
127 commands << "log";
128 commands << "move";
129 commands << "mv";
130 commands << "push";
131 commands << "record";
132 commands << "remove";
133 commands << "revert";
134 commands << "rm";
135 commands << "unrecord";
136 commands << "unrevert";
137 commands << "whatsnew";
139 QString command;
140 foreach(QString s, commands) {
141 if (m_command == s) { // perfect match
142 command = m_command;
143 break;
145 if (! command.isEmpty()) {
146 if (s.startsWith(m_command)) {
147 QTextStream out(stdout);
148 out << "vng failed: " << "Ambiguous command..." << endl;
149 return -1;
151 else
152 break; // found best match.
154 if (s.startsWith(m_command)) {
155 command = s;
156 continue;
159 if (command.isEmpty()) {
160 if (! m_command.isEmpty()) {
161 QTextStream out(stdout);
162 out << "vng failed: " << "Invalid command `" << m_command << "'\n\n";
164 printHelp();
165 return 0;
167 m_command = command;
169 AbstractCommand *ac = 0;
170 if (m_command == "whatsnew")
171 ac = new WhatsNew();
172 else if (m_command == "add")
173 ac = new Add();
174 else if (m_command == "amend-record")
175 ac = new AmendRecord();
176 else if (m_command == "branches")
177 ac = new Branches();
178 else if (m_command == "changes" || m_command == "log")
179 ac = new Changes();
180 else if (m_command == "initialize")
181 ac = new Initialize();
182 else if (m_command == "move" || m_command == "mv")
183 ac = new Move();
184 else if (m_command == "push")
185 ac = new Push();
186 else if (m_command == "remove" || m_command == "rm")
187 ac = new Remove();
188 else if (m_command == "revert")
189 ac = new Revert();
190 else if (m_command == "unrecord")
191 ac = new UnRecord();
192 else if (m_command == "unrevert")
193 ac = new UnRevert();
194 else if (m_command == "diff")
195 ac = new Diff();
196 else if (m_command == "dist")
197 ac = new Dist();
198 else if (command == "help") {
199 printHelp();
200 return 0;
202 else if (m_command == "record")
203 ac = new Record();
205 Q_ASSERT(ac);
206 return ac->start();
209 void VngCommandLine::printHelp()
211 QTextStream out(stdout);
212 out << "Usage: vng COMMAND ...\n\n" << VERSION << endl;
213 out << "Commands:" << endl;
214 out << " help Display help for vng or a single commands." << endl;
215 out << "Changing and querying the working copy:" << endl;
216 Configuration config("");
217 out << " add Add one or more new files or directories." << endl;
218 out << " remove Remove one or more files or directories from the repository." << endl;
219 out << " mv Move/rename one or more files or directories." << endl;
220 out << " revert Revert to the recorded version (safe the first time only)." << endl;
221 out << " unrevert Undo the last revert (may fail if changes after the revert)." << endl;
222 out << " whatsnew Display unrecorded changes in the working copy." << endl;
223 out << "Copying changes between the working copy and the repository:" << endl;
224 out << " record Save changes in the working copy to the repository as a patch." << endl;
225 out << " unrecord Remove recorded patches without changing the working copy." << endl;
226 out << " amend-record Replace a patch with a better version before it leaves your repository." << endl;
227 out << "Copying patches between repositories with working copy update:" << endl;
228 out << " push Copy and apply patches from this repository to another one." << endl;
229 out << "Querying the repository:" << endl;
230 out << " diff Create a diff between two versions of the repository." << endl;
231 // out << " branches " << endl;
232 out << " dist Create a distribution tarball." << endl;
233 out << " changes Gives a changelog-style summary of the repository history." << endl;
234 out << "Administrating repositories:" << endl;
235 out << " initialize Initialize a new source tree as a vng repository." << endl;
236 out << endl;
237 out << "Use 'vng --extended-help' for more detailed help." << endl;
238 out << "Use 'vng COMMAND --help' for help on a single command." << endl;
239 out << "Use 'vng --version' to see the vng version number." << endl;
242 void VngCommandLine::printExtendedHelp()
244 QTextStream out(stdout);
245 out << VERSION << endl;
246 out << "Usage: vng COMMAND ...\n\n";
247 out << "Extended Help:\n\n";
248 out << "A vng repository consists of:\n\n";
249 out << " - a set of PATCHES\n";
250 out << " - a WORKING directory\n\n";
251 out << "Here is a description of which of these components is altered by each\n";
252 out << "command, and how it is used or altered:\n\n";
253 out << " whatsnew Show the differences between WORKING and the \"recorded\"\n";
254 out << " version, that is, the result of applying all PATCHES in the\n";
255 out << " repository. This difference, we will call LOCAL CHANGES.\n\n";
256 out << " record Add a patch to PATCHES representing the LOCAL CHANGES.\n\n";
257 out << " unrecord Delete a patch from PATCHES, but *do not* alter WORKING.\n\n";
258 out << " revert Remove LOCAL CHANGES. Note that this command is interactive,\n";
259 out << " so you can use it to revert only some of these changes.\n\n";
260 out << " unrevert Undo the last revert operation.\n\n";
261 // out << " unpull Delete a patch from PATCHES and unapply it from WORKING.\n";
262 // out << " Note that this command works for any patch, not just one that\n";
263 // out << " was previously \"pull\"ed. If there are no LOCAL CHANGES,\n";
264 // out << " this command is equivalent to \"vng unrecord; vng revert\"\n\n";
265 // out << " rollback Create the inverse of a particular patch and add it to PATCHES,\n";
266 // out << " but DO NOT apply it to WORKING. Note that this command is the\n";
267 // out << " only way to wind up with a patch in PATCHES which has not been\n";
268 // out << " applied to WORKING.\n\n";
272 // a C main function
273 int main(int argc, char **argv)
275 VngCommandLine vng(argc, argv);
276 return vng.run();