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