Be memory efficient and clear hunk data after displaying it
[vng.git] / commands / Add.cpp
blobb23146d679d4f2ae7be3c93d6f7945229c4493ef
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/>.
20 #include "Add.h"
21 #include "CommandLineParser.h"
22 #include "Logger.h"
23 #include "GitRunner.h"
24 #include "Vng.h"
26 #include <QFileInfo>
27 #include <QProcess>
28 #include <QDebug>
30 static const CommandLineOption options[] = {
31 {"-r, --recursive", "recursively add files"},
32 {"--not-recursive", "do not add files in directories recursively"},
33 {"-f, --boring", "don't skip boring files"},
34 CommandLineLastOption
37 Add::Add()
38 : AbstractCommand("add"),
39 m_excludeMatcher(m_config),
40 m_argumentLength(0)
42 CommandLineParser::addOptionDefinitions(options);
43 CommandLineParser::setArgumentDefinition("add <FILE or DIRECTORY>" );
46 AbstractCommand::ReturnCodes Add::run()
48 if (! checkInRepository())
49 return NotInRepo;
50 moveToRoot();
51 CommandLineParser *args = CommandLineParser::instance();
52 const bool recursive = !m_config.contains("not-recursive") && !args->contains("not-recursive");
54 int argIndex = 0;
55 foreach(QString arg, rebasedArguments()) {
56 ++argIndex;
57 QFileInfo path(arg);
58 if (! arg.endsWith('/') && path.isDir())
59 arg = arg + '/';
60 if (!args->contains("boring") && m_excludeMatcher.isExcluded(arg)) {
61 Logger::warn() << "Skipping boring file: `" << args->arguments()[argIndex] << "'\n";
62 continue;
64 if (path.exists()) {
65 if (path.isFile()) {
66 ReturnCodes rc = addFile(path, true);
67 if (rc != Ok)
68 return rc;
70 else if (path.isDir()) {
71 if (recursive)
72 recurse(QDir(arg));
74 else if (path.isSymLink())
75 Logger::warn() << "Ignoring symbolic link '" << path.filePath() << "'" << endl;
76 else
77 Logger::warn() << "Ignoring non-file object '" << path.filePath() << "'" << endl;
79 else
80 Logger::error() << "Can not add non existing' " << path.filePath() << "'" << endl;
82 return flushAdds();
85 QString Add::argumentDescription() const
87 return "<FILE or DIRECTORY>";
90 QString Add::commandDescription() const
92 return "Add needs to be called whenever you add a new file or directory to your\n"
93 "project. Of course, it also needs to be called when you first create the\n"
94 "project, to let vng know which files should be kept track of.\n";
97 AbstractCommand::ReturnCodes Add::addFile(const QFileInfo &path, bool warn)
99 if (! m_config.isEmptyRepo()) {
100 QProcess checker;
101 QStringList arguments;
102 arguments << "ls-files" << path.filePath();
103 GitRunner runner(checker, arguments);
104 ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
105 if (rc != Ok)
106 return rc;
107 bool shouldAdd = true;
108 char buf[1024];
109 while(true) {
110 qint64 lineLength = Vng::readLine(&checker, buf, sizeof(buf));
111 if (lineLength == -1)
112 break;
113 shouldAdd = false; // lets assume here that the script just doesn't print anything if its not added yet.
114 break;
116 if (! shouldAdd) {
117 (warn ? Logger::warn() : Logger::info()) << "Not adding file, already in the repository: `" << path.filePath() << "'\n";
118 checker.waitForFinished();
119 return Ok;
122 Logger::info() << "Adding '" << path.filePath() << "'" << endl;
123 if (dryRun())
124 return Ok;
125 QString filePath = path.filePath();
126 m_filesToAdd << filePath;
127 m_argumentLength += filePath.size();
128 if (m_argumentLength > 20000)
129 return flushAdds();
130 return Ok;
133 AbstractCommand::ReturnCodes Add::flushAdds()
135 if (m_argumentLength == 0)
136 return Ok;
137 QProcess git;
138 QStringList arguments;
139 arguments << "update-index" << "--add";
140 arguments += m_filesToAdd;
141 m_filesToAdd.clear();
142 m_argumentLength = 0;
143 GitRunner runner(git, arguments);
144 ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
145 return rc;
148 void Add::recurse(const QDir &dir)
150 CommandLineParser *args = CommandLineParser::instance();
151 foreach(QFileInfo file, dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot)) {
152 if (!args->contains("boring")) {
153 QString path = file.filePath();
154 if (file.isDir())
155 path += '/';
156 if (m_excludeMatcher.isExcluded(path)) {
157 Logger::info() << "Skipping boring " << (file.isDir() ? "directory": "file") << "`" << file.filePath() << "'\n";
158 continue;
161 if(file.isDir())
162 recurse(QDir(file.filePath()));
163 else if (file.isFile())
164 addFile(file, false);