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>
21 Repository::Repository(const Rules::Repository &rule)
23 foreach (Rules::Repository::Branch branchRule, rule.branches) {
25 branch.branchFrom = branchRule.branchFrom;
26 branch.isCreated = false;
28 branches.insert(branchRule.name, branch);
31 fastImport.setWorkingDirectory(rule.name);
32 fastImport.setProcessChannelMode(QProcess::ForwardedChannels);
35 Repository::~Repository()
37 if (fastImport.state() == QProcess::Running) {
38 fastImport.closeWriteChannel();
39 fastImport.waitForFinished();
43 Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
46 if (!branches.contains(branch))
49 Transaction *txn = new Transaction;
50 txn->repository = this;
51 txn->branch = branch.toUtf8();
52 txn->svnprefix = svnprefix.toUtf8();
55 txn->lastmark = revnum;
57 if (fastImport.state() == QProcess::NotRunning) {
60 fastImport.start("git-fast-import", QStringList());
62 fastImport.start("/bin/cat", QStringList());
69 Repository::Transaction::~Transaction()
73 void Repository::Transaction::setAuthor(const QByteArray &a)
78 void Repository::Transaction::setDateTime(uint dt)
83 void Repository::Transaction::setLog(const QByteArray &l)
88 void Repository::Transaction::deleteFile(const QString &path)
90 deletedFiles.append(path);
93 QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
100 repository->fastImport.write("blob\nmark :");
101 repository->fastImport.write(QByteArray::number(fp.mark));
102 repository->fastImport.write("\ndata ");
103 repository->fastImport.write(QByteArray::number(length));
104 repository->fastImport.write("\n", 1);
105 repository->fastImport.waitForBytesWritten(0);
108 modifiedFiles.insert(path, fp);
109 return &repository->fastImport;
112 void Repository::Transaction::commit()
114 // create the commit message
115 QByteArray message = log;
116 if (!message.endsWith('\n'))
118 message += "\nsvn=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
121 QByteArray branchRef = branch;
122 if (!branchRef.startsWith("refs/heads/"))
123 branchRef.prepend("refs/heads/");
125 QTextStream s(&repository->fastImport);
126 s << "commit " << branchRef << endl;
127 s << "mark :" << revnum << endl;
128 s << "committer " << author << ' ' << datetime << "-0000" << endl;
130 Branch &br = repository->branches[branch];
133 s << "from " << br.branchFrom << endl;
136 s << "data " << message.length() << endl;
139 repository->fastImport.write(message);
141 // write the file deletions
142 foreach (QString df, deletedFiles)
143 repository->fastImport.write("D " + df.toUtf8() + "\n");
145 // write the file modifications
146 QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin();
147 for ( ; it != modifiedFiles.constEnd(); ++it) {
148 repository->fastImport.write("M ", 2);
149 repository->fastImport.write(QByteArray::number(it->mode, 8));
150 repository->fastImport.write(" :", 2);
151 repository->fastImport.write(QByteArray::number(it->mark));
152 repository->fastImport.write(" ", 1);
153 repository->fastImport.write(it.key().toUtf8());
154 repository->fastImport.write("\n", 1);
157 repository->fastImport.write("\n");
159 while (repository->fastImport.bytesToWrite() && repository->fastImport.waitForBytesWritten()) {