]> andersk Git - svn-all-fast-export.git/blame_incremental - src/repository.cpp
Store the modified files in git-fast-import format already.
[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 <QTextStream>
20#include <QDebug>
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;
46
47Repository::Repository(const Rules::Repository &rule)
48 : name(rule.name), commitCount(0), processHasStarted(false)
49{
50 foreach (Rules::Repository::Branch branchRule, rule.branches) {
51 Branch branch;
52 branch.created = 0; // not created
53
54 branches.insert(branchRule.name, branch);
55 }
56
57 // create the default branch
58 branches["master"].created = 1;
59
60 fastImport.setWorkingDirectory(name);
61}
62
63Repository::~Repository()
64{
65 closeFastImport();
66}
67
68void Repository::closeFastImport()
69{
70 if (fastImport.state() != QProcess::NotRunning) {
71 fastImport.write("checkpoint\n");
72 fastImport.waitForBytesWritten(-1);
73 fastImport.closeWriteChannel();
74 if (!fastImport.waitForFinished()) {
75 fastImport.terminate();
76 if (!fastImport.waitForFinished(200))
77 qWarning() << "git-fast-import for repository" << name << "did not die";
78 }
79 }
80 processHasStarted = false;
81 processCache.remove(this);
82}
83
84void Repository::reloadBranches()
85{
86 QProcess revParse;
87 revParse.setWorkingDirectory(name);
88 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
89 revParse.waitForFinished(-1);
90
91 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
92 while (revParse.canReadLine()) {
93 QByteArray branchName = revParse.readLine().trimmed();
94
95 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
96 branches[branchName].created = 1;
97 fastImport.write("reset refs/heads/" + branchName +
98 "\nfrom refs/heads/" + branchName + "^0\n\n"
99 "progress Branch refs/heads/" + branchName + " reloaded\n");
100 }
101 }
102}
103
104void Repository::createBranch(const QString &branch, int revnum,
105 const QString &branchFrom, int)
106{
107 startFastImport();
108 if (!branches.contains(branch)) {
109 qWarning() << branch << "is not a known branch in repository" << name << endl
110 << "Going to create it automatically";
111 }
112
113 QByteArray branchRef = branch.toUtf8();
114 if (!branchRef.startsWith("refs/"))
115 branchRef.prepend("refs/heads/");
116
117 Branch &br = branches[branch];
118 if (br.created && br.created != revnum) {
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
126 br.created = revnum;
127 QByteArray branchFromRef = branchFrom.toUtf8();
128 if (!branchFromRef.startsWith("refs/"))
129 branchFromRef.prepend("refs/heads/");
130
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
138 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
139 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
140}
141
142Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
143 int revnum)
144{
145 startFastImport();
146 if (!branches.contains(branch)) {
147 qWarning() << branch << "is not a known branch in repository" << name << endl
148 << "Going to create it automatically";
149 }
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
159 if ((++commitCount % 10000) == 0)
160 // write everything to disk every 10000 commits
161 fastImport.write("checkpoint\n");
162 return txn;
163}
164
165void Repository::startFastImport()
166{
167 if (fastImport.state() == QProcess::NotRunning) {
168 if (processHasStarted)
169 qFatal("git-fast-import has been started once and crashed?");
170 processHasStarted = true;
171
172 // start the process
173 QString outputFile = name;
174 outputFile.replace('/', '_');
175 outputFile.prepend("log-");
176 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
177 fastImport.setProcessChannelMode(QProcess::MergedChannels);
178
179#ifndef DRY_RUN
180 fastImport.start("git", QStringList() << "fast-import");
181#else
182 fastImport.start("/bin/cat", QStringList());
183#endif
184
185 reloadBranches();
186 }
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 int mark = ++lastmark;
216
217 if (modifiedFiles.capacity() == 0)
218 modifiedFiles.reserve(2048);
219 modifiedFiles.append("M ");
220 modifiedFiles.append(QByteArray::number(mode, 8));
221 modifiedFiles.append(" :");
222 modifiedFiles.append(QByteArray::number(mark));
223 modifiedFiles.append(' ');
224 modifiedFiles.append(path.toUtf8());
225 modifiedFiles.append("\n");
226
227#ifndef DRY_RUN
228 repository->fastImport.write("blob\nmark :");
229 repository->fastImport.write(QByteArray::number(mark));
230 repository->fastImport.write("\ndata ");
231 repository->fastImport.write(QByteArray::number(length));
232 repository->fastImport.write("\n", 1);
233#endif
234
235 return &repository->fastImport;
236}
237
238void Repository::Transaction::commit()
239{
240 processCache.touch(repository);
241
242 // create the commit message
243 QByteArray message = log;
244 if (!message.endsWith('\n'))
245 message += '\n';
246 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
247
248 {
249 QByteArray branchRef = branch;
250 if (!branchRef.startsWith("refs/"))
251 branchRef.prepend("refs/heads/");
252
253 QTextStream s(&repository->fastImport);
254 s << "commit " << branchRef << endl;
255 s << "mark :" << revnum << endl;
256 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
257
258 Branch &br = repository->branches[branch];
259 if (!br.created) {
260 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
261 << revnum << "-- did you resume from the wrong revision?";
262 br.created = revnum;
263 }
264
265 s << "data " << message.length() << endl;
266 }
267
268 repository->fastImport.write(message);
269 repository->fastImport.putChar('\n');
270
271 // write the file deletions
272 if (deletedFiles.contains(""))
273 repository->fastImport.write("deleteall\n");
274 else
275 foreach (QString df, deletedFiles)
276 repository->fastImport.write("D " + df.toUtf8() + "\n");
277
278 // write the file modifications
279 repository->fastImport.write(modifiedFiles);
280
281 repository->fastImport.write("\nprogress Commit #" +
282 QByteArray::number(repository->commitCount) +
283 " branch " + branch +
284 " = SVN r" + QByteArray::number(revnum) + "\n\n");
285 printf(" %d modifications to \"%s\"",
286 deletedFiles.count() + modifiedFiles.count(),
287 qPrintable(repository->name));
288
289 while (repository->fastImport.bytesToWrite())
290 if (!repository->fastImport.waitForBytesWritten(-1))
291 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
292}
This page took 0.027961 seconds and 5 git commands to generate.