]> andersk Git - svn-all-fast-export.git/blame_incremental - src/repository.cpp
Add support for annotated tags
[svn-all-fast-export.git] / src / repository.cpp
... / ...
CommitLineData
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"
19#include "options.h"
20#include <QTextStream>
21#include <QDebug>
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 {
43#if QT_VERSION >= 0x040400
44 removeOne(repo);
45#else
46 removeAll(repo);
47#endif
48 }
49};
50static ProcessCache processCache;
51
52Repository::Repository(const Rules::Repository &rule)
53 : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false)
54{
55 foreach (Rules::Repository::Branch branchRule, rule.branches) {
56 Branch branch;
57 branch.created = 0; // not created
58
59 branches.insert(branchRule.name, branch);
60 }
61
62 // create the default branch
63 branches["master"].created = 1;
64
65 fastImport.setWorkingDirectory(name);
66}
67
68Repository::~Repository()
69{
70 Q_ASSERT(outstandingTransactions == 0);
71 closeFastImport();
72}
73
74void Repository::closeFastImport()
75{
76 if (fastImport.state() != QProcess::NotRunning) {
77 fastImport.write("checkpoint\n");
78 fastImport.waitForBytesWritten(-1);
79 fastImport.closeWriteChannel();
80 if (!fastImport.waitForFinished()) {
81 fastImport.terminate();
82 if (!fastImport.waitForFinished(200))
83 qWarning() << "git-fast-import for repository" << name << "did not die";
84 }
85 }
86 processHasStarted = false;
87 processCache.remove(this);
88}
89
90void Repository::reloadBranches()
91{
92 QProcess revParse;
93 revParse.setWorkingDirectory(name);
94 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
95 revParse.waitForFinished(-1);
96
97 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
98 while (revParse.canReadLine()) {
99 QByteArray branchName = revParse.readLine().trimmed();
100
101 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
102 branches[branchName].created = 1;
103 fastImport.write("reset refs/heads/" + branchName +
104 "\nfrom refs/heads/" + branchName + "^0\n\n"
105 "progress Branch refs/heads/" + branchName + " reloaded\n");
106 }
107 }
108}
109
110void Repository::createBranch(const QString &branch, int revnum,
111 const QString &branchFrom, int)
112{
113 startFastImport();
114 if (!branches.contains(branch)) {
115 qWarning() << branch << "is not a known branch in repository" << name << endl
116 << "Going to create it automatically";
117 }
118
119 QByteArray branchRef = branch.toUtf8();
120 if (!branchRef.startsWith("refs/"))
121 branchRef.prepend("refs/heads/");
122
123 Branch &br = branches[branch];
124 if (br.created && br.created != revnum) {
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
132 br.created = revnum;
133 QByteArray branchFromRef = branchFrom.toUtf8();
134 if (!branchFromRef.startsWith("refs/"))
135 branchFromRef.prepend("refs/heads/");
136
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
144 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
145 "progress Branch " + branchRef + " created from "
146 + branchFromRef + " r" + QByteArray::number(revnum) + "\n\n");
147}
148
149Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
150 int revnum)
151{
152 startFastImport();
153 if (!branches.contains(branch)) {
154 qWarning() << branch << "is not a known branch in repository" << name << endl
155 << "Going to create it automatically";
156 }
157
158 Transaction *txn = new Transaction;
159 txn->repository = this;
160 txn->branch = branch.toUtf8();
161 txn->svnprefix = svnprefix.toUtf8();
162 txn->datetime = 0;
163 txn->revnum = revnum;
164
165 if ((++commitCount % 10000) == 0)
166 // write everything to disk every 10000 commits
167 fastImport.write("checkpoint\n");
168 if (outstandingTransactions++ == 0)
169 lastmark = 1; // reset the mark number
170 return txn;
171}
172
173void Repository::createAnnotatedTag(const QString &ref, const QString &svnprefix,
174 int revnum,
175 const QByteArray &author, uint dt,
176 const QByteArray &log)
177{
178 QString tagName = ref;
179 if (tagName.startsWith("refs/tags/"))
180 tagName.remove(0, 10);
181
182 if (!annotatedTags.contains(tagName))
183 printf("Creating annotated tag %s (%s)\n", qPrintable(tagName), qPrintable(ref));
184 else
185 printf("Re-creating annotated tag %s\n", qPrintable(tagName));
186
187 AnnotatedTag &tag = annotatedTags[tagName];
188 tag.supportingRef = ref;
189 tag.svnprefix = svnprefix.toUtf8();
190 tag.revnum = revnum;
191 tag.author = author;
192 tag.log = log;
193 tag.dt = dt;
194}
195
196void Repository::finalizeTags()
197{
198 if (annotatedTags.isEmpty())
199 return;
200
201 printf("Finalising tags for %s...", qPrintable(name));
202 startFastImport();
203
204 QHash<QString, AnnotatedTag>::ConstIterator it = annotatedTags.constBegin();
205 for ( ; it != annotatedTags.constEnd(); ++it) {
206 const QString &tagName = it.key();
207 const AnnotatedTag &tag = it.value();
208
209 QByteArray message = tag.log;
210 if (!message.endsWith('\n'))
211 message += '\n';
212 if (Options::globalOptions->switches.value("metadata", true))
213 message += "\nsvn path=" + tag.svnprefix + "; revision=" + QByteArray::number(tag.revnum) + "\n";
214
215 {
216 QByteArray branchRef = tag.supportingRef.toUtf8();
217 if (!branchRef.startsWith("refs/"))
218 branchRef.prepend("refs/heads/");
219
220 QTextStream s(&fastImport);
221 s << "progress Creating annotated tag " << tagName << " from ref " << branchRef << endl
222 << "tag " << tagName << endl
223 << "from " << branchRef << endl
224 << "tagger " << QString::fromUtf8(tag.author) << ' ' << tag.dt << " -0000" << endl
225 << "data " << message.length() << endl;
226 }
227
228 fastImport.write(message);
229 fastImport.putChar('\n');
230 if (!fastImport.waitForBytesWritten(-1))
231 qFatal("Failed to write to process: %s", qPrintable(fastImport.errorString()));
232
233 printf(" %s", qPrintable(tagName));
234 fflush(stdout);
235 }
236
237 while (fastImport.bytesToWrite())
238 if (!fastImport.waitForBytesWritten(-1))
239 qFatal("Failed to write to process: %s", qPrintable(fastImport.errorString()));
240 printf("\n");
241}
242
243void Repository::startFastImport()
244{
245 if (fastImport.state() == QProcess::NotRunning) {
246 if (processHasStarted)
247 qFatal("git-fast-import has been started once and crashed?");
248 processHasStarted = true;
249
250 // start the process
251 QString outputFile = name;
252 outputFile.replace('/', '_');
253 outputFile.prepend("log-");
254 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
255 fastImport.setProcessChannelMode(QProcess::MergedChannels);
256
257#ifndef DRY_RUN
258 fastImport.start("git", QStringList() << "fast-import");
259#else
260 fastImport.start("/bin/cat", QStringList());
261#endif
262
263 reloadBranches();
264 }
265}
266
267Repository::Transaction::~Transaction()
268{
269 --repository->outstandingTransactions;
270}
271
272void Repository::Transaction::setAuthor(const QByteArray &a)
273{
274 author = a;
275}
276
277void Repository::Transaction::setDateTime(uint dt)
278{
279 datetime = dt;
280}
281
282void Repository::Transaction::setLog(const QByteArray &l)
283{
284 log = l;
285}
286
287void Repository::Transaction::deleteFile(const QString &path)
288{
289 deletedFiles.append(path);
290}
291
292QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
293{
294 int mark = ++repository->lastmark;
295
296 if (modifiedFiles.capacity() == 0)
297 modifiedFiles.reserve(2048);
298 modifiedFiles.append("M ");
299 modifiedFiles.append(QByteArray::number(mode, 8));
300 modifiedFiles.append(" :");
301 modifiedFiles.append(QByteArray::number(mark));
302 modifiedFiles.append(' ');
303 modifiedFiles.append(path.toUtf8());
304 modifiedFiles.append("\n");
305
306#ifndef DRY_RUN
307 repository->fastImport.write("blob\nmark :");
308 repository->fastImport.write(QByteArray::number(mark));
309 repository->fastImport.write("\ndata ");
310 repository->fastImport.write(QByteArray::number(length));
311 repository->fastImport.write("\n", 1);
312#endif
313
314 return &repository->fastImport;
315}
316
317void Repository::Transaction::commit()
318{
319 processCache.touch(repository);
320
321 // create the commit message
322 QByteArray message = log;
323 if (!message.endsWith('\n'))
324 message += '\n';
325 if (Options::globalOptions->switches.value("metadata", true))
326 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
327
328 {
329 QByteArray branchRef = branch;
330 if (!branchRef.startsWith("refs/"))
331 branchRef.prepend("refs/heads/");
332
333 QTextStream s(&repository->fastImport);
334 s << "commit " << branchRef << endl;
335 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
336
337 Branch &br = repository->branches[branch];
338 if (!br.created) {
339 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
340 << revnum << "-- did you resume from the wrong revision?";
341 br.created = revnum;
342 }
343
344 s << "data " << message.length() << endl;
345 }
346
347 repository->fastImport.write(message);
348 repository->fastImport.putChar('\n');
349
350 // write the file deletions
351 if (deletedFiles.contains(""))
352 repository->fastImport.write("deleteall\n");
353 else
354 foreach (QString df, deletedFiles)
355 repository->fastImport.write("D " + df.toUtf8() + "\n");
356
357 // write the file modifications
358 repository->fastImport.write(modifiedFiles);
359
360 repository->fastImport.write("\nprogress Commit #" +
361 QByteArray::number(repository->commitCount) +
362 " branch " + branch +
363 " = SVN r" + QByteArray::number(revnum) + "\n\n");
364 printf(" %d modifications to \"%s\"",
365 deletedFiles.count() + modifiedFiles.count(),
366 qPrintable(repository->name));
367
368 while (repository->fastImport.bytesToWrite())
369 if (!repository->fastImport.waitForBytesWritten(-1))
370 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
371}
This page took 0.055924 seconds and 5 git commands to generate.