]>
Commit | Line | Data |
---|---|---|
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 | |
22 | Repository::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 | |
38 | Repository::~Repository() | |
39 | { | |
3ffa3592 | 40 | if (fastImport.state() != QProcess::NotRunning) { |
1c4df5aa TM |
41 | fastImport.closeWriteChannel(); |
42 | fastImport.waitForFinished(); | |
43 | } | |
44 | } | |
45 | ||
1a688729 TM |
46 | void 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 |
76 | void Repository::createBranch(const QString &branch, int revnum, |
77 | const QString &branchFrom, int) | |
78 | { | |
79 | if (!branches.contains(branch)) { | |
7dd430d8 TM |
80 | qWarning() << branch << "is not a known branch in repository" << name << endl |
81 | << "Going to create it automatically"; | |
c338ae37 TM |
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 | ||
7dd430d8 TM |
103 | if (!branches.contains(branchFrom) || !branches.value(branchFrom).created) { |
104 | qCritical() << branch << "in repository" << name | |
105 | << "is branching from branch" << branchFrom | |
106 | << "but the latter doesn't exist. Can't continue."; | |
107 | exit(1); | |
108 | } | |
109 | ||
c338ae37 TM |
110 | fastImport.write("reset " + branchRef + "\nfrom " + branchFromRef + "\n\n"); |
111 | } | |
112 | ||
1c4df5aa TM |
113 | Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix, |
114 | int revnum) | |
115 | { | |
b6ba9639 | 116 | if (!branches.contains(branch)) { |
ff26f180 TM |
117 | qWarning() << branch << "is not a known branch in repository" << name << endl |
118 | << "Going to create it automatically"; | |
b6ba9639 | 119 | } |
1c4df5aa TM |
120 | |
121 | Transaction *txn = new Transaction; | |
122 | txn->repository = this; | |
123 | txn->branch = branch.toUtf8(); | |
124 | txn->svnprefix = svnprefix.toUtf8(); | |
125 | txn->datetime = 0; | |
126 | txn->revnum = revnum; | |
127 | txn->lastmark = revnum; | |
128 | ||
1a688729 | 129 | startFastImport(); |
a812d0b1 | 130 | if ((++commitCount % 10000) == 0) |
b50b9285 TM |
131 | // write everything to disk every 10000 commits |
132 | fastImport.write("checkpoint\n"); | |
1a688729 TM |
133 | return txn; |
134 | } | |
135 | ||
136 | void Repository::startFastImport() | |
137 | { | |
688d69ec | 138 | if (fastImport.state() == QProcess::NotRunning) { |
298d7968 TM |
139 | if (processHasStarted) |
140 | qFatal("git-fast-import has been started once and crashed?"); | |
141 | processHasStarted = true; | |
142 | ||
1c4df5aa | 143 | // start the process |
298d7968 TM |
144 | QString outputFile = name; |
145 | outputFile.replace('/', '_'); | |
146 | outputFile.prepend("log-"); | |
147 | fastImport.setStandardOutputFile(outputFile, QIODevice::Append); | |
f34275cf | 148 | fastImport.setProcessChannelMode(QProcess::MergedChannels); |
298d7968 | 149 | |
688d69ec | 150 | #ifndef DRY_RUN |
1c4df5aa | 151 | fastImport.start("git-fast-import", QStringList()); |
688d69ec TM |
152 | #else |
153 | fastImport.start("/bin/cat", QStringList()); | |
154 | #endif | |
155 | } | |
1c4df5aa TM |
156 | } |
157 | ||
158 | Repository::Transaction::~Transaction() | |
159 | { | |
160 | } | |
161 | ||
162 | void Repository::Transaction::setAuthor(const QByteArray &a) | |
163 | { | |
164 | author = a; | |
165 | } | |
166 | ||
167 | void Repository::Transaction::setDateTime(uint dt) | |
168 | { | |
169 | datetime = dt; | |
170 | } | |
171 | ||
172 | void Repository::Transaction::setLog(const QByteArray &l) | |
173 | { | |
174 | log = l; | |
175 | } | |
176 | ||
177 | void Repository::Transaction::deleteFile(const QString &path) | |
178 | { | |
179 | deletedFiles.append(path); | |
180 | } | |
181 | ||
182 | QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length) | |
183 | { | |
184 | FileProperties fp; | |
185 | fp.mode = mode; | |
186 | fp.mark = ++lastmark; | |
187 | ||
688d69ec | 188 | #ifndef DRY_RUN |
1c4df5aa TM |
189 | repository->fastImport.write("blob\nmark :"); |
190 | repository->fastImport.write(QByteArray::number(fp.mark)); | |
191 | repository->fastImport.write("\ndata "); | |
192 | repository->fastImport.write(QByteArray::number(length)); | |
193 | repository->fastImport.write("\n", 1); | |
688d69ec | 194 | #endif |
1c4df5aa TM |
195 | |
196 | modifiedFiles.insert(path, fp); | |
197 | return &repository->fastImport; | |
198 | } | |
199 | ||
200 | void Repository::Transaction::commit() | |
201 | { | |
202 | // create the commit message | |
203 | QByteArray message = log; | |
204 | if (!message.endsWith('\n')) | |
205 | message += '\n'; | |
d91c2d9b | 206 | message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n"; |
1c4df5aa TM |
207 | |
208 | { | |
209 | QByteArray branchRef = branch; | |
8d7b49e5 | 210 | if (!branchRef.startsWith("refs/")) |
1c4df5aa TM |
211 | branchRef.prepend("refs/heads/"); |
212 | ||
213 | QTextStream s(&repository->fastImport); | |
214 | s << "commit " << branchRef << endl; | |
215 | s << "mark :" << revnum << endl; | |
f34275cf | 216 | s << "committer " << QString::fromUtf8(author) << ' ' << datetime << " -0000" << endl; |
1c4df5aa TM |
217 | |
218 | Branch &br = repository->branches[branch]; | |
bdc20860 | 219 | if (!br.created) { |
72da4610 | 220 | qWarning() << "Branch" << branch << "in repository" << repository->name << "doesn't exist at revision" |
bdc20860 TM |
221 | << revnum << "-- did you resume from the wrong revision?"; |
222 | br.created = revnum; | |
223 | } | |
1c4df5aa TM |
224 | |
225 | s << "data " << message.length() << endl; | |
226 | } | |
227 | ||
228 | repository->fastImport.write(message); | |
6b510c52 | 229 | repository->fastImport.putChar('\n'); |
1c4df5aa TM |
230 | |
231 | // write the file deletions | |
6b450d1d TM |
232 | if (deletedFiles.contains("")) |
233 | repository->fastImport.write("deleteall\n"); | |
234 | else | |
235 | foreach (QString df, deletedFiles) | |
236 | repository->fastImport.write("D " + df.toUtf8() + "\n"); | |
1c4df5aa TM |
237 | |
238 | // write the file modifications | |
239 | QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin(); | |
240 | for ( ; it != modifiedFiles.constEnd(); ++it) { | |
241 | repository->fastImport.write("M ", 2); | |
242 | repository->fastImport.write(QByteArray::number(it->mode, 8)); | |
243 | repository->fastImport.write(" :", 2); | |
244 | repository->fastImport.write(QByteArray::number(it->mark)); | |
245 | repository->fastImport.write(" ", 1); | |
246 | repository->fastImport.write(it.key().toUtf8()); | |
247 | repository->fastImport.write("\n", 1); | |
248 | } | |
249 | ||
250 | repository->fastImport.write("\n"); | |
251 | ||
25f3205a | 252 | while (repository->fastImport.bytesToWrite()) |
50cd32a4 | 253 | if (!repository->fastImport.waitForBytesWritten(-1)) |
25f3205a | 254 | qFatal("Failed to write to process: %s", qPrintable(repository->fastImport.errorString())); |
1c4df5aa | 255 | } |