move commands to their own subdir
[vng.git] / Add.cpp
blob40201dee352422e0f4979ff9c6b27dedbff554f1
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::warn() << "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::warn() << "Ignoring symbolic link '" << path.filePath() << "'" << endl;
75 else
76 Logger::warn() << "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 warn ? Logger::warn() : Logger::info() << "Not adding file, already in the repository: `" << path.filePath() << "'\n";
117 checker.waitForFinished();
118 return Ok;
120 Logger::warn() << "Adding '" << path.filePath() << "'" << endl;
121 if (dryRun())
122 return Ok;
123 QProcess git;
124 arguments.clear();
125 arguments << "update-index" << "--add" << path.filePath();
126 GitRunner runner2(git, arguments);
127 rc = runner2.start(GitRunner::WaitUntilFinished);
128 return rc;
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();
137 if (file.isDir())
138 path += '/';
139 if (m_excludeMatcher.isExcluded(path)) {
140 Logger::info() << "Skipping boring " << (file.isDir() ? "directory": "file") << "`" << file.filePath() << "'\n";
141 continue;
144 if(file.isDir())
145 recurse(QDir(file.filePath()));
146 else if (file.isFile())
147 addFile(file, false);