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"
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
)
48 m_command
= QString(argv
[1]);
49 if (m_command
== "help" && argc
> 2) { // 'help foo' -> 'foo -h'
51 argv
[2] = new char[3];// this is a memory leak, do we care?
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") {
72 if (m_command
== "--version" || m_command
== "-v"
77 QTextStream
out(stdout
);
78 out
<< VERSION
<< endl
;
81 if (m_command
.isEmpty()) {
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()) {
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
;
104 out
<< "push" << endl
;
105 out
<< "record" << endl
;
106 out
<< "remove" << endl
;
107 out
<< "revert" << endl
;
109 out
<< "unrecord" << endl
;
110 out
<< "unrevert" << endl
;
111 out
<< "whatsnew" << endl
;
116 // autoComplete command
117 QStringList commands
;
118 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
120 commands
<< "amend-record";
121 commands
<< "branches";
122 commands
<< "changes";
126 commands
<< "initialize";
131 commands
<< "record";
132 commands
<< "remove";
133 commands
<< "revert";
135 commands
<< "unrecord";
136 commands
<< "unrevert";
137 commands
<< "whatsnew";
140 foreach(QString s
, commands
) {
141 if (m_command
== s
) { // perfect match
145 if (! command
.isEmpty()) {
146 if (s
.startsWith(m_command
)) {
147 QTextStream
out(stdout
);
148 out
<< "vng failed: " << "Ambiguous command..." << endl
;
152 break; // found best match.
154 if (s
.startsWith(m_command
)) {
159 if (command
.isEmpty()) {
160 if (! m_command
.isEmpty()) {
161 QTextStream
out(stdout
);
162 out
<< "vng failed: " << "Invalid command `" << m_command
<< "'\n\n";
169 AbstractCommand
*ac
= 0;
170 if (m_command
== "whatsnew")
172 else if (m_command
== "add")
174 else if (m_command
== "amend-record")
175 ac
= new AmendRecord();
176 else if (m_command
== "branches")
178 else if (m_command
== "changes" || m_command
== "log")
180 else if (m_command
== "initialize")
181 ac
= new Initialize();
182 else if (m_command
== "move" || m_command
== "mv")
184 else if (m_command
== "push")
186 else if (m_command
== "remove" || m_command
== "rm")
188 else if (m_command
== "revert")
190 else if (m_command
== "unrecord")
192 else if (m_command
== "unrevert")
194 else if (m_command
== "diff")
196 else if (m_command
== "dist")
198 else if (command
== "help") {
202 else if (m_command
== "record")
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
;
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";
273 int main(int argc
, char **argv
)
275 VngCommandLine
vng(argc
, argv
);