Add 'branches' command to test the fetching of branches in Configuration
[vng.git] / WhatsNew.cpp
bloba1cbd2e8a0cfba9e965dcf724b2da8453c209942
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 "WhatsNew.h"
20 #include "hunks/ChangeSet.h"
21 #include "Logger.h"
22 #include "CommandLineParser.h"
24 #include <QTextStream>
26 static const CommandLineOption options[] = {
27 {"-s, --summary", "summarize changes"},
28 {"--no-summary", "don't summarize changes"},
29 {"-u, --unified", "output patch in a format similar to diff -u"},
30 // TODO :)
31 //{"--ignore-times", "don't trust the file modification times"},
32 //{"-l, --look-for-adds", "In addition to modifications, look for files that are not boring, and thus are potentially pending addition"},
33 //{"--dont-look-for-adds", "Don't look for any files or directories that could be added, and don't add them automatically"},
34 //{"--boring", "don't skip boring files"},
35 CommandLineLastOption
38 WhatsNew::WhatsNew()
39 : AbstractCommand("whatsnew")
41 CommandLineParser::addOptionDefinitions(options);
42 CommandLineParser::setArgumentDefinition("whatsnew [FILE or DIRECTORY]" );
45 AbstractCommand::ReturnCodes WhatsNew::run()
47 if (! checkInRepository())
48 return NotInRepo;
49 CommandLineParser *args = CommandLineParser::instance();
50 const bool printSummary = m_config.contains("summary") && !args->contains("no-summary") || args->contains("summary");
51 const bool unified = m_config.contains("unified") || args->contains("unified");
52 if (shouldUsePager())
53 Logger::startPager();
54 ChangeSet changeSet;
55 changeSet.fillFromCurrentChanges(rebasedArguments());
57 QTextStream &out = Logger::standardOut();
58 if (changeSet.count() == 0) {
59 out << "No Changes!" << endl;
60 return Ok;
62 if (!printSummary && !unified) {
63 m_config.colorize(out);
64 out << "{\n";
66 foreach(File file, changeSet.files()) {
67 const bool deleted = file.fileName().isEmpty() && !file.oldFileName().isEmpty();
68 const bool added = !deleted && !file.fileName().isEmpty() && file.oldFileName().isEmpty();
69 const bool renamed = !deleted && !added && file.fileName() != file.oldFileName();
70 if (printSummary) {
71 if (deleted)
72 out <<"D ";
73 else if (added)
74 out <<"A ";
75 else if (renamed)
76 out << "R ";
77 else
78 out << "M ";
80 if (deleted || renamed)
81 out << file.oldFileName();
82 else
83 out << file.fileName();
84 if (renamed)
85 out << " => " << file.fileName();
87 int removed = file.linesRemoved();
88 int added = file.linesAdded();
89 if (removed)
90 out << " -" << QString::number(removed);
91 if (added)
92 out << " +" << QString::number(added);
93 out << endl;
94 continue;
97 if (deleted) { // file deleted.
98 if (!unified) {
99 m_config.colorize(out);
100 out <<"rmfile ";
101 m_config.normalColor(out);
102 out << file.oldFileName() << endl;
105 else if (added) { // file added.
106 if (!unified) {
107 m_config.colorize(out);
108 out <<"addfile ";
109 m_config.normalColor(out);
110 out << file.fileName() << endl;
113 else {
114 if(renamed) { // rename
115 if (!unified) m_config.colorize(out);
116 out <<"move ";
117 if (!unified) m_config.normalColor(out);
118 out << "`" << file.oldFileName() << "' `" << file.fileName() << "'" << endl;
120 if (file.oldProtection() != file.protection()) {
121 if (!unified) m_config.colorize(out);
122 out <<"mode change ";
123 if (!unified) m_config.normalColor(out);
124 out << file.fileName() << " " << file.oldProtection() << " => " << file.protection() << endl;
127 if (file.isBinary()) { // will not have any hunks
128 if (!unified) m_config.colorize(out);
129 out << "binary modification ";
130 if (!unified) m_config.normalColor(out);
131 out << file.fileName() << endl;
132 continue;
134 if (unified) {
135 out << "--- a/";
136 if (file.oldFileName().isEmpty())
137 out << "/dev/null\n";
138 else
139 out << file.oldFileName() << endl;
140 out << "--- b/";
141 if (file.fileName().isEmpty())
142 out << "/dev/null\n";
143 else
144 out << file.fileName() << endl;
146 foreach(Hunk hunk, file.hunks()) {
147 if (unified) {
148 out << QString(hunk.header());
149 out << QString(hunk.patch());
150 continue;
152 for (int i = 0; i < hunk.subHunkCount(); i++) {
153 m_config.colorize(out);
154 out <<"hunk ";
155 QByteArray patch = hunk.subHunk(i);
156 m_config.normalColor(out);
157 out << file.fileName() <<" "<< QString::number(hunk.lineNumber(i)) << endl;
158 if (patch.contains((char) 0)) { // binary
159 m_config.colorize(out);
160 out << "binary data\n";
161 m_config.normalColor(out);
163 else
164 out << QString(patch);
167 Logger::flushPager();
169 if (!printSummary && !unified) {
170 m_config.colorize(out);
171 out << "}\n";
172 m_config.normalColor(out);
175 return AbstractCommand::Ok;
178 QString WhatsNew::argumentDescription() const
180 return QString("[FILE or DIRECTORY]");
183 QString WhatsNew::commandDescription() const
185 return "whatsnew gives you a view of what changes you've made in your working\n"
186 "copy that haven't yet been recorded. The changes are displayed in\n"
187 "vng patch format.\n";