]> andersk Git - svn-all-fast-export.git/blame - src/repository.cpp
Fix a bug when committing to two branches of the same repository in the same SVN revision
[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"
1c4df5aa 19#include <QTextStream>
b6ba9639 20#include <QDebug>
374d4cd6
TM
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;
5a7327f6
TM
46
47Repository::Repository(const Rules::Repository &rule)
8b6c8bd6 48 : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false)
5a7327f6
TM
49{
50 foreach (Rules::Repository::Branch branchRule, rule.branches) {
51 Branch branch;
e087596c 52 branch.created = 0; // not created
5a7327f6
TM
53
54 branches.insert(branchRule.name, branch);
55 }
56
b6ba9639 57 // create the default branch
e087596c 58 branches["master"].created = 1;
b6ba9639 59
1a688729 60 fastImport.setWorkingDirectory(name);
5a7327f6 61}
1c4df5aa
TM
62
63Repository::~Repository()
374d4cd6 64{
8b6c8bd6 65 Q_ASSERT(outstandingTransactions == 0);
374d4cd6
TM
66 closeFastImport();
67}
68
69void Repository::closeFastImport()
1c4df5aa 70{
3ffa3592 71 if (fastImport.state() != QProcess::NotRunning) {
ca192b2c
TM
72 fastImport.write("checkpoint\n");
73 fastImport.waitForBytesWritten(-1);
1c4df5aa 74 fastImport.closeWriteChannel();
ca192b2c
TM
75 if (!fastImport.waitForFinished()) {
76 fastImport.terminate();
77 if (!fastImport.waitForFinished(200))
78 qWarning() << "git-fast-import for repository" << name << "did not die";
79 }
1c4df5aa 80 }
374d4cd6
TM
81 processHasStarted = false;
82 processCache.remove(this);
1c4df5aa
TM
83}
84
1a688729
TM
85void Repository::reloadBranches()
86{
c0a3eea3
TM
87 QProcess revParse;
88 revParse.setWorkingDirectory(name);
89 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
25e6c28e 90 revParse.waitForFinished(-1);
1a688729 91
c0a3eea3 92 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
c0a3eea3 93 while (revParse.canReadLine()) {
b3b433a8 94 QByteArray branchName = revParse.readLine().trimmed();
1a688729 95
25e6c28e 96 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
c0a3eea3
TM
97 branches[branchName].created = 1;
98 fastImport.write("reset refs/heads/" + branchName +
25e6c28e
TM
99 "\nfrom refs/heads/" + branchName + "^0\n\n"
100 "progress Branch refs/heads/" + branchName + " reloaded\n");
1a688729
TM
101 }
102 }
103}
104
c338ae37
TM
105void Repository::createBranch(const QString &branch, int revnum,
106 const QString &branchFrom, int)
107{
25e6c28e 108 startFastImport();
c338ae37 109 if (!branches.contains(branch)) {
7dd430d8
TM
110 qWarning() << branch << "is not a known branch in repository" << name << endl
111 << "Going to create it automatically";
c338ae37
TM
112 }
113
c338ae37
TM
114 QByteArray branchRef = branch.toUtf8();
115 if (!branchRef.startsWith("refs/"))
116 branchRef.prepend("refs/heads/");
117
118 Branch &br = branches[branch];
e087596c 119 if (br.created && br.created != revnum) {
c338ae37
TM
120 QByteArray backupBranch = branchRef + '_' + QByteArray::number(revnum);
121 qWarning() << branch << "already exists; backing up to" << backupBranch;
122
123 fastImport.write("reset " + backupBranch + "\nfrom " + branchRef + "\n\n");
124 }
125
126 // now create the branch
e087596c 127 br.created = revnum;
c338ae37
TM
128 QByteArray branchFromRef = branchFrom.toUtf8();
129 if (!branchFromRef.startsWith("refs/"))
130 branchFromRef.prepend("refs/heads/");
131
7dd430d8
TM
132 if (!branches.contains(branchFrom) || !branches.value(branchFrom).created) {
133 qCritical() << branch << "in repository" << name
134 << "is branching from branch" << branchFrom
135 << "but the latter doesn't exist. Can't continue.";
136 exit(1);
137 }
138
25e6c28e
TM
139 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
140 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
c338ae37
TM
141}
142
1c4df5aa
TM
143Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
144 int revnum)
145{
25e6c28e 146 startFastImport();
b6ba9639 147 if (!branches.contains(branch)) {
ff26f180
TM
148 qWarning() << branch << "is not a known branch in repository" << name << endl
149 << "Going to create it automatically";
b6ba9639 150 }
1c4df5aa
TM
151
152 Transaction *txn = new Transaction;
153 txn->repository = this;
154 txn->branch = branch.toUtf8();
155 txn->svnprefix = svnprefix.toUtf8();
156 txn->datetime = 0;
157 txn->revnum = revnum;
1c4df5aa 158
a812d0b1 159 if ((++commitCount % 10000) == 0)
b50b9285
TM
160 // write everything to disk every 10000 commits
161 fastImport.write("checkpoint\n");
8b6c8bd6
TM
162 if (++outstandingTransactions == 0)
163 lastmark = 1; // reset the mark number
1a688729
TM
164 return txn;
165}
166
167void Repository::startFastImport()
168{
688d69ec 169 if (fastImport.state() == QProcess::NotRunning) {
298d7968
TM
170 if (processHasStarted)
171 qFatal("git-fast-import has been started once and crashed?");
172 processHasStarted = true;
173
1c4df5aa 174 // start the process
298d7968
TM
175 QString outputFile = name;
176 outputFile.replace('/', '_');
177 outputFile.prepend("log-");
178 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
f34275cf 179 fastImport.setProcessChannelMode(QProcess::MergedChannels);
298d7968 180
688d69ec 181#ifndef DRY_RUN
25e6c28e 182 fastImport.start("git", QStringList() << "fast-import");
688d69ec
TM
183#else
184 fastImport.start("/bin/cat", QStringList());
185#endif
25e6c28e
TM
186
187 reloadBranches();
688d69ec 188 }
1c4df5aa
TM
189}
190
191Repository::Transaction::~Transaction()
192{
8b6c8bd6 193 --repository->outstandingTransactions;
1c4df5aa
TM
194}
195
196void Repository::Transaction::setAuthor(const QByteArray &a)
197{
198 author = a;
199}
200
201void Repository::Transaction::setDateTime(uint dt)
202{
203 datetime = dt;
204}
205
206void Repository::Transaction::setLog(const QByteArray &l)
207{
208 log = l;
209}
210
211void Repository::Transaction::deleteFile(const QString &path)
212{
213 deletedFiles.append(path);
214}
215
216QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
217{
8b6c8bd6 218 int mark = ++repository->lastmark;
e3f0499d
TM
219
220 if (modifiedFiles.capacity() == 0)
221 modifiedFiles.reserve(2048);
222 modifiedFiles.append("M ");
223 modifiedFiles.append(QByteArray::number(mode, 8));
224 modifiedFiles.append(" :");
225 modifiedFiles.append(QByteArray::number(mark));
226 modifiedFiles.append(' ');
227 modifiedFiles.append(path.toUtf8());
228 modifiedFiles.append("\n");
1c4df5aa 229
688d69ec 230#ifndef DRY_RUN
1c4df5aa 231 repository->fastImport.write("blob\nmark :");
e3f0499d 232 repository->fastImport.write(QByteArray::number(mark));
1c4df5aa
TM
233 repository->fastImport.write("\ndata ");
234 repository->fastImport.write(QByteArray::number(length));
235 repository->fastImport.write("\n", 1);
688d69ec 236#endif
1c4df5aa 237
1c4df5aa
TM
238 return &repository->fastImport;
239}
240
241void Repository::Transaction::commit()
242{
374d4cd6
TM
243 processCache.touch(repository);
244
1c4df5aa
TM
245 // create the commit message
246 QByteArray message = log;
247 if (!message.endsWith('\n'))
248 message += '\n';
d91c2d9b 249 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
1c4df5aa
TM
250
251 {
252 QByteArray branchRef = branch;
8d7b49e5 253 if (!branchRef.startsWith("refs/"))
1c4df5aa
TM
254 branchRef.prepend("refs/heads/");
255
256 QTextStream s(&repository->fastImport);
257 s << "commit " << branchRef << endl;
f34275cf 258 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
1c4df5aa
TM
259
260 Branch &br = repository->branches[branch];
bdc20860 261 if (!br.created) {
72da4610 262 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
bdc20860
TM
263 << revnum << "-- did you resume from the wrong revision?";
264 br.created = revnum;
265 }
1c4df5aa
TM
266
267 s << "data " << message.length() << endl;
268 }
269
270 repository->fastImport.write(message);
6b510c52 271 repository->fastImport.putChar('\n');
1c4df5aa
TM
272
273 // write the file deletions
6b450d1d
TM
274 if (deletedFiles.contains(""))
275 repository->fastImport.write("deleteall\n");
276 else
277 foreach (QString df, deletedFiles)
278 repository->fastImport.write("D " + df.toUtf8() + "\n");
1c4df5aa
TM
279
280 // write the file modifications
e3f0499d 281 repository->fastImport.write(modifiedFiles);
1c4df5aa 282
25e6c28e
TM
283 repository->fastImport.write("\nprogress Commit #" +
284 QByteArray::number(repository->commitCount) +
285 " branch " + branch +
286 " = SVN r" + QByteArray::number(revnum) + "\n\n");
287 printf(" %d modifications to \"%s\"",
288 deletedFiles.count() + modifiedFiles.count(),
289 qPrintable(repository->name));
1c4df5aa 290
25f3205a 291 while (repository->fastImport.bytesToWrite())
50cd32a4 292 if (!repository->fastImport.waitForBytesWritten(-1))
25f3205a 293 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
1c4df5aa 294}
This page took 0.419513 seconds and 5 git commands to generate.