9c2a9d5421964cd2261a0f75c2ebedf9ae91d369
[svn-all-fast-export.git] / src / repository.cpp
blob9c2a9d5421964cd2261a0f75c2ebedf9ae91d369
1 /*
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 "options.h"
20 #include <QTextStream>
21 #include <QDebug>
22 #include <QLinkedList>
24 static const int maxSimultaneousProcesses = 100;
26 class ProcessCache: QLinkedList<Repository *>
28 public:
29 void touch(Repository *repo)
31 remove(repo);
33 // if the cache is too big, remove from the front
34 while (size() >= maxSimultaneousProcesses)
35 takeFirst()->closeFastImport();
37 // append to the end
38 append(repo);
41 inline void remove(Repository *repo)
43 removeOne(repo);
46 static ProcessCache processCache;
48 Repository::Repository(const Rules::Repository &rule)
49 : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false)
51 foreach (Rules::Repository::Branch branchRule, rule.branches) {
52 Branch branch;
53 branch.created = 0; // not created
55 branches.insert(branchRule.name, branch);
58 // create the default branch
59 branches["master"].created = 1;
61 fastImport.setWorkingDirectory(name);
64 Repository::~Repository()
66 Q_ASSERT(outstandingTransactions == 0);
67 closeFastImport();
70 void Repository::closeFastImport()
72 if (fastImport.state() != QProcess::NotRunning) {
73 fastImport.write("checkpoint\n");
74 fastImport.waitForBytesWritten(-1);
75 fastImport.closeWriteChannel();
76 if (!fastImport.waitForFinished()) {
77 fastImport.terminate();
78 if (!fastImport.waitForFinished(200))
79 qWarning() << "git-fast-import for repository" << name << "did not die";
82 processHasStarted = false;
83 processCache.remove(this);
86 void Repository::reloadBranches()
88 QProcess revParse;
89 revParse.setWorkingDirectory(name);
90 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
91 revParse.waitForFinished(-1);
93 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
94 while (revParse.canReadLine()) {
95 QByteArray branchName = revParse.readLine().trimmed();
97 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
98 branches[branchName].created = 1;
99 fastImport.write("reset refs/heads/" + branchName +
100 "\nfrom refs/heads/" + branchName + "^0\n\n"
101 "progress Branch refs/heads/" + branchName + " reloaded\n");
106 void Repository::createBranch(const QString &branch, int revnum,
107 const QString &branchFrom, int)
109 startFastImport();
110 if (!branches.contains(branch)) {
111 qWarning() << branch << "is not a known branch in repository" << name << endl
112 << "Going to create it automatically";
115 QByteArray branchRef = branch.toUtf8();
116 if (!branchRef.startsWith("refs/"))
117 branchRef.prepend("refs/heads/");
119 Branch &br = branches[branch];
120 if (br.created && br.created != revnum) {
121 QByteArray backupBranch = branchRef + '_' + QByteArray::number(revnum);
122 qWarning() << branch << "already exists; backing up to" << backupBranch;
124 fastImport.write("reset " + backupBranch + "\nfrom " + branchRef + "\n\n");
127 // now create the branch
128 br.created = revnum;
129 QByteArray branchFromRef = branchFrom.toUtf8();
130 if (!branchFromRef.startsWith("refs/"))
131 branchFromRef.prepend("refs/heads/");
133 if (!branches.contains(branchFrom) || !branches.value(branchFrom).created) {
134 qCritical() << branch << "in repository" << name
135 << "is branching from branch" << branchFrom
136 << "but the latter doesn't exist. Can't continue.";
137 exit(1);
140 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
141 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
144 Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
145 int revnum)
147 startFastImport();
148 if (!branches.contains(branch)) {
149 qWarning() << branch << "is not a known branch in repository" << name << endl
150 << "Going to create it automatically";
153 Transaction *txn = new Transaction;
154 txn->repository = this;
155 txn->branch = branch.toUtf8();
156 txn->svnprefix = svnprefix.toUtf8();
157 txn->datetime = 0;
158 txn->revnum = revnum;
160 if ((++commitCount % 10000) == 0)
161 // write everything to disk every 10000 commits
162 fastImport.write("checkpoint\n");
163 if (outstandingTransactions++ == 0)
164 lastmark = 1; // reset the mark number
165 return txn;
168 void Repository::startFastImport()
170 if (fastImport.state() == QProcess::NotRunning) {
171 if (processHasStarted)
172 qFatal("git-fast-import has been started once and crashed?");
173 processHasStarted = true;
175 // start the process
176 QString outputFile = name;
177 outputFile.replace('/', '_');
178 outputFile.prepend("log-");
179 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
180 fastImport.setProcessChannelMode(QProcess::MergedChannels);
182 #ifndef DRY_RUN
183 fastImport.start("git", QStringList() << "fast-import");
184 #else
185 fastImport.start("/bin/cat", QStringList());
186 #endif
188 reloadBranches();
192 Repository::Transaction::~Transaction()
194 --repository->outstandingTransactions;
197 void Repository::Transaction::setAuthor(const QByteArray &a)
199 author = a;
202 void Repository::Transaction::setDateTime(uint dt)
204 datetime = dt;
207 void Repository::Transaction::setLog(const QByteArray &l)
209 log = l;
212 void Repository::Transaction::deleteFile(const QString &path)
214 deletedFiles.append(path);
217 QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
219 int mark = ++repository->lastmark;
221 if (modifiedFiles.capacity() == 0)
222 modifiedFiles.reserve(2048);
223 modifiedFiles.append("M ");
224 modifiedFiles.append(QByteArray::number(mode, 8));
225 modifiedFiles.append(" :");
226 modifiedFiles.append(QByteArray::number(mark));
227 modifiedFiles.append(' ');
228 modifiedFiles.append(path.toUtf8());
229 modifiedFiles.append("\n");
231 #ifndef DRY_RUN
232 repository->fastImport.write("blob\nmark :");
233 repository->fastImport.write(QByteArray::number(mark));
234 repository->fastImport.write("\ndata ");
235 repository->fastImport.write(QByteArray::number(length));
236 repository->fastImport.write("\n", 1);
237 #endif
239 return &repository->fastImport;
242 void Repository::Transaction::commit()
244 processCache.touch(repository);
246 // create the commit message
247 QByteArray message = log;
248 if (!message.endsWith('\n'))
249 message += '\n';
250 if (Options::globalOptions->switches.value("metadata", true))
251 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
254 QByteArray branchRef = branch;
255 if (!branchRef.startsWith("refs/"))
256 branchRef.prepend("refs/heads/");
258 QTextStream s(&repository->fastImport);
259 s << "commit " << branchRef << endl;
260 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
262 Branch &br = repository->branches[branch];
263 if (!br.created) {
264 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
265 << revnum << "-- did you resume from the wrong revision?";
266 br.created = revnum;
269 s << "data " << message.length() << endl;
272 repository->fastImport.write(message);
273 repository->fastImport.putChar('\n');
275 // write the file deletions
276 if (deletedFiles.contains(""))
277 repository->fastImport.write("deleteall\n");
278 else
279 foreach (QString df, deletedFiles)
280 repository->fastImport.write("D " + df.toUtf8() + "\n");
282 // write the file modifications
283 repository->fastImport.write(modifiedFiles);
285 repository->fastImport.write("\nprogress Commit #" +
286 QByteArray::number(repository->commitCount) +
287 " branch " + branch +
288 " = SVN r" + QByteArray::number(revnum) + "\n\n");
289 printf(" %d modifications to \"%s\"",
290 deletedFiles.count() + modifiedFiles.count(),
291 qPrintable(repository->name));
293 while (repository->fastImport.bytesToWrite())
294 if (!repository->fastImport.waitForBytesWritten(-1))
295 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));