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"
23 #include "GitRunner.h"
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"},
38 : AbstractCommand("add"),
39 m_excludeMatcher(m_config
),
42 CommandLineParser::addOptionDefinitions(options
);
43 CommandLineParser::setArgumentDefinition("add <FILE or DIRECTORY>" );
46 AbstractCommand::ReturnCodes
Add::run()
48 if (! checkInRepository())
51 CommandLineParser
*args
= CommandLineParser::instance();
52 const bool recursive
= !m_config
.contains("not-recursive") && !args
->contains("not-recursive");
55 foreach(QString arg
, rebasedArguments()) {
58 if (! arg
.endsWith('/') && path
.isDir())
60 if (!args
->contains("boring") && m_excludeMatcher
.isExcluded(arg
)) {
61 Logger::warn() << "Skipping boring file: `" << args
->arguments()[argIndex
] << "'\n";
66 ReturnCodes rc
= addFile(path
, true);
70 else if (path
.isDir()) {
74 else if (path
.isSymLink())
75 Logger::warn() << "Ignoring symbolic link '" << path
.filePath() << "'" << endl
;
77 Logger::warn() << "Ignoring non-file object '" << path
.filePath() << "'" << endl
;
80 Logger::error() << "Can not add non existing' " << path
.filePath() << "'" << endl
;
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()) {
101 QStringList arguments
;
102 arguments
<< "ls-files" << path
.filePath();
103 GitRunner
runner(checker
, arguments
);
104 ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
107 bool shouldAdd
= true;
110 qint64 lineLength
= Vng::readLine(&checker
, buf
, sizeof(buf
));
111 if (lineLength
== -1)
113 shouldAdd
= false; // lets assume here that the script just doesn't print anything if its not added yet.
117 (warn
? Logger::warn() : Logger::info()) << "Not adding file, already in the repository: `" << path
.filePath() << "'\n";
118 checker
.waitForFinished();
122 Logger::info() << "Adding '" << path
.filePath() << "'" << endl
;
125 QString filePath
= path
.filePath();
126 m_filesToAdd
<< filePath
;
127 m_argumentLength
+= filePath
.size();
128 if (m_argumentLength
> 20000)
133 AbstractCommand::ReturnCodes
Add::flushAdds()
135 if (m_argumentLength
== 0)
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
);
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();
156 if (m_excludeMatcher
.isExcluded(path
)) {
157 Logger::info() << "Skipping boring " << (file
.isDir() ? "directory": "file") << "`" << file
.filePath() << "'\n";
162 recurse(QDir(file
.filePath()));
163 else if (file
.isFile())
164 addFile(file
, false);