2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "repository.h"
19 #include <QTextStream>
22 Repository::Repository(const Rules::Repository &rule)
25 foreach (Rules::Repository::Branch branchRule, rule.branches) {
27 branch.branchFrom = branchRule.branchFrom;
28 if (!branch.branchFrom.startsWith("refs/heads/"))
29 branch.branchFrom.prepend("refs/heads/");
30 branch.isCreated = false;
32 branches.insert(branchRule.name, branch);
35 // create the default branch
36 branches["master"].isCreated = true;
38 fastImport.setWorkingDirectory(name);
39 fastImport.setProcessChannelMode(QProcess::ForwardedChannels);
42 Repository::~Repository()
44 if (fastImport.state() != QProcess::NotRunning) {
45 fastImport.closeWriteChannel();
46 fastImport.waitForFinished();
50 void Repository::reloadBranches()
52 QHash<QString, Branch>::Iterator it = branches.begin(),
54 for ( ; it != end; ++it) {
55 QString branchRef = it.key();
56 if (!branchRef.startsWith("refs/heads/"))
57 branchRef.prepend("refs/heads/");
60 // does this branch already exist?
62 revParse.setWorkingDirectory(name);
63 revParse.start("git-rev-parse", QStringList() << "--verify" << branchRef);
64 revParse.waitForFinished();
66 if (revParse.exitCode() == 0)
73 fastImport.write("reset " + branchRef.toUtf8() +
74 "\nfrom " + branchRef.toUtf8() + "^0\n\n");
80 Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
83 if (!branches.contains(branch)) {
84 qCritical() << branch << "is not known in repository" << name;
88 Transaction *txn = new Transaction;
89 txn->repository = this;
90 txn->branch = branch.toUtf8();
91 txn->svnprefix = svnprefix.toUtf8();
94 txn->lastmark = revnum;
100 void Repository::startFastImport()
102 if (fastImport.state() == QProcess::NotRunning) {
105 fastImport.start("git-fast-import", QStringList());
107 fastImport.start("/bin/cat", QStringList());
112 Repository::Transaction::~Transaction()
116 void Repository::Transaction::setAuthor(const QByteArray &a)
121 void Repository::Transaction::setDateTime(uint dt)
126 void Repository::Transaction::setLog(const QByteArray &l)
131 void Repository::Transaction::deleteFile(const QString &path)
133 deletedFiles.append(path);
136 QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
140 fp.mark = ++lastmark;
143 repository->fastImport.write("blob\nmark :");
144 repository->fastImport.write(QByteArray::number(fp.mark));
145 repository->fastImport.write("\ndata ");
146 repository->fastImport.write(QByteArray::number(length));
147 repository->fastImport.write("\n", 1);
148 repository->fastImport.waitForBytesWritten(0);
151 modifiedFiles.insert(path, fp);
152 return &repository->fastImport;
155 void Repository::Transaction::commit()
157 // create the commit message
158 QByteArray message = log;
159 if (!message.endsWith('\n'))
161 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
164 QByteArray branchRef = branch;
165 if (!branchRef.startsWith("refs/heads/"))
166 branchRef.prepend("refs/heads/");
168 QTextStream s(&repository->fastImport);
169 s << "commit " << branchRef << endl;
170 s << "mark :" << revnum << endl;
171 s << "committer " << author << ' ' << datetime << " -0000" << endl;
173 Branch &br = repository->branches[branch];
176 s << "from " << br.branchFrom << endl;
179 s << "data " << message.length() << endl;
182 repository->fastImport.write(message);
184 // write the file deletions
185 foreach (QString df, deletedFiles)
186 repository->fastImport.write("D " + df.toUtf8() + "\n");
188 // write the file modifications
189 QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin();
190 for ( ; it != modifiedFiles.constEnd(); ++it) {
191 repository->fastImport.write("M ", 2);
192 repository->fastImport.write(QByteArray::number(it->mode, 8));
193 repository->fastImport.write(" :", 2);
194 repository->fastImport.write(QByteArray::number(it->mark));
195 repository->fastImport.write(" ", 1);
196 repository->fastImport.write(it.key().toUtf8());
197 repository->fastImport.write("\n", 1);
200 repository->fastImport.write("\n");
202 while (repository->fastImport.bytesToWrite() && repository->fastImport.waitForBytesWritten()) {