]>
Commit | Line | Data |
---|---|---|
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 <QTextStream> | |
20 | #include <QDebug> | |
21 | #include <QLinkedList> | |
22 | ||
23 | static const int maxSimultaneousProcesses = 100; | |
24 | ||
25 | class ProcessCache: QLinkedList<Repository *> | |
26 | { | |
27 | public: | |
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 | }; | |
45 | static ProcessCache processCache; | |
46 | ||
47 | Repository::Repository(const Rules::Repository &rule) | |
48 | : name(rule.name), commitCount(0), outstandingTransactions(0), processHasStarted(false) | |
49 | { | |
50 | foreach (Rules::Repository::Branch branchRule, rule.branches) { | |
51 | Branch branch; | |
52 | branch.created = 0; // not created | |
53 | ||
54 | branches.insert(branchRule.name, branch); | |
55 | } | |
56 | ||
57 | // create the default branch | |
58 | branches["master"].created = 1; | |
59 | ||
60 | fastImport.setWorkingDirectory(name); | |
61 | } | |
62 | ||
63 | Repository::~Repository() | |
64 | { | |
65 | Q_ASSERT(outstandingTransactions == 0); | |
66 | closeFastImport(); | |
67 | } | |
68 | ||
69 | void Repository::closeFastImport() | |
70 | { | |
71 | if (fastImport.state() != QProcess::NotRunning) { | |
72 | fastImport.write("checkpoint\n"); | |
73 | fastImport.waitForBytesWritten(-1); | |
74 | fastImport.closeWriteChannel(); | |
75 | if (!fastImport.waitForFinished()) { | |
76 | fastImport.terminate(); | |
77 | if (!fastImport.waitForFinished(200)) | |
78 | qWarning() << "git-fast-import for repository" << name << "did not die"; | |
79 | } | |
80 | } | |
81 | processHasStarted = false; | |
82 | processCache.remove(this); | |
83 | } | |
84 | ||
85 | void Repository::reloadBranches() | |
86 | { | |
87 | QProcess revParse; | |
88 | revParse.setWorkingDirectory(name); | |
89 | revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches"); | |
90 | revParse.waitForFinished(-1); | |
91 | ||
92 | if (revParse.exitCode() == 0 && revParse.bytesAvailable()) { | |
93 | while (revParse.canReadLine()) { | |
94 | QByteArray branchName = revParse.readLine().trimmed(); | |
95 | ||
96 | //qDebug() << "Repo" << name << "reloaded branch" << branchName; | |
97 | branches[branchName].created = 1; | |
98 | fastImport.write("reset refs/heads/" + branchName + | |
99 | "\nfrom refs/heads/" + branchName + "^0\n\n" | |
100 | "progress Branch refs/heads/" + branchName + " reloaded\n"); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | void Repository::createBranch(const QString &branch, int revnum, | |
106 | const QString &branchFrom, int) | |
107 | { | |
108 | startFastImport(); | |
109 | if (!branches.contains(branch)) { | |
110 | qWarning() << branch << "is not a known branch in repository" << name << endl | |
111 | << "Going to create it automatically"; | |
112 | } | |
113 | ||
114 | QByteArray branchRef = branch.toUtf8(); | |
115 | if (!branchRef.startsWith("refs/")) | |
116 | branchRef.prepend("refs/heads/"); | |
117 | ||
118 | Branch &br = branches[branch]; | |
119 | if (br.created && br.created != revnum) { | |
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 | |
127 | br.created = revnum; | |
128 | QByteArray branchFromRef = branchFrom.toUtf8(); | |
129 | if (!branchFromRef.startsWith("refs/")) | |
130 | branchFromRef.prepend("refs/heads/"); | |
131 | ||
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 | ||
139 | fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n" | |
140 | "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n"); | |
141 | } | |
142 | ||
143 | Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix, | |
144 | int revnum) | |
145 | { | |
146 | startFastImport(); | |
147 | if (!branches.contains(branch)) { | |
148 | qWarning() << branch << "is not a known branch in repository" << name << endl | |
149 | << "Going to create it automatically"; | |
150 | } | |
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; | |
158 | ||
159 | if ((++commitCount % 10000) == 0) | |
160 | // write everything to disk every 10000 commits | |
161 | fastImport.write("checkpoint\n"); | |
162 | if (outstandingTransactions++ == 0) | |
163 | lastmark = 1; // reset the mark number | |
164 | return txn; | |
165 | } | |
166 | ||
167 | void Repository::startFastImport() | |
168 | { | |
169 | if (fastImport.state() == QProcess::NotRunning) { | |
170 | if (processHasStarted) | |
171 | qFatal("git-fast-import has been started once and crashed?"); | |
172 | processHasStarted = true; | |
173 | ||
174 | // start the process | |
175 | QString outputFile = name; | |
176 | outputFile.replace('/', '_'); | |
177 | outputFile.prepend("log-"); | |
178 | fastImport.setStandardOutputFile(outputFile, QIODevice::Append); | |
179 | fastImport.setProcessChannelMode(QProcess::MergedChannels); | |
180 | ||
181 | #ifndef DRY_RUN | |
182 | fastImport.start("git", QStringList() << "fast-import"); | |
183 | #else | |
184 | fastImport.start("/bin/cat", QStringList()); | |
185 | #endif | |
186 | ||
187 | reloadBranches(); | |
188 | } | |
189 | } | |
190 | ||
191 | Repository::Transaction::~Transaction() | |
192 | { | |
193 | --repository->outstandingTransactions; | |
194 | } | |
195 | ||
196 | void Repository::Transaction::setAuthor(const QByteArray &a) | |
197 | { | |
198 | author = a; | |
199 | } | |
200 | ||
201 | void Repository::Transaction::setDateTime(uint dt) | |
202 | { | |
203 | datetime = dt; | |
204 | } | |
205 | ||
206 | void Repository::Transaction::setLog(const QByteArray &l) | |
207 | { | |
208 | log = l; | |
209 | } | |
210 | ||
211 | void Repository::Transaction::deleteFile(const QString &path) | |
212 | { | |
213 | deletedFiles.append(path); | |
214 | } | |
215 | ||
216 | QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length) | |
217 | { | |
218 | int mark = ++repository->lastmark; | |
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"); | |
229 | ||
230 | #ifndef DRY_RUN | |
231 | repository->fastImport.write("blob\nmark :"); | |
232 | repository->fastImport.write(QByteArray::number(mark)); | |
233 | repository->fastImport.write("\ndata "); | |
234 | repository->fastImport.write(QByteArray::number(length)); | |
235 | repository->fastImport.write("\n", 1); | |
236 | #endif | |
237 | ||
238 | return &repository->fastImport; | |
239 | } | |
240 | ||
241 | void Repository::Transaction::commit() | |
242 | { | |
243 | processCache.touch(repository); | |
244 | ||
245 | // create the commit message | |
246 | QByteArray message = log; | |
247 | if (!message.endsWith('\n')) | |
248 | message += '\n'; | |
249 | message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n"; | |
250 | ||
251 | { | |
252 | QByteArray branchRef = branch; | |
253 | if (!branchRef.startsWith("refs/")) | |
254 | branchRef.prepend("refs/heads/"); | |
255 | ||
256 | QTextStream s(&repository->fastImport); | |
257 | s << "commit " << branchRef << endl; | |
258 | s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl; | |
259 | ||
260 | Branch &br = repository->branches[branch]; | |
261 | if (!br.created) { | |
262 | qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision" | |
263 | << revnum << "-- did you resume from the wrong revision?"; | |
264 | br.created = revnum; | |
265 | } | |
266 | ||
267 | s << "data " << message.length() << endl; | |
268 | } | |
269 | ||
270 | repository->fastImport.write(message); | |
271 | repository->fastImport.putChar('\n'); | |
272 | ||
273 | // write the file deletions | |
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"); | |
279 | ||
280 | // write the file modifications | |
281 | repository->fastImport.write(modifiedFiles); | |
282 | ||
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)); | |
290 | ||
291 | while (repository->fastImport.bytesToWrite()) | |
292 | if (!repository->fastImport.waitForBytesWritten(-1)) | |
293 | qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString())); | |
294 | } |