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)
59 fastImport.start("git-fast-import", QStringList());
64 Repository::Transaction::~Transaction()
68 void Repository::Transaction::setAuthor(const QByteArray &a)
73 void Repository::Transaction::setDateTime(uint dt)
78 void Repository::Transaction::setLog(const QByteArray &l)
83 void Repository::Transaction::deleteFile(const QString &path)
85 deletedFiles.append(path);
88 QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
94 repository->fastImport.write("blob\nmark :");
95 repository->fastImport.write(QByteArray::number(fp.mark));
96 repository->fastImport.write("\ndata ");
97 repository->fastImport.write(QByteArray::number(length));
98 repository->fastImport.write("\n", 1);
99 repository->fastImport.waitForBytesWritten(0);
101 modifiedFiles.insert(path, fp);
102 return &repository->fastImport;
105 void Repository::Transaction::commit()
107 // create the commit message
108 QByteArray message = log;
109 if (!message.endsWith('\n'))
111 message += "\nsvn=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
114 QByteArray branchRef = branch;
115 if (!branchRef.startsWith("refs/heads/"))
116 branchRef.prepend("refs/heads/");
118 QTextStream s(&repository->fastImport);
119 s << "commit " << branchRef << endl;
120 s << "mark :" << revnum << endl;
121 s << "committer " << author << ' ' << datetime << "-0000" << endl;
123 Branch &br = repository->branches[branch];
126 s << "from " << br.branchFrom << endl;
129 s << "data " << message.length() << endl;
132 repository->fastImport.write(message);
134 // write the file deletions
135 foreach (QString df, deletedFiles)
136 repository->fastImport.write("D " + df.toUtf8() + "\n");
138 // write the file modifications
139 QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin();
140 for ( ; it != modifiedFiles.constEnd(); ++it) {
141 repository->fastImport.write("M ", 2);
142 repository->fastImport.write(QByteArray::number(it->mode, 8));
143 repository->fastImport.write(" :", 2);
144 repository->fastImport.write(QByteArray::number(it->mark));
145 repository->fastImport.write(" ", 1);
146 repository->fastImport.write(it.key().toUtf8());
147 repository->fastImport.write("\n", 1);
150 repository->fastImport.write("\n");
152 while (repository->fastImport.bytesToWrite() && repository->fastImport.waitForBytesWritten()) {