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"
19 #include <QTextStream>
21 #include <QLinkedList>
23 static const int maxSimultaneousProcesses
= 100;
25 class ProcessCache
: QLinkedList
<Repository
*>
28 void touch(Repository
*repo
)
32 // if the cache is too big, remove from the front
33 while (size() >= maxSimultaneousProcesses
)
34 takeFirst()->closeFastImport();
40 inline void remove(Repository
*repo
)
45 static ProcessCache processCache
;
47 Repository::Repository(const Rules::Repository
&rule
)
48 : name(rule
.name
), commitCount(0), processHasStarted(false)
50 foreach (Rules::Repository::Branch branchRule
, rule
.branches
) {
52 branch
.created
= 0; // not created
54 branches
.insert(branchRule
.name
, branch
);
57 // create the default branch
58 branches
["master"].created
= 1;
60 fastImport
.setWorkingDirectory(name
);
63 Repository::~Repository()
68 void Repository::closeFastImport()
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";
80 processHasStarted
= false;
81 processCache
.remove(this);
84 void Repository::reloadBranches()
87 revParse
.setWorkingDirectory(name
);
88 revParse
.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
89 revParse
.waitForFinished(-1);
91 if (revParse
.exitCode() == 0 && revParse
.bytesAvailable()) {
92 while (revParse
.canReadLine()) {
93 QByteArray branchName
= revParse
.readLine().trimmed();
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");
104 void Repository::createBranch(const QString
&branch
, int revnum
,
105 const QString
&branchFrom
, int)
108 if (!branches
.contains(branch
)) {
109 qWarning() << branch
<< "is not a known branch in repository" << name
<< endl
110 << "Going to create it automatically";
113 QByteArray branchRef
= branch
.toUtf8();
114 if (!branchRef
.startsWith("refs/"))
115 branchRef
.prepend("refs/heads/");
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
;
122 fastImport
.write("reset " + backupBranch
+ "\nfrom " + branchRef
+ "\n\n");
125 // now create the branch
127 QByteArray branchFromRef
= branchFrom
.toUtf8();
128 if (!branchFromRef
.startsWith("refs/"))
129 branchFromRef
.prepend("refs/heads/");
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.";
138 fastImport
.write("reset " + branchRef
+ "\nfrom " + branchFromRef
+ "\n\n"
139 "progress Branch " + branchRef
+ " created from " + branchFromRef
+ "\n\n");
142 Repository::Transaction
*Repository::newTransaction(const QString
&branch
, const QString
&svnprefix
,
146 if (!branches
.contains(branch
)) {
147 qWarning() << branch
<< "is not a known branch in repository" << name
<< endl
148 << "Going to create it automatically";
151 Transaction
*txn
= new Transaction
;
152 txn
->repository
= this;
153 txn
->branch
= branch
.toUtf8();
154 txn
->svnprefix
= svnprefix
.toUtf8();
156 txn
->revnum
= revnum
;
157 txn
->lastmark
= revnum
;
159 if ((++commitCount
% 10000) == 0)
160 // write everything to disk every 10000 commits
161 fastImport
.write("checkpoint\n");
165 void Repository::startFastImport()
167 if (fastImport
.state() == QProcess::NotRunning
) {
168 if (processHasStarted
)
169 qFatal("git-fast-import has been started once and crashed?");
170 processHasStarted
= true;
173 QString outputFile
= name
;
174 outputFile
.replace('/', '_');
175 outputFile
.prepend("log-");
176 fastImport
.setStandardOutputFile(outputFile
, QIODevice::Append
);
177 fastImport
.setProcessChannelMode(QProcess::MergedChannels
);
180 fastImport
.start("git", QStringList() << "fast-import");
182 fastImport
.start("/bin/cat", QStringList());
189 Repository::Transaction::~Transaction()
193 void Repository::Transaction::setAuthor(const QByteArray
&a
)
198 void Repository::Transaction::setDateTime(uint dt
)
203 void Repository::Transaction::setLog(const QByteArray
&l
)
208 void Repository::Transaction::deleteFile(const QString
&path
)
210 deletedFiles
.append(path
);
213 QIODevice
*Repository::Transaction::addFile(const QString
&path
, int mode
, qint64 length
)
217 fp
.mark
= ++lastmark
;
220 repository
->fastImport
.write("blob\nmark :");
221 repository
->fastImport
.write(QByteArray::number(fp
.mark
));
222 repository
->fastImport
.write("\ndata ");
223 repository
->fastImport
.write(QByteArray::number(length
));
224 repository
->fastImport
.write("\n", 1);
227 modifiedFiles
.insert(path
, fp
);
228 return &repository
->fastImport
;
231 void Repository::Transaction::commit()
233 processCache
.touch(repository
);
235 // create the commit message
236 QByteArray message
= log
;
237 if (!message
.endsWith('\n'))
239 message
+= "\nsvn path=" + svnprefix
+ "; revision=" + QByteArray::number(revnum
) + "\n";
242 QByteArray branchRef
= branch
;
243 if (!branchRef
.startsWith("refs/"))
244 branchRef
.prepend("refs/heads/");
246 QTextStream
s(&repository
->fastImport
);
247 s
<< "commit " << branchRef
<< endl
;
248 s
<< "mark :" << revnum
<< endl
;
249 s
<< "committer " << QString::fromUtf8(author
) << ' ' << datetime
<< " -0000" << endl
;
251 Branch
&br
= repository
->branches
[branch
];
253 qWarning() << "Branch" << branch
<< "in repository" << repository
->name
<< "doesn't exist at revision"
254 << revnum
<< "-- did you resume from the wrong revision?";
258 s
<< "data " << message
.length() << endl
;
261 repository
->fastImport
.write(message
);
262 repository
->fastImport
.putChar('\n');
264 // write the file deletions
265 if (deletedFiles
.contains(""))
266 repository
->fastImport
.write("deleteall\n");
268 foreach (QString df
, deletedFiles
)
269 repository
->fastImport
.write("D " + df
.toUtf8() + "\n");
271 // write the file modifications
272 QHash
<QString
, FileProperties
>::ConstIterator it
= modifiedFiles
.constBegin();
273 for ( ; it
!= modifiedFiles
.constEnd(); ++it
) {
274 repository
->fastImport
.write("M ", 2);
275 repository
->fastImport
.write(QByteArray::number(it
->mode
, 8));
276 repository
->fastImport
.write(" :", 2);
277 repository
->fastImport
.write(QByteArray::number(it
->mark
));
278 repository
->fastImport
.write(" ", 1);
279 repository
->fastImport
.write(it
.key().toUtf8());
280 repository
->fastImport
.write("\n", 1);
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
));
291 while (repository
->fastImport
.bytesToWrite())
292 if (!repository
->fastImport
.waitForBytesWritten(-1))
293 qFatal("Failed to write to process: %s", qPrintable(repository
->fastImport
.errorString()));