Move Matcher to its own class+file CommitsMatcher
[vng.git] / Changes.cpp
blob92515626760b86558ff1965632dd230ea0ac9883
1 /*
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/>.
19 #include "Changes.h"
20 #include "CommandLineParser.h"
21 #include "GitRunner.h"
22 #include "Logger.h"
23 #include "Vng.h"
24 #include "hunks/ChangeSet.h"
25 #include "commits/Commit.h"
26 #include "commits/CommitsMatcher.h"
28 #include <QDebug>
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"},
48 CommandLineLastOption
51 Changes::Changes()
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())
71 return NotInRepo;
72 CommandLineParser *args = CommandLineParser::instance();
74 QProcess git;
75 QStringList arguments;
76 arguments << "whatchanged" << "-m" << "--pretty=raw";
78 if (args->contains("last"))
79 arguments << "-n" << args->optionArgument("last");
81 QList<int> usedArguments;
82 usedArguments << 0;
83 foreach (Branch branch, m_config.allBranches()) {
84 QString branchName = branch.branchName();
85 if (branchName.endsWith("/HEAD"))
86 continue;
87 bool first = true;
88 int index = -1;
89 foreach (QString arg, args->arguments()) {
90 index++;
91 if (first) {
92 first = false; // skip command, args for this command are next.
93 continue;
95 if (branchName.endsWith(arg) && branchName[branchName.length() - arg.length() - 1] == '/') {
96 arguments << branchName;
97 usedArguments << index;
98 break;
103 // now we have to use the rest of the arguments the user passed.
104 int index = 0;
105 foreach (QString arg, args->arguments()) {
106 if (! usedArguments.contains(index++))
107 arguments << arg;
110 GitRunner runner(git, arguments);
111 ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
112 if (rc) {
113 // TODO proper reporting
114 return rc;
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;
122 while(true) {
123 Commit commit = Commit::createFromStream(&git);
124 if (! commit.isValid())
125 break;
126 switch(matcher.match(commit)) {
127 case CommitsMatcher::SkipPatch: continue;
128 case CommitsMatcher::ShowPatch: break;
129 case CommitsMatcher::Exit:
130 Logger::stopPager();
131 git.waitForFinished();
132 return Ok;
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) {
142 out << endl;
143 ChangeSet cs = commit.changeSet();
144 cs.generateHunks();
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();
150 else
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();
158 out << endl;
159 if (Logger::verbosity() >= Logger::Verbose)
160 file.outputWhatsChanged(out, m_config, false, false);
163 out << endl;
164 Logger::flushPager();
166 Logger::stopPager();
167 git.waitForFinished();
168 return Ok;