]> andersk Git - svn-all-fast-export.git/blob - src/repository.cpp
Initialize variable
[svn-all-fast-export.git] / src / repository.cpp
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
22 Repository::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
38 Repository::~Repository()
39 {
40     if (fastImport.state() != QProcess::NotRunning) {
41         fastImport.closeWriteChannel();
42         fastImport.waitForFinished();
43     }
44 }
45
46 void Repository::reloadBranches()
47 {
48     QProcess revParse;
49     revParse.setWorkingDirectory(name);
50     revParse.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
51     revParse.waitForFinished();
52
53     if (revParse.exitCode() == 0 && revParse.bytesAvailable()) {
54         startFastImport();
55
56         while (revParse.canReadLine()) {
57             QByteArray branchName = revParse.readLine().trimmed();
58
59             branches[branchName].created = 1;
60             fastImport.write("reset refs/heads/" + branchName +
61                              "\nfrom refs/heads/" + branchName + "^0\n\n");
62         }
63     }
64 }
65
66 void Repository::createBranch(const QString &branch, int revnum,
67                               const QString &branchFrom, int)
68 {
69     if (!branches.contains(branch)) {
70         qWarning() << branch << "is not a known branch in repository" << name << endl
71                    << "Going to create it automatically";
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];
80     if (br.created && br.created != revnum) {
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
88     br.created = revnum;
89     QByteArray branchFromRef = branchFrom.toUtf8();
90     if (!branchFromRef.startsWith("refs/"))
91         branchFromRef.prepend("refs/heads/");
92
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
100     fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n");
101 }
102
103 Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix,
104                                                     int revnum)
105 {
106     if (!branches.contains(branch)) {
107         qWarning() << branch << "is not a known branch in repository" << name << endl
108                    << "Going to create it automatically";
109     }
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
119     startFastImport();
120     if ((++commitCount % 10000) == 0)
121         // write everything to disk every 10000 commits
122         fastImport.write("checkpoint\n");
123     return txn;
124 }
125
126 void Repository::startFastImport()
127 {
128     if (fastImport.state() == QProcess::NotRunning) {
129         if (processHasStarted)
130             qFatal("git-fast-import has been started once and crashed?");
131         processHasStarted = true;
132
133         // start the process
134         QString outputFile = name;
135         outputFile.replace('/', '_');
136         outputFile.prepend("log-");
137         fastImport.setStandardOutputFile(outputFile, QIODevice::Append);
138         fastImport.setProcessChannelMode(QProcess::MergedChannels);
139
140 #ifndef DRY_RUN
141         fastImport.start("git-fast-import", QStringList());
142 #else
143         fastImport.start("/bin/cat", QStringList());
144 #endif
145     }
146 }
147
148 Repository::Transaction::~Transaction()
149 {
150 }
151
152 void Repository::Transaction::setAuthor(const QByteArray &a)
153 {
154     author = a;
155 }
156
157 void Repository::Transaction::setDateTime(uint dt)
158 {
159     datetime = dt;
160 }
161
162 void Repository::Transaction::setLog(const QByteArray &l)
163 {
164     log = l;
165 }
166
167 void Repository::Transaction::deleteFile(const QString &path)
168 {
169     deletedFiles.append(path);
170 }
171
172 QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
173 {
174     FileProperties fp;
175     fp.mode = mode;
176     fp.mark = ++lastmark;
177
178 #ifndef DRY_RUN
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);
184 #endif
185
186     modifiedFiles.insert(path, fp);
187     return &repository->fastImport;
188 }
189
190 void Repository::Transaction::commit()
191 {
192     // create the commit message
193     QByteArray message = log;
194     if (!message.endsWith('\n'))
195         message += '\n';
196     message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
197
198     {
199         QByteArray branchRef = branch;
200         if (!branchRef.startsWith("refs/"))
201             branchRef.prepend("refs/heads/");
202
203         QTextStream s(&repository->fastImport);
204         s << "commit " << branchRef << endl;
205         s << "mark :" << revnum << endl;
206         s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl;
207
208         Branch &br = repository->branches[branch];
209         if (!br.created) {
210             qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision"
211                        << revnum << "-- did you resume from the wrong revision?";
212             br.created = revnum;
213         }
214
215         s << "data " << message.length() << endl;
216     }
217
218     repository->fastImport.write(message);
219     repository->fastImport.putChar('\n');
220
221     // write the file deletions
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");
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
242     while (repository->fastImport.bytesToWrite())
243         if (!repository->fastImport.waitForBytesWritten(-1))
244             qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString()));
245 }
This page took 0.047592 seconds and 5 git commands to generate.