2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
4 * Copyright (C) 2002-2004 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/>.
20 #include "CommandLineParser.h"
21 #include "GitRunner.h"
24 #include "hunks/ChangeSet.h"
25 #include "commits/Commit.h"
26 #include "commits/CommitsMatcher.h"
29 static const CommandLineOption options
[] = {
30 // {"-a, --all", "answer yes to all patches"},
31 {"--to-match PATTERN", "select changes up to a patch matching PATTERN"},
32 {"--to-patch REGEXP", "select changes up to a patch matching REGEXP"},
33 // {"--to-tag REGEXP", "select changes up to a tag matching REGEXP"},
34 {"--from-match PATTERN", "select changes starting with a patch matching PATTERN"},
35 {"--from-patch REGEXP", "select changes starting with a patch matching REGEXP"},
36 // {"--from-tag REGEXP", "select changes starting with a tag matching REGEXP"},
37 {"-n, --last NUMBER", "select the last NUMBER patches"},
38 {"--match PATTERN", "select patches matching PATTERN"},
39 {"-p, --patches REGEXP", "select patches matching REGEXP"},
40 // {"-t, --tags=REGEXP", "select tags matching REGEXP"},
41 // {"--context", "give output suitable for get --context"},
42 // {"--xml-output", "generate XML formatted output"},
43 // {"--human-readable", "give human-readable output"},
44 {"-s, --summary", "summarize changes"},
45 {"--no-summary", "don't summarize changes"},
46 // {"--reverse", "show changes in reverse order"},
51 : AbstractCommand("changes")
53 CommandLineParser::addOptionDefinitions(options
);
54 CommandLineParser::setArgumentDefinition("changes [FILE or DIRECTORY]" );
57 QString
Changes::argumentDescription() const
59 return "[FILE or DIRECTORY]";
62 QString
Changes::commandDescription() const
64 return "Gives a changelog-style summary of the repository history.\n";
67 AbstractCommand::ReturnCodes
Changes::run()
69 if (! checkInRepository())
71 CommandLineParser
*args
= CommandLineParser::instance();
74 QStringList arguments
;
75 arguments
<< "whatchanged" << "-m" << "--pretty=raw";
77 if (args
->contains("last"))
78 arguments
<< "-n" << args
->optionArgument("last");
80 QList
<int> usedArguments
;
82 foreach (Branch branch
, m_config
.allBranches()) {
83 QString branchName
= branch
.branchName();
84 if (branchName
.endsWith("/HEAD"))
88 foreach (QString arg
, args
->arguments()) {
91 first
= false; // skip command, args for this command are next.
94 if (branchName
== arg
|| branchName
.endsWith(arg
) && branchName
[branchName
.length() - arg
.length() - 1] == '/') {
95 arguments
<< branchName
;
96 usedArguments
<< index
;
102 // now we have to use the rest of the arguments the user passed.
104 QStringList unknownArguments
;
105 foreach (QString arg
, args
->arguments()) {
106 if (! usedArguments
.contains(index
++)) {
108 unknownArguments
<< arg
;
112 GitRunner
runner(git
, arguments
);
113 ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
115 Logger::error() << "Vng failed: Unknown branch or revision: ";
116 foreach(QString arg
, unknownArguments
)
117 Logger::error() << "`" << arg
<< "' ";;
118 Logger::error() << endl
;
121 if (shouldUsePager())
122 Logger::startPager();
124 const bool showSummery
= m_config
.contains("summary") && !args
->contains("no-summary") || args
->contains("summary");
125 QTextStream
&out
= Logger::standardOut();
126 CommitsMatcher matcher
;
128 Commit commit
= Commit::createFromStream(&git
);
129 if (! commit
.isValid())
131 switch(matcher
.match(commit
)) {
132 case CommitsMatcher::SkipPatch
: continue;
133 case CommitsMatcher::ShowPatch
: break;
134 case CommitsMatcher::Exit
:
136 git
.waitForFinished();
140 out
<< commit
.commitTime().toString() << " ";
141 m_config
.colorize(out
);
142 out
<< commit
.author() << endl
;
143 m_config
.normalColor(out
);
144 out
<< " ID " << commit
.commitTreeIsm() << endl
;
145 out
<< commit
.logMessage();
146 if (Logger::verbosity() >= Logger::Verbose
|| showSummery
) {
148 ChangeSet cs
= commit
.changeSet();
150 foreach (File file
, cs
.files()) {
151 if (file
.oldFileName().isEmpty())
152 out
<<" A " << file
.fileName();
153 else if (file
.fileName().isEmpty())
154 out
<<" D " << file
.oldFileName();
156 out
<<" M " << file
.fileName();
157 if (Logger::verbosity() < Logger::Verbose
) {
158 if (file
.linesRemoved() > 0)
159 out
<< " -" << file
.linesRemoved();
160 if (file
.linesAdded() > 0)
161 out
<< " +" << file
.linesAdded();
164 if (Logger::verbosity() >= Logger::Verbose
)
165 file
.outputWhatsChanged(out
, m_config
, false, false);
169 Logger::flushPager();
170 if (! out
.device()->isWritable()) { // output cancelled; lets kill git.
176 git
.waitForFinished();