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
)
41 CommandLineParser::addOptionDefinitions(options
);
42 CommandLineParser::setArgumentDefinition("add <FILE or DIRECTORY>" );
45 AbstractCommand::ReturnCodes
Add::run()
47 if (! checkInRepository())
50 CommandLineParser
*args
= CommandLineParser::instance();
51 const bool recursive
= !m_config
.contains("not-recursive") && !args
->contains("not-recursive");
54 foreach(QString arg
, rebasedArguments()) {
57 if (! arg
.endsWith('/') && path
.isDir())
59 if (!args
->contains("boring") && m_excludeMatcher
.isExcluded(arg
)) {
60 Logger::warn() << "Skipping boring file: `" << args
->arguments()[argIndex
] << "'\n";
65 ReturnCodes rc
= addFile(path
, true);
69 else if (path
.isDir()) {
73 else if (path
.isSymLink())
74 Logger::warn() << "Ignoring symbolic link '" << path
.filePath() << "'" << endl
;
76 Logger::warn() << "Ignoring non-file object '" << path
.filePath() << "'" << endl
;
79 Logger::error() << "Can not add non existing' " << path
.filePath() << "'" << endl
;
84 QString
Add::argumentDescription() const
86 return "<FILE or DIRECTORY>";
89 QString
Add::commandDescription() const
91 return "Add needs to be called whenever you add a new file or directory to your\n"
92 "project. Of course, it also needs to be called when you first create the\n"
93 "project, to let vng know which files should be kept track of.\n";
96 AbstractCommand::ReturnCodes
Add::addFile(const QFileInfo
&path
, bool warn
)
100 QStringList arguments
;
101 arguments
<< "ls-files" << path
.filePath();
102 GitRunner
runner(checker
, arguments
);
103 ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
106 bool shouldAdd
= true;
109 qint64 lineLength
= Vng::readLine(&checker
, buf
, sizeof(buf
));
110 if (lineLength
== -1)
112 shouldAdd
= false; // lets assume here that the script just doesn't print anything if its not added yet.
116 warn
? Logger::warn() : Logger::info() << "Not adding file, already in the repository: `" << path
.filePath() << "'\n";
117 checker
.waitForFinished();
120 Logger::warn() << "Adding '" << path
.filePath() << "'" << endl
;
125 arguments
<< "update-index" << "--add" << path
.filePath();
126 GitRunner
runner2(git
, arguments
);
127 rc
= runner2
.start(GitRunner::WaitUntilFinished
);
131 void Add::recurse(const QDir
&dir
)
133 CommandLineParser
*args
= CommandLineParser::instance();
134 foreach(QFileInfo file
, dir
.entryInfoList(QDir::Files
| QDir::Dirs
| QDir::NoDotAndDotDot
)) {
135 if (!args
->contains("boring")) {
136 QString path
= file
.filePath();
139 if (m_excludeMatcher
.isExcluded(path
)) {
140 Logger::info() << "Skipping boring " << (file
.isDir() ? "directory": "file") << "`" << file
.filePath() << "'\n";
145 recurse(QDir(file
.filePath()));
146 else if (file
.isFile())
147 addFile(file
, false);