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/>.
21 #include "../CommandLineParser.h"
22 #include "../Logger.h"
23 #include "../GitRunner.h"
25 #include "../hunks/File.h"
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"},
39 : AbstractCommand("add"),
40 m_excludeMatcher(m_config
),
44 CommandLineParser::addOptionDefinitions(options
);
45 CommandLineParser::setArgumentDefinition("add <FILE or DIRECTORY>" );
48 AbstractCommand::ReturnCodes
Add::run()
50 if (! checkInRepository())
53 CommandLineParser
*args
= CommandLineParser::instance();
54 const bool recursive
= !m_config
.contains(QLatin1String("not-recursive")) && !args
->contains(QLatin1String("not-recursive"));
57 foreach(QString arg
, rebasedArguments()) {
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";
68 ReturnCodes rc
= addFile(path
, true);
72 else if (path
.isDir()) {
76 else if (path
.isSymLink())
77 Logger::warn() << "Ignoring symbolic link '" << path
.filePath() << "'" << endl
;
79 Logger::warn() << "Ignoring non-file object '" << path
.filePath() << "'" << endl
;
82 Logger::error() << "Can not add non existing' " << path
.filePath() << "'" << endl
;
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());
111 (warn
? Logger::warn() : Logger::info()) << "Not adding file, already in the repository: `" << path
.filePath() << "'\n";
115 Logger::info() << "Adding '" << path
.filePath() << "'" << endl
;
118 QString filePath
= path
.filePath();
119 m_filesToAdd
<< filePath
;
120 m_argumentLength
+= filePath
.size();
121 if (m_argumentLength
> 30000)
126 AbstractCommand::ReturnCodes
Add::flushAdds()
128 if (m_argumentLength
== 0)
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
);
137 m_filesToAdd
.clear();
138 m_argumentLength
= 0;
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("..")))
150 if (!args
->contains(QLatin1String("boring"))) {
151 QString path
= file
.filePath();
153 path
+= QLatin1Char('/');
154 if (m_excludeMatcher
.isExcluded(path
)) {
155 Logger::info() << "Skipping boring " << (file
.isDir() ? "directory": "file") << "`" << file
.filePath() << "'\n";
160 recurse(QDir(file
.filePath()));
161 else if (file
.isFile())
162 addFile(file
, false);