Added recursive to Add
[vng.git] / Add.cpp
blobae9c91dd9595422d7e01619f1680b2504c9c9bee
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)
41 CommandLineParser::addOptionDefinitions(options);
42 CommandLineParser::setArgumentDefinition("add <FILE or DIRECTORY>" );
45 AbstractCommand::ReturnCodes Add::run()
47 if (! checkInRepository())
48 return NotInRepo;
49 moveToRoot();
50 CommandLineParser *args = CommandLineParser::instance();
51 const bool recursive = !m_config.contains("not-recursive") && !args->contains("not-recursive");
53 int argIndex = 0;
54 foreach(QString arg, rebasedArguments()) {
55 ++argIndex;
56 QFileInfo path(arg);
57 if (! arg.endsWith('/') && path.isDir())
58 arg = arg + '/';
59 if (!args->contains("boring") && m_excludeMatcher.isExcluded(arg)) {
60 Logger::info() << "Skipping boring file: `" << args->arguments()[argIndex] << "'\n";
61 continue;
63 if (path.exists()) {
64 if (path.isFile()) {
65 ReturnCodes rc = addFile(path, true);
66 if (rc != Ok)
67 return rc;
69 else if (path.isDir()) {
70 if (recursive)
71 recurse(QDir(arg));
73 else if (path.isSymLink())
74 Logger::info() << "Ignoring symbolic link '" << path.filePath() << "'" << endl;
75 else
76 Logger::info() << "Ignoring non-file object '" << path.filePath() << "'" << endl;
78 else
79 Logger::error() << "Can not add non existing' " << path.filePath() << "'" << endl;
81 return Ok;
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)
99 QProcess checker;
100 QStringList arguments;
101 arguments << "ls-files" << path.filePath();
102 GitRunner runner(checker, arguments);
103 ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
104 if (rc != Ok)
105 return rc;
106 bool shouldAdd = true;
107 char buf[1024];
108 while(true) {
109 qint64 lineLength = Vng::readLine(&checker, buf, sizeof(buf));
110 if (lineLength == -1)
111 break;
112 shouldAdd = false; // lets assume here that the script just doesn't print anything if its not added yet.
113 break;
115 if (! shouldAdd) {
116 if (warn)
117 Logger::info() << "Not adding file, already in the repository: `" << path.filePath() << "'\n";
118 checker.waitForFinished();
119 return Ok;
121 Logger::warn() << "Adding '" << path.filePath() << "'" << endl;
122 if (dryRun())
123 return Ok;
124 QProcess git;
125 arguments.clear();
126 arguments << "update-index" << "--add" << path.filePath();
127 GitRunner runner2(git, arguments);
128 rc = runner2.start(GitRunner::WaitUntilFinished);
129 return rc;
132 void Add::recurse(const QDir &dir)
134 CommandLineParser *args = CommandLineParser::instance();
135 foreach(QFileInfo file, dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot)) {
136 if (!args->contains("boring")) {
137 QString path = file.filePath();
138 if (file.isDir())
139 path += '/';
140 if (m_excludeMatcher.isExcluded(path)) {
141 Logger::info() << "Skipping boring " << (file.isDir() ? "directory": "file") << "`" << file.filePath() << "'\n";
142 continue;
145 if(file.isDir())
146 recurse(QDir(file.filePath()));
147 else if (file.isFile())
148 addFile(file, false);