2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
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.
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.
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/>.
18 #include "repository.h"
19 #include <QTextStream>
22 Repository::Repository(const Rules::Repository
&rule
)
23 : name(rule
.name
), processHasStarted(false)
25 foreach (Rules::Repository::Branch branchRule
, rule
.branches
) {
27 branch
.created
= 0; // not created
29 branches
.insert(branchRule
.name
, branch
);
32 // create the default branch
33 branches
["master"].created
= 1;
35 fastImport
.setWorkingDirectory(name
);
38 Repository::~Repository()
40 if (fastImport
.state() != QProcess::NotRunning
) {
41 fastImport
.closeWriteChannel();
42 fastImport
.waitForFinished();
46 void Repository::reloadBranches()
49 revParse
.setWorkingDirectory(name
);
50 revParse
.start("git", QStringList() << "rev-parse" << "--symbolic" << "--branches");
51 revParse
.waitForFinished();
53 if (revParse
.exitCode() == 0 && revParse
.bytesAvailable()) {
56 while (revParse
.canReadLine()) {
57 QByteArray branchName
= revParse
.readLine();
59 branches
[branchName
].created
= 1;
60 fastImport
.write("reset refs/heads/" + branchName
+
61 "\nfrom refs/heads/" + branchName
+ "^0\n\n");
66 void Repository::createBranch(const QString
&branch
, int revnum
,
67 const QString
&branchFrom
, int)
69 if (!branches
.contains(branch
)) {
70 qWarning() << branch
<< "is not a known branch in repository" << name
<< endl
71 << "Going to create it automatically";
75 QByteArray branchRef
= branch
.toUtf8();
76 if (!branchRef
.startsWith("refs/"))
77 branchRef
.prepend("refs/heads/");
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
;
84 fastImport
.write("reset " + backupBranch
+ "\nfrom " + branchRef
+ "\n\n");
87 // now create the branch
89 QByteArray branchFromRef
= branchFrom
.toUtf8();
90 if (!branchFromRef
.startsWith("refs/"))
91 branchFromRef
.prepend("refs/heads/");
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.";
100 fastImport
.write("reset " + branchRef
+ "\nfrom " + branchFromRef
+ "\n\n");
103 Repository::Transaction
*Repository::newTransaction(const QString
&branch
, const QString
&svnprefix
,
106 if (!branches
.contains(branch
)) {
107 qWarning() << branch
<< "is not a known branch in repository" << name
<< endl
108 << "Going to create it automatically";
111 Transaction
*txn
= new Transaction
;
112 txn
->repository
= this;
113 txn
->branch
= branch
.toUtf8();
114 txn
->svnprefix
= svnprefix
.toUtf8();
116 txn
->revnum
= revnum
;
117 txn
->lastmark
= revnum
;
120 if ((++commitCount
% 10000) == 0)
121 // write everything to disk every 10000 commits
122 fastImport
.write("checkpoint\n");
126 void Repository::startFastImport()
128 if (fastImport
.state() == QProcess::NotRunning
) {
129 if (processHasStarted
)
130 qFatal("git-fast-import has been started once and crashed?");
131 processHasStarted
= true;
134 QString outputFile
= name
;
135 outputFile
.replace('/', '_');
136 outputFile
.prepend("log-");
137 fastImport
.setStandardOutputFile(outputFile
, QIODevice::Append
);
138 fastImport
.setProcessChannelMode(QProcess::MergedChannels
);
141 fastImport
.start("git-fast-import", QStringList());
143 fastImport
.start("/bin/cat", QStringList());
148 Repository::Transaction::~Transaction()
152 void Repository::Transaction::setAuthor(const QByteArray
&a
)
157 void Repository::Transaction::setDateTime(uint dt
)
162 void Repository::Transaction::setLog(const QByteArray
&l
)
167 void Repository::Transaction::deleteFile(const QString
&path
)
169 deletedFiles
.append(path
);
172 QIODevice
*Repository::Transaction::addFile(const QString
&path
, int mode
, qint64 length
)
176 fp
.mark
= ++lastmark
;
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);
186 modifiedFiles
.insert(path
, fp
);
187 return &repository
->fastImport
;
190 void Repository::Transaction::commit()
192 // create the commit message
193 QByteArray message
= log
;
194 if (!message
.endsWith('\n'))
196 message
+= "\nsvn path=" + svnprefix
+ "; revision=" + QByteArray::number(revnum
) + "\n";
199 QByteArray branchRef
= branch
;
200 if (!branchRef
.startsWith("refs/"))
201 branchRef
.prepend("refs/heads/");
203 QTextStream
s(&repository
->fastImport
);
204 s
<< "commit " << branchRef
<< endl
;
205 s
<< "mark :" << revnum
<< endl
;
206 s
<< "committer " << QString::fromUtf8(author
) << ' ' << datetime
<< " -0000" << endl
;
208 Branch
&br
= repository
->branches
[branch
];
210 qWarning() << "Branch" << branch
<< "in repository" << repository
->name
<< "doesn't exist at revision"
211 << revnum
<< "-- did you resume from the wrong revision?";
215 s
<< "data " << message
.length() << endl
;
218 repository
->fastImport
.write(message
);
219 repository
->fastImport
.putChar('\n');
221 // write the file deletions
222 if (deletedFiles
.contains(""))
223 repository
->fastImport
.write("deleteall\n");
225 foreach (QString df
, deletedFiles
)
226 repository
->fastImport
.write("D " + df
.toUtf8() + "\n");
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);
240 repository
->fastImport
.write("\n");
242 while (repository
->fastImport
.bytesToWrite())
243 if (!repository
->fastImport
.waitForBytesWritten(-1))
244 qFatal("Failed to write to process: %s", qPrintable(repository
->fastImport
.errorString()));