Add a 'diff' command that basically does the same as WhatsNew -u
[vng.git] / src / commands / WhatsNew.cpp
blob65fc8c62fd59883899fbd86ede90cbcd22cba781
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 "GitRunner.h"
23 #include "CommandLineParser.h"
25 #include <QTextStream>
26 #include <QProcess>
28 static const CommandLineOption options[] = {
29 {"-s, --summary", "summarize changes"},
30 {"--no-summary", "don't summarize changes"},
31 {"-u, --unified", "output patch in a format similar to diff -u"},
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"),
40 m_excludeMatcher(m_config)
42 CommandLineParser::addOptionDefinitions(options);
43 CommandLineParser::setArgumentDefinition("whatsnew [FILE or DIRECTORY]" );
46 AbstractCommand::ReturnCodes WhatsNew::run()
48 if (! checkInRepository())
49 return NotInRepo;
50 const bool unified = m_config.contains("unified") || CommandLineParser::instance()->contains("unified");
51 return printWhatsNew(unified, true);
54 AbstractCommand::ReturnCodes WhatsNew::printWhatsNew(bool unified, bool chatty)
56 moveToRoot( static_cast <RebaseOptions> (CheckFileSystem | PrintError) ); // XXX why is the static cast needed :(
58 CommandLineParser *args = CommandLineParser::instance();
59 const bool lookForAdds = (m_config.contains("look-for-adds") && !args->contains("dont-look-for-adds-summary"))
60 || args->contains("look-for-adds");
61 const bool printSummary = lookForAdds
62 || (m_config.contains("summary") && !args->contains("no-summary"))
63 || args->contains("summary");
64 if (shouldUsePager())
65 Logger::startPager();
66 ChangeSet changeSet;
67 changeSet.fillFromCurrentChanges(rebasedArguments());
68 changeSet.waitForFinishFirstFile();
70 QTextStream &out = Logger::standardOut();
71 if (lookForAdds) {
72 QProcess git;
73 QStringList arguments;
74 arguments << "ls-files" << "-o" << "--directory" << "--no-empty-directory" << "--exclude-standard";
76 GitRunner runner(git, arguments);
77 ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
78 if (rc != Ok)
79 return rc;
81 char buf[1024];
82 while(true) {
83 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
84 if (lineLength == -1)
85 break;
86 QString line = QString::fromUtf8(buf, lineLength);
87 if (!m_excludeMatcher.isExcluded(line))
88 out << "a " << line;
91 else if (changeSet.count() == 0) {
92 if (chatty)
93 out << "No Changes!";
94 out << endl;
95 return Ok;
97 if (!printSummary && !unified) {
98 m_config.colorize(out);
99 out << "{\n";
101 for (int i=0; i < changeSet.count(); ++i ) {
102 File file = changeSet.file(i);
103 file.outputWhatsChanged(out, m_config, printSummary, unified);
104 file.cleanHunksData();
105 Logger::flushPager();
107 if (!printSummary && !unified) {
108 m_config.colorize(out);
109 out << "}\n";
110 m_config.normalColor(out);
113 return AbstractCommand::Ok;
116 QString WhatsNew::argumentDescription() const
118 return QString("[FILE or DIRECTORY]");
121 QString WhatsNew::commandDescription() const
123 return "Whatsnew gives you a view of what changes you've made in your working\n"
124 "copy that haven't yet been recorded. The changes are displayed in\n"
125 "vng patch format.\n";