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"
30 static const CommandLineOption options
[] = {
31 // {"-a, --all", "answer yes to all patches"},
32 {"--to-match PATTERN", "select changes up to a patch matching PATTERN"},
33 {"--to-patch REGEXP", "select changes up to a patch matching REGEXP"},
34 // {"--to-tag REGEXP", "select changes up to a tag matching REGEXP"},
35 {"--from-match PATTERN", "select changes starting with a patch matching PATTERN"},
36 {"--from-patch REGEXP", "select changes starting with a patch matching REGEXP"},
37 // {"--from-tag REGEXP", "select changes starting with a tag matching REGEXP"},
38 {"-n, --last NUMBER", "select the last NUMBER patches"},
39 {"--match PATTERN", "select patches matching PATTERN"},
40 {"-p, --patches REGEXP", "select patches matching REGEXP"},
41 // {"-t, --tags=REGEXP", "select tags matching REGEXP"},
42 // {"--context", "give output suitable for get --context"},
43 // {"--xml-output", "generate XML formatted output"},
44 // {"--human-readable", "give human-readable output"},
45 {"-s, --summary", "summarize changes"},
46 {"--no-summary", "don't summarize changes"},
47 // {"--reverse", "show changes in reverse order"},
52 : AbstractCommand("changes")
54 CommandLineParser::addOptionDefinitions(options
);
55 CommandLineParser::setArgumentDefinition("changes [FILE or DIRECTORY]" );
58 QString
Changes::argumentDescription() const
60 return "[FILE or DIRECTORY]";
63 QString
Changes::commandDescription() const
65 return "Gives a changelog-style summary of the repository history.\n";
68 AbstractCommand::ReturnCodes
Changes::run()
70 if (! checkInRepository())
72 CommandLineParser
*args
= CommandLineParser::instance();
75 QStringList arguments
;
76 arguments
<< "whatchanged" << "-m" << "--pretty=raw";
78 if (args
->contains("last"))
79 arguments
<< "-n" << args
->optionArgument("last");
81 QList
<int> usedArguments
;
83 foreach (Branch branch
, m_config
.allBranches()) {
84 QString branchName
= branch
.branchName();
85 if (branchName
.endsWith("/HEAD"))
89 foreach (QString arg
, args
->arguments()) {
92 first
= false; // skip command, args for this command are next.
95 if (branchName
== arg
|| branchName
.endsWith(arg
) && branchName
[branchName
.length() - arg
.length() - 1] == '/') {
96 arguments
<< branchName
;
97 usedArguments
<< index
;
103 // now we have to use the rest of the arguments the user passed.
105 foreach (QString arg
, args
->arguments()) {
106 if (! usedArguments
.contains(index
++))
110 GitRunner
runner(git
, arguments
);
111 ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
113 // TODO proper reporting
116 if (shouldUsePager())
117 Logger::startPager();
119 const bool showSummery
= m_config
.contains("summary") && !args
->contains("no-summary") || args
->contains("summary");
120 QTextStream
&out
= Logger::standardOut();
121 CommitsMatcher matcher
;
123 Commit commit
= Commit::createFromStream(&git
);
124 if (! commit
.isValid())
126 switch(matcher
.match(commit
)) {
127 case CommitsMatcher::SkipPatch
: continue;
128 case CommitsMatcher::ShowPatch
: break;
129 case CommitsMatcher::Exit
:
131 git
.waitForFinished();
135 out
<< commit
.commitTime().toString() << " ";
136 m_config
.colorize(out
);
137 out
<< commit
.author() << endl
;
138 m_config
.normalColor(out
);
139 out
<< " ID " << commit
.commitTreeIsm() << endl
;
140 out
<< commit
.logMessage();
141 if (Logger::verbosity() >= Logger::Verbose
|| showSummery
) {
143 ChangeSet cs
= commit
.changeSet();
145 foreach (File file
, cs
.files()) {
146 if (file
.oldFileName().isEmpty())
147 out
<<" A " << file
.fileName();
148 else if (file
.fileName().isEmpty())
149 out
<<" D " << file
.oldFileName();
151 out
<<" M " << file
.fileName();
152 if (Logger::verbosity() < Logger::Verbose
) {
153 if (file
.linesRemoved() > 0)
154 out
<< " -" << file
.linesRemoved();
155 if (file
.linesAdded() > 0)
156 out
<< " +" << file
.linesAdded();
159 if (Logger::verbosity() >= Logger::Verbose
)
160 file
.outputWhatsChanged(out
, m_config
, false, false);
164 Logger::flushPager();
167 git
.waitForFinished();