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"
20 #include <QTextStream>
22 #include <QLinkedList>
24 static const int maxSimultaneousProcesses
= 100;
26 class ProcessCache
: QLinkedList
<Repository
*>
29 void touch(Repository
*repo
)
33 // if the cache is too big, remove from the front
34 while (size() >= maxSimultaneousProcesses
)
35 takeFirst()->closeFastImport();
41 inline void remove(Repository
*repo
)
43 #if QT_VERSION >= 0x040400
50 static ProcessCache processCache
;
52 Repository::Repository(const Rules::Repository
&rule
)
53 : name(rule
.name
), commitCount(0), outstandingTransactions(0), processHasStarted(false)
55 foreach (Rules::Repository::Branch branchRule
, rule
.branches
) {
57 branch
.created
= 0; // not created
59 branches
.insert(branchRule
.name
, branch
);
62 // create the default branch
63 branches
["master"].created
= 1;
65 fastImport
.setWorkingDirectory(name
);
68 Repository::~Repository()
70 Q_ASSERT(outstandingTransactions
== 0);
74 void Repository::closeFastImport()
76 if (fastImport
.state() != QProcess::NotRunning
) {
77 fastImport
.write("checkpoint\n");
78 fastImport
.waitForBytesWritten(-1);
79 fastImport
.closeWriteChannel();
80 if (!fastImport
.waitForFinished()) {
81 fastImport
.terminate();
82 if (!fastImport
.waitForFinished(200))
83 qWarning() << "git-fast-import for repository" << name
<< "did not die";
86 processHasStarted
= false;
87 processCache
.remove(this);
90 void Repository::reloadBranches()
93 revParse
.setWorkingDirectory(name
);
94 revParse
.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
95 revParse
.waitForFinished(-1);
97 if (revParse
.exitCode() == 0 && revParse
.bytesAvailable()) {
98 while (revParse
.canReadLine()) {
99 QByteArray branchName
= revParse
.readLine().trimmed();
101 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
102 branches
[branchName
].created
= 1;
103 fastImport
.write("reset refs/heads/" + branchName
+
104 "\nfrom refs/heads/" + branchName
+ "^0\n\n"
105 "progress Branch refs/heads/" + branchName
+ " reloaded\n");
110 void Repository::createBranch(const QString
&branch
, int revnum
,
111 const QString
&branchFrom
, int)
114 if (!branches
.contains(branch
)) {
115 qWarning() << branch
<< "is not a known branch in repository" << name
<< endl
116 << "Going to create it automatically";
119 QByteArray branchRef
= branch
.toUtf8();
120 if (!branchRef
.startsWith("refs/"))
121 branchRef
.prepend("refs/heads/");
123 Branch
&br
= branches
[branch
];
124 if (br
.created
&& br
.created
!= revnum
) {
125 QByteArray backupBranch
= branchRef
+ '_' + QByteArray::number(revnum
);
126 qWarning() << branch
<< "already exists; backing up to" << backupBranch
;
128 fastImport
.write("reset " + backupBranch
+ "\nfrom " + branchRef
+ "\n\n");
131 // now create the branch
133 QByteArray branchFromRef
= branchFrom
.toUtf8();
134 if (!branchFromRef
.startsWith("refs/"))
135 branchFromRef
.prepend("refs/heads/");
137 if (!branches
.contains(branchFrom
) || !branches
.value(branchFrom
).created
) {
138 qCritical() << branch
<< "in repository" << name
139 << "is branching from branch" << branchFrom
140 << "but the latter doesn't exist. Can't continue.";
144 fastImport
.write("reset " + branchRef
+ "\nfrom " + branchFromRef
+ "\n\n"
145 "progress Branch " + branchRef
+ " created from " + branchFromRef
+ "\n\n");
148 Repository::Transaction
*Repository::newTransaction(const QString
&branch
, const QString
&svnprefix
,
152 if (!branches
.contains(branch
)) {
153 qWarning() << branch
<< "is not a known branch in repository" << name
<< endl
154 << "Going to create it automatically";
157 Transaction
*txn
= new Transaction
;
158 txn
->repository
= this;
159 txn
->branch
= branch
.toUtf8();
160 txn
->svnprefix
= svnprefix
.toUtf8();
162 txn
->revnum
= revnum
;
164 if ((++commitCount
% 10000) == 0)
165 // write everything to disk every 10000 commits
166 fastImport
.write("checkpoint\n");
167 if (outstandingTransactions
++ == 0)
168 lastmark
= 1; // reset the mark number
172 void Repository::startFastImport()
174 if (fastImport
.state() == QProcess::NotRunning
) {
175 if (processHasStarted
)
176 qFatal("git-fast-import has been started once and crashed?");
177 processHasStarted
= true;
180 QString outputFile
= name
;
181 outputFile
.replace('/', '_');
182 outputFile
.prepend("log-");
183 fastImport
.setStandardOutputFile(outputFile
, QIODevice::Append
);
184 fastImport
.setProcessChannelMode(QProcess::MergedChannels
);
187 fastImport
.start("git", QStringList() << "fast-import");
189 fastImport
.start("/bin/cat", QStringList());
196 Repository::Transaction::~Transaction()
198 --repository
->outstandingTransactions
;
201 void Repository::Transaction::setAuthor(const QByteArray
&a
)
206 void Repository::Transaction::setDateTime(uint dt
)
211 void Repository::Transaction::setLog(const QByteArray
&l
)
216 void Repository::Transaction::deleteFile(const QString
&path
)
218 deletedFiles
.append(path
);
221 QIODevice
*Repository::Transaction::addFile(const QString
&path
, int mode
, qint64 length
)
223 int mark
= ++repository
->lastmark
;
225 if (modifiedFiles
.capacity() == 0)
226 modifiedFiles
.reserve(2048);
227 modifiedFiles
.append("M ");
228 modifiedFiles
.append(QByteArray::number(mode
, 8));
229 modifiedFiles
.append(" :");
230 modifiedFiles
.append(QByteArray::number(mark
));
231 modifiedFiles
.append(' ');
232 modifiedFiles
.append(path
.toUtf8());
233 modifiedFiles
.append("\n");
236 repository
->fastImport
.write("blob\nmark :");
237 repository
->fastImport
.write(QByteArray::number(mark
));
238 repository
->fastImport
.write("\ndata ");
239 repository
->fastImport
.write(QByteArray::number(length
));
240 repository
->fastImport
.write("\n", 1);
243 return &repository
->fastImport
;
246 void Repository::Transaction::commit()
248 processCache
.touch(repository
);
250 // create the commit message
251 QByteArray message
= log
;
252 if (!message
.endsWith('\n'))
254 if (Options::globalOptions
->switches
.value("metadata", true))
255 message
+= "\nsvn path=" + svnprefix
+ "; revision=" + QByteArray::number(revnum
) + "\n";
258 QByteArray branchRef
= branch
;
259 if (!branchRef
.startsWith("refs/"))
260 branchRef
.prepend("refs/heads/");
262 QTextStream
s(&repository
->fastImport
);
263 s
<< "commit " << branchRef
<< endl
;
264 s
<< "committer " << QString::fromUtf8(author
) << ' ' << datetime
<< " -0000" << endl
;
266 Branch
&br
= repository
->branches
[branch
];
268 qWarning() << "Branch" << branch
<< "in repository" << repository
->name
<< "doesn't exist at revision"
269 << revnum
<< "-- did you resume from the wrong revision?";
273 s
<< "data " << message
.length() << endl
;
276 repository
->fastImport
.write(message
);
277 repository
->fastImport
.putChar('\n');
279 // write the file deletions
280 if (deletedFiles
.contains(""))
281 repository
->fastImport
.write("deleteall\n");
283 foreach (QString df
, deletedFiles
)
284 repository
->fastImport
.write("D " + df
.toUtf8() + "\n");
286 // write the file modifications
287 repository
->fastImport
.write(modifiedFiles
);
289 repository
->fastImport
.write("\nprogress Commit #" +
290 QByteArray::number(repository
->commitCount
) +
291 " branch " + branch
+
292 " = SVN r" + QByteArray::number(revnum
) + "\n\n");
293 printf(" %d modifications to \"%s\"",
294 deletedFiles
.count() + modifiedFiles
.count(),
295 qPrintable(repository
->name
));
297 while (repository
->fastImport
.bytesToWrite())
298 if (!repository
->fastImport
.waitForBytesWritten(-1))
299 qFatal("Failed to write to process: %s", qPrintable(repository
->fastImport
.errorString()));