Make sure we don't ignore hidden files.
[vng.git] / src / commands / Add.cpp
blob9e93756f13310addb27e8a938c1a5dcba3b93bc1
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"
25 #include "../hunks/File.h"
27 #include <QFileInfo>
28 #include <QProcess>
29 #include <QDebug>
31 static const CommandLineOption options[] = {
32 {"-r, --recursive", "recursively add files"},
33 {"--not-recursive", "do not add files in directories recursively"},
34 {"-f, --boring", "don't skip boring files"},
35 CommandLineLastOption
38 Add::Add()
39 : AbstractCommand("add"),
40 m_excludeMatcher(m_config),
41 m_argumentLength(0),
42 m_fetchedTree(false)
44 CommandLineParser::addOptionDefinitions(options);
45 CommandLineParser::setArgumentDefinition("add <FILE or DIRECTORY>" );
48 AbstractCommand::ReturnCodes Add::run()
50 if (! checkInRepository())
51 return NotInRepo;
52 moveToRoot();
53 CommandLineParser *args = CommandLineParser::instance();
54 const bool recursive = !m_config.contains(QLatin1String("not-recursive")) && !args->contains(QLatin1String("not-recursive"));
56 int argIndex = 0;
57 foreach(QString arg, rebasedArguments()) {
58 ++argIndex;
59 QFileInfo path(arg);
60 if (! arg.endsWith(QLatin1Char('/')) && path.isDir())
61 arg = arg + QLatin1Char('/');
62 if (!args->contains(QLatin1String("boring")) && m_excludeMatcher.isExcluded(arg)) {
63 Logger::warn() << "Skipping boring file: `" << args->arguments()[argIndex] << "'\n";
64 continue;
66 if (path.exists()) {
67 if (path.isFile()) {
68 ReturnCodes rc = addFile(path, true);
69 if (rc != Ok)
70 return rc;
72 else if (path.isDir()) {
73 if (recursive)
74 recurse(QDir(arg));
76 else if (path.isSymLink())
77 Logger::warn() << "Ignoring symbolic link '" << path.filePath() << "'" << endl;
78 else
79 Logger::warn() << "Ignoring non-file object '" << path.filePath() << "'" << endl;
81 else
82 Logger::error() << "Can not add non existing' " << path.filePath() << "'" << endl;
84 return flushAdds();
87 QString Add::argumentDescription() const
89 return QLatin1String("<FILE or DIRECTORY>");
92 QString Add::commandDescription() const
94 return QLatin1String("Add needs to be called whenever you add a new file or directory to your\n"
95 "project. Of course, it also needs to be called when you first create the\n"
96 "project, to let vng know which files should be kept track of.\n");
99 AbstractCommand::ReturnCodes Add::addFile(const QFileInfo &path, bool warn)
101 if (! m_config.isEmptyRepo()) {
102 if (! m_fetchedTree) {
103 foreach(File file, Commit::allFiles()) {
104 m_fileTree << QString::fromUtf8(file.fileName());
106 m_fetchedTree = true;
109 bool shouldAdd = !m_fileTree.contains(path.filePath());
110 if (! shouldAdd) {
111 (warn ? Logger::warn() : Logger::info()) << "Not adding file, already in the repository: `" << path.filePath() << "'\n";
112 return Ok;
115 Logger::info() << "Adding '" << path.filePath() << "'" << endl;
116 if (dryRun())
117 return Ok;
118 QString filePath = path.filePath();
119 m_filesToAdd << filePath;
120 m_argumentLength += filePath.size();
121 if (m_argumentLength > 30000)
122 return flushAdds();
123 return Ok;
126 AbstractCommand::ReturnCodes Add::flushAdds()
128 if (m_argumentLength == 0)
129 return Ok;
130 QProcess git;
131 QStringList arguments;
132 arguments << QLatin1String("update-index") << QLatin1String("--add");
133 arguments += m_filesToAdd;
134 GitRunner runner(git, arguments);
135 ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
136 if (rc == Ok) {
137 m_filesToAdd.clear();
138 m_argumentLength = 0;
140 return rc;
143 void Add::recurse(const QDir &dir)
145 CommandLineParser *args = CommandLineParser::instance();
146 foreach(QFileInfo file, dir.entryInfoList(QDir::Files | QDir::Dirs)) {
147 QString name = file.fileName();
148 if (file.isDir() && (name == QLatin1String(".") || name == QLatin1String("..")))
149 continue;
150 if (!args->contains(QLatin1String("boring"))) {
151 QString path = file.filePath();
152 if (file.isDir())
153 path += QLatin1Char('/');
154 if (m_excludeMatcher.isExcluded(path)) {
155 Logger::info() << "Skipping boring " << (file.isDir() ? "directory": "file") << "`" << file.filePath() << "'\n";
156 continue;
159 if(file.isDir())
160 recurse(QDir(file.filePath()));
161 else if (file.isFile())
162 addFile(file, false);