]> andersk Git - svn-all-fast-export.git/blame - 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
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"
29b44c19 19#include "options.h"
1c4df5aa 20#include <QTextStream>
b6ba9639 21#include <QDebug>
374d4cd6
TM
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;
5a7327f6
TM
47
48Repository::Repository(const Rules::Repository &rule)
8b6c8bd6 49 : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false)
5a7327f6
TM
50{
51 foreach (Rules::Repository::Branch branchRule, rule.branches) {
52 Branch branch;
e087596c 53 branch.created = 0; // not created
5a7327f6
TM
54
55 branches.insert(branchRule.name, branch);
56 }
57
b6ba9639 58 // create the default branch
e087596c 59 branches["master"].created = 1;
b6ba9639 60
1a688729 61 fastImport.setWorkingDirectory(name);
5a7327f6 62}
1c4df5aa
TM
63
64Repository::~Repository()
374d4cd6 65{
8b6c8bd6 66 Q_ASSERT(outstandingTransactions == 0);
374d4cd6
TM
67 closeFastImport();
68}
69
70void Repository::closeFastImport()
1c4df5aa 71{
3ffa3592 72 if (fastImport.state() != QProcess::NotRunning) {
ca192b2c
TM
73 fastImport.write("checkpoint\n");
74 fastImport.waitForBytesWritten(-1);
1c4df5aa 75 fastImport.closeWriteChannel();
ca192b2c
TM
76 if (!fastImport.waitForFinished()) {
77 fastImport.terminate();
78 if (!fastImport.waitForFinished(200))
79 qWarning() << "git-fast-import for repository" << name << "did not die";
80 }
1c4df5aa 81 }
374d4cd6
TM
82 processHasStarted = false;
83 processCache.remove(this);
1c4df5aa
TM
84}
85
1a688729
TM
86void Repository::reloadBranches()
87{
c0a3eea3
TM
88 QProcess revParse;
89 revParse.setWorkingDirectory(name);
90 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
25e6c28e 91 revParse.waitForFinished(-1);
1a688729 92
c0a3eea3 93 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
c0a3eea3 94 while (revParse.canReadLine()) {
b3b433a8 95 QByteArray branchName = revParse.readLine().trimmed();
1a688729 96
25e6c28e 97 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
c0a3eea3
TM
98 branches[branchName].created = 1;
99 fastImport.write("reset refs/heads/" + branchName +
25e6c28e
TM
100 "\nfrom refs/heads/" + branchName + "^0\n\n"
101 "progress Branch refs/heads/" + branchName + " reloaded\n");
1a688729
TM
102 }
103 }
104}
105
c338ae37
TM
106void Repository::createBranch(const QString &branch, int revnum,
107 const QString &branchFrom, int)
108{
25e6c28e 109 startFastImport();
c338ae37 110 if (!branches.contains(branch)) {
7dd430d8
TM
111 qWarning() << branch << "is not a known branch in repository" << name << endl
112 << "Going to create it automatically";
c338ae37
TM
113 }
114
c338ae37
TM
115 QByteArray branchRef = branch.toUtf8();
116 if (!branchRef.startsWith("refs/"))
117 branchRef.prepend("refs/heads/");
118
119 Branch &br = branches[branch];
e087596c 120 if (br.created && br.created != revnum) {
c338ae37
TM
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
e087596c 128 br.created = revnum;
c338ae37
TM
129 QByteArray branchFromRef = branchFrom.toUtf8();
130 if (!branchFromRef.startsWith("refs/"))
131 branchFromRef.prepend("refs/heads/");
132
7dd430d8
TM
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
25e6c28e
TM
140 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
141 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
c338ae37
TM
142}
143
1c4df5aa
TM
144Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
145 int revnum)
146{
25e6c28e 147 startFastImport();
b6ba9639 148 if (!branches.contains(branch)) {
ff26f180
TM
149 qWarning() << branch << "is not a known branch in repository" << name << endl
150 << "Going to create it automatically";
b6ba9639 151 }
1c4df5aa
TM
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;
1c4df5aa 159
a812d0b1 160 if ((++commitCount % 10000) == 0)
b50b9285
TM
161 // write everything to disk every 10000 commits
162 fastImport.write("checkpoint\n");
fc7b4bcc 163 if (outstandingTransactions++ == 0)
8b6c8bd6 164 lastmark = 1; // reset the mark number
1a688729
TM
165 return txn;
166}
167
168void Repository::startFastImport()
169{
688d69ec 170 if (fastImport.state() == QProcess::NotRunning) {
298d7968
TM
171 if (processHasStarted)
172 qFatal("git-fast-import has been started once and crashed?");
173 processHasStarted = true;
174
1c4df5aa 175 // start the process
298d7968
TM
176 QString outputFile = name;
177 outputFile.replace('/', '_');
178 outputFile.prepend("log-");
179 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
f34275cf 180 fastImport.setProcessChannelMode(QProcess::MergedChannels);
298d7968 181
688d69ec 182#ifndef DRY_RUN
25e6c28e 183 fastImport.start("git", QStringList() << "fast-import");
688d69ec
TM
184#else
185 fastImport.start("/bin/cat", QStringList());
186#endif
25e6c28e
TM
187
188 reloadBranches();
688d69ec 189 }
1c4df5aa
TM
190}
191
192Repository::Transaction::~Transaction()
193{
8b6c8bd6 194 --repository->outstandingTransactions;
1c4df5aa
TM
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{
8b6c8bd6 219 int mark = ++repository->lastmark;
e3f0499d
TM
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");
1c4df5aa 230
688d69ec 231#ifndef DRY_RUN
1c4df5aa 232 repository->fastImport.write("blob\nmark :");
e3f0499d 233 repository->fastImport.write(QByteArray::number(mark));
1c4df5aa
TM
234 repository->fastImport.write("\ndata ");
235 repository->fastImport.write(QByteArray::number(length));
236 repository->fastImport.write("\n", 1);
688d69ec 237#endif
1c4df5aa 238
1c4df5aa
TM
239 return &repository->fastImport;
240}
241
242void Repository::Transaction::commit()
243{
374d4cd6
TM
244 processCache.touch(repository);
245
1c4df5aa
TM
246 // create the commit message
247 QByteArray message = log;
248 if (!message.endsWith('\n'))
249 message += '\n';
29b44c19
AK
250 if (Options::globalOptions->switches.value("metadata", true))
251 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
1c4df5aa
TM
252
253 {
254 QByteArray branchRef = branch;
8d7b49e5 255 if (!branchRef.startsWith("refs/"))
1c4df5aa
TM
256 branchRef.prepend("refs/heads/");
257
258 QTextStream s(&repository->fastImport);
259 s << "commit " << branchRef << endl;
f34275cf 260 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
1c4df5aa
TM
261
262 Branch &br = repository->branches[branch];
bdc20860 263 if (!br.created) {
72da4610 264 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
bdc20860
TM
265 << revnum << "-- did you resume from the wrong revision?";
266 br.created = revnum;
267 }
1c4df5aa
TM
268
269 s << "data " << message.length() << endl;
270 }
271
272 repository->fastImport.write(message);
6b510c52 273 repository->fastImport.putChar('\n');
1c4df5aa
TM
274
275 // write the file deletions
6b450d1d
TM
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");
1c4df5aa
TM
281
282 // write the file modifications
e3f0499d 283 repository->fastImport.write(modifiedFiles);
1c4df5aa 284
25e6c28e
TM
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));
1c4df5aa 292
25f3205a 293 while (repository->fastImport.bytesToWrite())
50cd32a4 294 if (!repository->fastImport.waitForBytesWritten(-1))
25f3205a 295 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
1c4df5aa 296}
This page took 0.11976 seconds and 5 git commands to generate.