]> andersk Git - svn-all-fast-export.git/blame - src/repository.cpp
Keep track of when a given branch was created instead
[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)
b6ba9639 23 : name(rule.name)
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{
48 QHash<QString, Branch>::Iterator it = branches.begin(),
49 end = branches.end();
50 for ( ; it != end; ++it) {
51 QString branchRef = it.key();
8d7b49e5 52 if (!branchRef.startsWith("refs/"))
1a688729
TM
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() +
2cdbbd82 70 "\nfrom " + branchRef.toUtf8() + "^0\n\n");
e087596c 71 it->created = 1;
1a688729
TM
72 }
73 }
74}
75
c338ae37
TM
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];
e087596c 90 if (br.created && br.created != revnum) {
c338ae37
TM
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
e087596c 98 br.created = revnum;
c338ae37
TM
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
1c4df5aa
TM
106Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
107 int revnum)
108{
b6ba9639 109 if (!branches.contains(branch)) {
9d158550 110 qCritical() << branch << "is not a known branch in repository" << name;
1c4df5aa 111 return 0;
b6ba9639 112 }
1c4df5aa
TM
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
1a688729
TM
122 startFastImport();
123 return txn;
124}
125
126void Repository::startFastImport()
127{
688d69ec 128 if (fastImport.state() == QProcess::NotRunning) {
1c4df5aa 129 // start the process
688d69ec 130#ifndef DRY_RUN
9fa82b40 131 fastImport.setProcessChannelMode(QProcess::ForwardedChannels);
1c4df5aa 132 fastImport.start("git-fast-import", QStringList());
688d69ec 133#else
295db4b2
TM
134 QString outputFile = name;
135 outputFile.replace('/', '_');
893ad8c5 136 fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
688d69ec
TM
137 fastImport.start("/bin/cat", QStringList());
138#endif
139 }
1c4df5aa
TM
140}
141
142Repository::Transaction::~Transaction()
143{
144}
145
146void Repository::Transaction::setAuthor(const QByteArray &a)
147{
148 author = a;
149}
150
151void Repository::Transaction::setDateTime(uint dt)
152{
153 datetime = dt;
154}
155
156void Repository::Transaction::setLog(const QByteArray &l)
157{
158 log = l;
159}
160
161void Repository::Transaction::deleteFile(const QString &path)
162{
163 deletedFiles.append(path);
164}
165
166QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
167{
168 FileProperties fp;
169 fp.mode = mode;
170 fp.mark = ++lastmark;
171
688d69ec 172#ifndef DRY_RUN
1c4df5aa
TM
173 repository->fastImport.write("blob\nmark :");
174 repository->fastImport.write(QByteArray::number(fp.mark));
175 repository->fastImport.write("\ndata ");
176 repository->fastImport.write(QByteArray::number(length));
177 repository->fastImport.write("\n", 1);
178 repository->fastImport.waitForBytesWritten(0);
688d69ec 179#endif
1c4df5aa
TM
180
181 modifiedFiles.insert(path, fp);
182 return &repository->fastImport;
183}
184
185void Repository::Transaction::commit()
186{
187 // create the commit message
188 QByteArray message = log;
189 if (!message.endsWith('\n'))
190 message += '\n';
d91c2d9b 191 message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
1c4df5aa
TM
192
193 {
194 QByteArray branchRef = branch;
8d7b49e5 195 if (!branchRef.startsWith("refs/"))
1c4df5aa
TM
196 branchRef.prepend("refs/heads/");
197
198 QTextStream s(&repository->fastImport);
199 s << "commit " << branchRef << endl;
200 s << "mark :" << revnum << endl;
b6ba9639 201 s << "committer " << author << ' ' << datetime << " -0000" << endl;
1c4df5aa
TM
202
203 Branch &br = repository->branches[branch];
e087596c 204 Q_ASSERT(br.created);
1c4df5aa
TM
205
206 s << "data " << message.length() << endl;
207 }
208
209 repository->fastImport.write(message);
210
211 // write the file deletions
6b450d1d
TM
212 if (deletedFiles.contains(""))
213 repository->fastImport.write("deleteall\n");
214 else
215 foreach (QString df, deletedFiles)
216 repository->fastImport.write("D " + df.toUtf8() + "\n");
1c4df5aa
TM
217
218 // write the file modifications
219 QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin();
220 for ( ; it != modifiedFiles.constEnd(); ++it) {
221 repository->fastImport.write("M ", 2);
222 repository->fastImport.write(QByteArray::number(it->mode, 8));
223 repository->fastImport.write(" :", 2);
224 repository->fastImport.write(QByteArray::number(it->mark));
225 repository->fastImport.write(" ", 1);
226 repository->fastImport.write(it.key().toUtf8());
227 repository->fastImport.write("\n", 1);
228 }
229
230 repository->fastImport.write("\n");
231
232 while (repository->fastImport.bytesToWrite() && repository->fastImport.waitForBytesWritten()) {
233 // nothing
234 }
235}
This page took 0.088498 seconds and 5 git commands to generate.