]> andersk Git - svn-all-fast-export.git/blame_incremental - src/repository.cpp
Add a --no-metadata option to suppress the svn info in commit messages.
[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 removeOne(repo);
44 }
45};
46static ProcessCache processCache;
47
48Repository::Repository(const Rules::Repository &rule)
49 : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false)
50{
51 foreach (Rules::Repository::Branch branchRule, rule.branches) {
52 Branch branch;
53 branch.created = 0; // not created
54
55 branches.insert(branchRule.name, branch);
56 }
57
58 // create the default branch
59 branches["master"].created = 1;
60
61 fastImport.setWorkingDirectory(name);
62}
63
64Repository::~Repository()
65{
66 Q_ASSERT(outstandingTransactions == 0);
67 closeFastImport();
68}
69
70void Repository::closeFastImport()
71{
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";
80 }
81 }
82 processHasStarted = false;
83 processCache.remove(this);
84}
85
86void Repository::reloadBranches()
87{
88 QProcess revParse;
89 revParse.setWorkingDirectory(name);
90 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
91 revParse.waitForFinished(-1);
92
93 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
94 while (revParse.canReadLine()) {
95 QByteArray branchName = revParse.readLine().trimmed();
96
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");
102 }
103 }
104}
105
106void Repository::createBranch(const QString &branch, int revnum,
107 const QString &branchFrom, int)
108{
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";
113 }
114
115 QByteArray branchRef = branch.toUtf8();
116 if (!branchRef.startsWith("refs/"))
117 branchRef.prepend("refs/heads/");
118
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;
123
124 fastImport.write("reset " + backupBranch + "\nfrom " + branchRef + "\n\n");
125 }
126
127 // now create the branch
128 br.created = revnum;
129 QByteArray branchFromRef = branchFrom.toUtf8();
130 if (!branchFromRef.startsWith("refs/"))
131 branchFromRef.prepend("refs/heads/");
132
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);
138 }
139
140 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
141 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
142}
143
144Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
145 int revnum)
146{
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";
151 }
152
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;
159
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;
166}
167
168void Repository::startFastImport()
169{
170 if (fastImport.state() == QProcess::NotRunning) {
171 if (processHasStarted)
172 qFatal("git-fast-import has been started once and crashed?");
173 processHasStarted = true;
174
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);
181
182#ifndef DRY_RUN
183 fastImport.start("git", QStringList() << "fast-import");
184#else
185 fastImport.start("/bin/cat", QStringList());
186#endif
187
188 reloadBranches();
189 }
190}
191
192Repository::Transaction::~Transaction()
193{
194 --repository->outstandingTransactions;
195}
196
197void Repository::Transaction::setAuthor(const QByteArray &a)
198{
199 author = a;
200}
201
202void Repository::Transaction::setDateTime(uint dt)
203{
204 datetime = dt;
205}
206
207void Repository::Transaction::setLog(const QByteArray &l)
208{
209 log = l;
210}
211
212void Repository::Transaction::deleteFile(const QString &path)
213{
214 deletedFiles.append(path);
215}
216
217QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
218{
219 int mark = ++repository->lastmark;
220
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");
230
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
238
239 return &repository->fastImport;
240}
241
242void Repository::Transaction::commit()
243{
244 processCache.touch(repository);
245
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";
252
253 {
254 QByteArray branchRef = branch;
255 if (!branchRef.startsWith("refs/"))
256 branchRef.prepend("refs/heads/");
257
258 QTextStream s(&repository->fastImport);
259 s << "commit " << branchRef << endl;
260 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
261
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;
267 }
268
269 s << "data " << message.length() << endl;
270 }
271
272 repository->fastImport.write(message);
273 repository->fastImport.putChar('\n');
274
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");
281
282 // write the file modifications
283 repository->fastImport.write(modifiedFiles);
284
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));
292
293 while (repository->fastImport.bytesToWrite())
294 if (!repository->fastImport.waitForBytesWritten(-1))
295 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
296}
This page took 0.1353 seconds and 5 git commands to generate.