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