]> andersk Git - svn-all-fast-export.git/blame - src/repository.cpp
Support Qt 4.3 too
[svn-all-fast-export.git] / src / repository.cpp
CommitLineData
5a7327f6
TM
1/*
2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#include "repository.h"
29b44c19 19#include "options.h"
1c4df5aa 20#include <QTextStream>
b6ba9639 21#include <QDebug>
374d4cd6
TM
22#include <QLinkedList>
23
24static const int maxSimultaneousProcesses = 100;
25
26class ProcessCache: QLinkedList<Repository *>
27{
28public:
29 void touch(Repository *repo)
30 {
31 remove(repo);
32
33 // if the cache is too big, remove from the front
34 while (size() >= maxSimultaneousProcesses)
35 takeFirst()->closeFastImport();
36
37 // append to the end
38 append(repo);
39 }
40
41 inline void remove(Repository *repo)
42 {
7abbe81c 43#if QT_VERSION >= 0x040400
374d4cd6 44 removeOne(repo);
7abbe81c
TM
45#else
46 removeAll(repo);
47#endif
374d4cd6
TM
48 }
49};
50static ProcessCache processCache;
5a7327f6
TM
51
52Repository::Repository(const Rules::Repository &rule)
8b6c8bd6 53 : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false)
5a7327f6
TM
54{
55 foreach (Rules::Repository::Branch branchRule, rule.branches) {
56 Branch branch;
e087596c 57 branch.created = 0; // not created
5a7327f6
TM
58
59 branches.insert(branchRule.name, branch);
60 }
61
b6ba9639 62 // create the default branch
e087596c 63 branches["master"].created = 1;
b6ba9639 64
1a688729 65 fastImport.setWorkingDirectory(name);
5a7327f6 66}
1c4df5aa
TM
67
68Repository::~Repository()
374d4cd6 69{
8b6c8bd6 70 Q_ASSERT(outstandingTransactions == 0);
374d4cd6
TM
71 closeFastImport();
72}
73
74void Repository::closeFastImport()
1c4df5aa 75{
3ffa3592 76 if (fastImport.state() != QProcess::NotRunning) {
ca192b2c
TM
77 fastImport.write("checkpoint\n");
78 fastImport.waitForBytesWritten(-1);
1c4df5aa 79 fastImport.closeWriteChannel();
ca192b2c
TM
80 if (!fastImport.waitForFinished()) {
81 fastImport.terminate();
82 if (!fastImport.waitForFinished(200))
83 qWarning() << "git-fast-import for repository" << name << "did not die";
84 }
1c4df5aa 85 }
374d4cd6
TM
86 processHasStarted = false;
87 processCache.remove(this);
1c4df5aa
TM
88}
89
1a688729
TM
90void Repository::reloadBranches()
91{
c0a3eea3
TM
92 QProcess revParse;
93 revParse.setWorkingDirectory(name);
94 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
25e6c28e 95 revParse.waitForFinished(-1);
1a688729 96
c0a3eea3 97 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
c0a3eea3 98 while (revParse.canReadLine()) {
b3b433a8 99 QByteArray branchName = revParse.readLine().trimmed();
1a688729 100
25e6c28e 101 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
c0a3eea3
TM
102 branches[branchName].created = 1;
103 fastImport.write("reset refs/heads/" + branchName +
25e6c28e
TM
104 "\nfrom refs/heads/" + branchName + "^0\n\n"
105 "progress Branch refs/heads/" + branchName + " reloaded\n");
1a688729
TM
106 }
107 }
108}
109
c338ae37
TM
110void Repository::createBranch(const QString &branch, int revnum,
111 const QString &branchFrom, int)
112{
25e6c28e 113 startFastImport();
c338ae37 114 if (!branches.contains(branch)) {
7dd430d8
TM
115 qWarning() << branch << "is not a known branch in repository" << name << endl
116 << "Going to create it automatically";
c338ae37
TM
117 }
118
c338ae37
TM
119 QByteArray branchRef = branch.toUtf8();
120 if (!branchRef.startsWith("refs/"))
121 branchRef.prepend("refs/heads/");
122
123 Branch &br = branches[branch];
e087596c 124 if (br.created && br.created != revnum) {
c338ae37
TM
125 QByteArray backupBranch = branchRef + '_' + QByteArray::number(revnum);
126 qWarning() << branch << "already exists; backing up to" << backupBranch;
127
128 fastImport.write("reset " + backupBranch + "\nfrom " + branchRef + "\n\n");
129 }
130
131 // now create the branch
e087596c 132 br.created = revnum;
c338ae37
TM
133 QByteArray branchFromRef = branchFrom.toUtf8();
134 if (!branchFromRef.startsWith("refs/"))
135 branchFromRef.prepend("refs/heads/");
136
7dd430d8
TM
137 if (!branches.contains(branchFrom) || !branches.value(branchFrom).created) {
138 qCritical() << branch << "in repository" << name
139 << "is branching from branch" << branchFrom
140 << "but the latter doesn't exist. Can't continue.";
141 exit(1);
142 }
143
25e6c28e
TM
144 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
145 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
c338ae37
TM
146}
147
1c4df5aa
TM
148Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
149 int revnum)
150{
25e6c28e 151 startFastImport();
b6ba9639 152 if (!branches.contains(branch)) {
ff26f180
TM
153 qWarning() << branch << "is not a known branch in repository" << name << endl
154 << "Going to create it automatically";
b6ba9639 155 }
1c4df5aa
TM
156
157 Transaction *txn = new Transaction;
158 txn->repository = this;
159 txn->branch = branch.toUtf8();
160 txn->svnprefix = svnprefix.toUtf8();
161 txn->datetime = 0;
162 txn->revnum = revnum;
1c4df5aa 163
a812d0b1 164 if ((++commitCount % 10000) == 0)
b50b9285
TM
165 // write everything to disk every 10000 commits
166 fastImport.write("checkpoint\n");
fc7b4bcc 167 if (outstandingTransactions++ == 0)
8b6c8bd6 168 lastmark = 1; // reset the mark number
1a688729
TM
169 return txn;
170}
171
172void Repository::startFastImport()
173{
688d69ec 174 if (fastImport.state() == QProcess::NotRunning) {
298d7968
TM
175 if (processHasStarted)
176 qFatal("git-fast-import has been started once and crashed?");
177 processHasStarted = true;
178
1c4df5aa 179 // start the process
298d7968
TM
180 QString outputFile = name;
181 outputFile.replace('/', '_');
182 outputFile.prepend("log-");
183 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
f34275cf 184 fastImport.setProcessChannelMode(QProcess::MergedChannels);
298d7968 185
688d69ec 186#ifndef DRY_RUN
25e6c28e 187 fastImport.start("git", QStringList() << "fast-import");
688d69ec
TM
188#else
189 fastImport.start("/bin/cat", QStringList());
190#endif
25e6c28e
TM
191
192 reloadBranches();
688d69ec 193 }
1c4df5aa
TM
194}
195
196Repository::Transaction::~Transaction()
197{
8b6c8bd6 198 --repository->outstandingTransactions;
1c4df5aa
TM
199}
200
201void Repository::Transaction::setAuthor(const QByteArray &a)
202{
203 author = a;
204}
205
206void Repository::Transaction::setDateTime(uint dt)
207{
208 datetime = dt;
209}
210
211void Repository::Transaction::setLog(const QByteArray &l)
212{
213 log = l;
214}
215
216void Repository::Transaction::deleteFile(const QString &path)
217{
218 deletedFiles.append(path);
219}
220
221QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
222{
8b6c8bd6 223 int mark = ++repository->lastmark;
e3f0499d
TM
224
225 if (modifiedFiles.capacity() == 0)
226 modifiedFiles.reserve(2048);
227 modifiedFiles.append("M ");
228 modifiedFiles.append(QByteArray::number(mode, 8));
229 modifiedFiles.append(" :");
230 modifiedFiles.append(QByteArray::number(mark));
231 modifiedFiles.append(' ');
232 modifiedFiles.append(path.toUtf8());
233 modifiedFiles.append("\n");
1c4df5aa 234
688d69ec 235#ifndef DRY_RUN
1c4df5aa 236 repository->fastImport.write("blob\nmark :");
e3f0499d 237 repository->fastImport.write(QByteArray::number(mark));
1c4df5aa
TM
238 repository->fastImport.write("\ndata ");
239 repository->fastImport.write(QByteArray::number(length));
240 repository->fastImport.write("\n", 1);
688d69ec 241#endif
1c4df5aa 242
1c4df5aa
TM
243 return &repository->fastImport;
244}
245
246void Repository::Transaction::commit()
247{
374d4cd6
TM
248 processCache.touch(repository);
249
1c4df5aa
TM
250 // create the commit message
251 QByteArray message = log;
252 if (!message.endsWith('\n'))
253 message += '\n';
29b44c19
AK
254 if (Options::globalOptions->switches.value("metadata", true))
255 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
1c4df5aa
TM
256
257 {
258 QByteArray branchRef = branch;
8d7b49e5 259 if (!branchRef.startsWith("refs/"))
1c4df5aa
TM
260 branchRef.prepend("refs/heads/");
261
262 QTextStream s(&repository->fastImport);
263 s << "commit " << branchRef << endl;
f34275cf 264 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
1c4df5aa
TM
265
266 Branch &br = repository->branches[branch];
bdc20860 267 if (!br.created) {
72da4610 268 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
bdc20860
TM
269 << revnum << "-- did you resume from the wrong revision?";
270 br.created = revnum;
271 }
1c4df5aa
TM
272
273 s << "data " << message.length() << endl;
274 }
275
276 repository->fastImport.write(message);
6b510c52 277 repository->fastImport.putChar('\n');
1c4df5aa
TM
278
279 // write the file deletions
6b450d1d
TM
280 if (deletedFiles.contains(""))
281 repository->fastImport.write("deleteall\n");
282 else
283 foreach (QString df, deletedFiles)
284 repository->fastImport.write("D " + df.toUtf8() + "\n");
1c4df5aa
TM
285
286 // write the file modifications
e3f0499d 287 repository->fastImport.write(modifiedFiles);
1c4df5aa 288
25e6c28e
TM
289 repository->fastImport.write("\nprogress Commit #" +
290 QByteArray::number(repository->commitCount) +
291 " branch " + branch +
292 " = SVN r" + QByteArray::number(revnum) + "\n\n");
293 printf(" %d modifications to \"%s\"",
294 deletedFiles.count() + modifiedFiles.count(),
295 qPrintable(repository->name));
1c4df5aa 296
25f3205a 297 while (repository->fastImport.bytesToWrite())
50cd32a4 298 if (!repository->fastImport.waitForBytesWritten(-1))
25f3205a 299 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
1c4df5aa 300}
This page took 0.128283 seconds and 5 git commands to generate.