]> andersk Git - svn-all-fast-export.git/blame_incremental - src/repository.cpp
General improvements and reload branches automatically when starting git-fast-import
[svn-all-fast-export.git] / src / repository.cpp
... / ...
CommitLineData
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
22Repository::Repository(const Rules::Repository &rule)
23 : name(rule.name), commitCount(0), processHasStarted(false)
24{
25 foreach (Rules::Repository::Branch branchRule, rule.branches) {
26 Branch branch;
27 branch.created = 0; // not created
28
29 branches.insert(branchRule.name, branch);
30 }
31
32 // create the default branch
33 branches["master"].created = 1;
34
35 fastImport.setWorkingDirectory(name);
36}
37
38Repository::~Repository()
39{
40 if (fastImport.state() != QProcess::NotRunning) {
41 fastImport.write("checkpoint\n");
42 fastImport.waitForBytesWritten(-1);
43 fastImport.closeWriteChannel();
44 if (!fastImport.waitForFinished()) {
45 fastImport.terminate();
46 if (!fastImport.waitForFinished(200))
47 qWarning() << "git-fast-import for repository" << name << "did not die";
48 }
49 }
50}
51
52void Repository::reloadBranches()
53{
54 QProcess revParse;
55 revParse.setWorkingDirectory(name);
56 revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
57 revParse.waitForFinished(-1);
58
59 if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
60 while (revParse.canReadLine()) {
61 QByteArray branchName = revParse.readLine().trimmed();
62
63 //qDebug() << "Repo" << name << "reloaded branch" << branchName;
64 branches[branchName].created = 1;
65 fastImport.write("reset refs/heads/" + branchName +
66 "\nfrom refs/heads/" + branchName + "^0\n\n"
67 "progress Branch refs/heads/" + branchName + " reloaded\n");
68 }
69 }
70}
71
72void Repository::createBranch(const QString &branch, int revnum,
73 const QString &branchFrom, int)
74{
75 startFastImport();
76 if (!branches.contains(branch)) {
77 qWarning() << branch << "is not a known branch in repository" << name << endl
78 << "Going to create it automatically";
79 }
80
81 QByteArray branchRef = branch.toUtf8();
82 if (!branchRef.startsWith("refs/"))
83 branchRef.prepend("refs/heads/");
84
85 Branch &br = branches[branch];
86 if (br.created && br.created != revnum) {
87 QByteArray backupBranch = branchRef + '_' + QByteArray::number(revnum);
88 qWarning() << branch << "already exists; backing up to" << backupBranch;
89
90 fastImport.write("reset " + backupBranch + "\nfrom " + branchRef + "\n\n");
91 }
92
93 // now create the branch
94 br.created = revnum;
95 QByteArray branchFromRef = branchFrom.toUtf8();
96 if (!branchFromRef.startsWith("refs/"))
97 branchFromRef.prepend("refs/heads/");
98
99 if (!branches.contains(branchFrom) || !branches.value(branchFrom).created) {
100 qCritical() << branch << "in repository" << name
101 << "is branching from branch" << branchFrom
102 << "but the latter doesn't exist. Can't continue.";
103 exit(1);
104 }
105
106 fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"
107 "progress Branch " + branchRef + " created from " + branchFromRef + "\n\n");
108}
109
110Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
111 int revnum)
112{
113 startFastImport();
114 if (!branches.contains(branch)) {
115 qWarning() << branch << "is not a known branch in repository" << name << endl
116 << "Going to create it automatically";
117 }
118
119 Transaction *txn = new Transaction;
120 txn->repository = this;
121 txn->branch = branch.toUtf8();
122 txn->svnprefix = svnprefix.toUtf8();
123 txn->datetime = 0;
124 txn->revnum = revnum;
125 txn->lastmark = revnum;
126
127 if ((++commitCount % 10000) == 0)
128 // write everything to disk every 10000 commits
129 fastImport.write("checkpoint\n");
130 return txn;
131}
132
133void Repository::startFastImport()
134{
135 if (fastImport.state() == QProcess::NotRunning) {
136 if (processHasStarted)
137 qFatal("git-fast-import has been started once and crashed?");
138 processHasStarted = true;
139
140 // start the process
141 QString outputFile = name;
142 outputFile.replace('/', '_');
143 outputFile.prepend("log-");
144 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
145 fastImport.setProcessChannelMode(QProcess::MergedChannels);
146
147#ifndef DRY_RUN
148 fastImport.start("git", QStringList() << "fast-import");
149#else
150 fastImport.start("/bin/cat", QStringList());
151#endif
152
153 reloadBranches();
154 }
155}
156
157Repository::Transaction::~Transaction()
158{
159}
160
161void Repository::Transaction::setAuthor(const QByteArray &a)
162{
163 author = a;
164}
165
166void Repository::Transaction::setDateTime(uint dt)
167{
168 datetime = dt;
169}
170
171void Repository::Transaction::setLog(const QByteArray &l)
172{
173 log = l;
174}
175
176void Repository::Transaction::deleteFile(const QString &path)
177{
178 deletedFiles.append(path);
179}
180
181QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
182{
183 FileProperties fp;
184 fp.mode = mode;
185 fp.mark = ++lastmark;
186
187#ifndef DRY_RUN
188 repository->fastImport.write("blob\nmark :");
189 repository->fastImport.write(QByteArray::number(fp.mark));
190 repository->fastImport.write("\ndata ");
191 repository->fastImport.write(QByteArray::number(length));
192 repository->fastImport.write("\n", 1);
193#endif
194
195 modifiedFiles.insert(path, fp);
196 return &repository->fastImport;
197}
198
199void Repository::Transaction::commit()
200{
201 // create the commit message
202 QByteArray message = log;
203 if (!message.endsWith('\n'))
204 message += '\n';
205 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
206
207 {
208 QByteArray branchRef = branch;
209 if (!branchRef.startsWith("refs/"))
210 branchRef.prepend("refs/heads/");
211
212 QTextStream s(&repository->fastImport);
213 s << "commit " << branchRef << endl;
214 s << "mark :" << revnum << endl;
215 s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
216
217 Branch &br = repository->branches[branch];
218 if (!br.created) {
219 qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
220 << revnum << "-- did you resume from the wrong revision?";
221 br.created = revnum;
222 }
223
224 s << "data " << message.length() << endl;
225 }
226
227 repository->fastImport.write(message);
228 repository->fastImport.putChar('\n');
229
230 // write the file deletions
231 if (deletedFiles.contains(""))
232 repository->fastImport.write("deleteall\n");
233 else
234 foreach (QString df, deletedFiles)
235 repository->fastImport.write("D " + df.toUtf8() + "\n");
236
237 // write the file modifications
238 QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin();
239 for ( ; it != modifiedFiles.constEnd(); ++it) {
240 repository->fastImport.write("M ", 2);
241 repository->fastImport.write(QByteArray::number(it->mode, 8));
242 repository->fastImport.write(" :", 2);
243 repository->fastImport.write(QByteArray::number(it->mark));
244 repository->fastImport.write(" ", 1);
245 repository->fastImport.write(it.key().toUtf8());
246 repository->fastImport.write("\n", 1);
247 }
248
249 repository->fastImport.write("\nprogress Commit #" +
250 QByteArray::number(repository->commitCount) +
251 " branch " + branch +
252 " = SVN r" + QByteArray::number(revnum) + "\n\n");
253 printf(" %d modifications to \"%s\"",
254 deletedFiles.count() + modifiedFiles.count(),
255 qPrintable(repository->name));
256
257 while (repository->fastImport.bytesToWrite())
258 if (!repository->fastImport.waitForBytesWritten(-1))
259 qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
260}
This page took 0.282237 seconds and 5 git commands to generate.