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