]>
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) | |
b6ba9639 | 23 | : name(rule.name) |
5a7327f6 TM |
24 | { |
25 | foreach (Rules::Repository::Branch branchRule, rule.branches) { | |
26 | Branch branch; | |
27 | branch.branchFrom = branchRule.branchFrom; | |
8d7b49e5 | 28 | if (!branch.branchFrom.startsWith("refs/")) |
09f6282e | 29 | branch.branchFrom.prepend("refs/heads/"); |
5a7327f6 TM |
30 | branch.isCreated = false; |
31 | ||
32 | branches.insert(branchRule.name, branch); | |
33 | } | |
34 | ||
b6ba9639 TM |
35 | // create the default branch |
36 | branches["master"].isCreated = true; | |
37 | ||
1a688729 | 38 | fastImport.setWorkingDirectory(name); |
5a7327f6 | 39 | } |
1c4df5aa TM |
40 | |
41 | Repository::~Repository() | |
42 | { | |
3ffa3592 | 43 | if (fastImport.state() != QProcess::NotRunning) { |
1c4df5aa TM |
44 | fastImport.closeWriteChannel(); |
45 | fastImport.waitForFinished(); | |
46 | } | |
47 | } | |
48 | ||
1a688729 TM |
49 | void Repository::reloadBranches() |
50 | { | |
51 | QHash<QString, Branch>::Iterator it = branches.begin(), | |
52 | end = branches.end(); | |
53 | for ( ; it != end; ++it) { | |
54 | QString branchRef = it.key(); | |
8d7b49e5 | 55 | if (!branchRef.startsWith("refs/")) |
1a688729 TM |
56 | branchRef.prepend("refs/heads/"); |
57 | ||
58 | bool branchExists; | |
59 | // does this branch already exist? | |
60 | QProcess revParse; | |
61 | revParse.setWorkingDirectory(name); | |
62 | revParse.start("git-rev-parse", QStringList() << "--verify" << branchRef); | |
63 | revParse.waitForFinished(); | |
64 | ||
65 | if (revParse.exitCode() == 0) | |
66 | branchExists = true; | |
67 | else | |
68 | branchExists = false; | |
69 | ||
70 | if (branchExists) { | |
71 | startFastImport(); | |
72 | fastImport.write("reset " + branchRef.toUtf8() + | |
2cdbbd82 | 73 | "\nfrom " + branchRef.toUtf8() + "^0\n\n"); |
1a688729 TM |
74 | it->isCreated = true; |
75 | } | |
76 | } | |
77 | } | |
78 | ||
1c4df5aa TM |
79 | Repository::Transaction *Repository::newTransaction(const QString &branch, const QString &svnprefix, |
80 | int revnum) | |
81 | { | |
b6ba9639 | 82 | if (!branches.contains(branch)) { |
9d158550 | 83 | qCritical() << branch << "is not a known branch in repository" << name; |
1c4df5aa | 84 | return 0; |
b6ba9639 | 85 | } |
1c4df5aa TM |
86 | |
87 | Transaction *txn = new Transaction; | |
88 | txn->repository = this; | |
89 | txn->branch = branch.toUtf8(); | |
90 | txn->svnprefix = svnprefix.toUtf8(); | |
91 | txn->datetime = 0; | |
92 | txn->revnum = revnum; | |
93 | txn->lastmark = revnum; | |
94 | ||
1a688729 TM |
95 | startFastImport(); |
96 | return txn; | |
97 | } | |
98 | ||
99 | void Repository::startFastImport() | |
100 | { | |
688d69ec | 101 | if (fastImport.state() == QProcess::NotRunning) { |
1c4df5aa | 102 | // start the process |
688d69ec | 103 | #ifndef DRY_RUN |
9fa82b40 | 104 | fastImport.setProcessChannelMode(QProcess::ForwardedChannels); |
1c4df5aa | 105 | fastImport.start("git-fast-import", QStringList()); |
688d69ec | 106 | #else |
9fa82b40 | 107 | fastImport.setStandardOutputFile(name); |
688d69ec TM |
108 | fastImport.start("/bin/cat", QStringList()); |
109 | #endif | |
110 | } | |
1c4df5aa TM |
111 | } |
112 | ||
113 | Repository::Transaction::~Transaction() | |
114 | { | |
115 | } | |
116 | ||
117 | void Repository::Transaction::setAuthor(const QByteArray &a) | |
118 | { | |
119 | author = a; | |
120 | } | |
121 | ||
122 | void Repository::Transaction::setDateTime(uint dt) | |
123 | { | |
124 | datetime = dt; | |
125 | } | |
126 | ||
127 | void Repository::Transaction::setLog(const QByteArray &l) | |
128 | { | |
129 | log = l; | |
130 | } | |
131 | ||
132 | void Repository::Transaction::deleteFile(const QString &path) | |
133 | { | |
134 | deletedFiles.append(path); | |
135 | } | |
136 | ||
137 | QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length) | |
138 | { | |
139 | FileProperties fp; | |
140 | fp.mode = mode; | |
141 | fp.mark = ++lastmark; | |
142 | ||
688d69ec | 143 | #ifndef DRY_RUN |
1c4df5aa TM |
144 | repository->fastImport.write("blob\nmark :"); |
145 | repository->fastImport.write(QByteArray::number(fp.mark)); | |
146 | repository->fastImport.write("\ndata "); | |
147 | repository->fastImport.write(QByteArray::number(length)); | |
148 | repository->fastImport.write("\n", 1); | |
149 | repository->fastImport.waitForBytesWritten(0); | |
688d69ec | 150 | #endif |
1c4df5aa TM |
151 | |
152 | modifiedFiles.insert(path, fp); | |
153 | return &repository->fastImport; | |
154 | } | |
155 | ||
156 | void Repository::Transaction::commit() | |
157 | { | |
158 | // create the commit message | |
159 | QByteArray message = log; | |
160 | if (!message.endsWith('\n')) | |
161 | message += '\n'; | |
d91c2d9b | 162 | message += "\nsvn path=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n"; |
1c4df5aa TM |
163 | |
164 | { | |
165 | QByteArray branchRef = branch; | |
8d7b49e5 | 166 | if (!branchRef.startsWith("refs/")) |
1c4df5aa TM |
167 | branchRef.prepend("refs/heads/"); |
168 | ||
169 | QTextStream s(&repository->fastImport); | |
170 | s << "commit " << branchRef << endl; | |
171 | s << "mark :" << revnum << endl; | |
b6ba9639 | 172 | s << "committer " << author << ' ' << datetime << " -0000" << endl; |
1c4df5aa TM |
173 | |
174 | Branch &br = repository->branches[branch]; | |
175 | if (!br.isCreated) { | |
176 | br.isCreated = true; | |
177 | s << "from " << br.branchFrom << endl; | |
178 | } | |
179 | ||
180 | s << "data " << message.length() << endl; | |
181 | } | |
182 | ||
183 | repository->fastImport.write(message); | |
184 | ||
185 | // write the file deletions | |
186 | foreach (QString df, deletedFiles) | |
187 | repository->fastImport.write("D " + df.toUtf8() + "\n"); | |
188 | ||
189 | // write the file modifications | |
190 | QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin(); | |
191 | for ( ; it != modifiedFiles.constEnd(); ++it) { | |
192 | repository->fastImport.write("M ", 2); | |
193 | repository->fastImport.write(QByteArray::number(it->mode, 8)); | |
194 | repository->fastImport.write(" :", 2); | |
195 | repository->fastImport.write(QByteArray::number(it->mark)); | |
196 | repository->fastImport.write(" ", 1); | |
197 | repository->fastImport.write(it.key().toUtf8()); | |
198 | repository->fastImport.write("\n", 1); | |
199 | } | |
200 | ||
201 | repository->fastImport.write("\n"); | |
202 | ||
203 | while (repository->fastImport.bytesToWrite() && repository->fastImport.waitForBytesWritten()) { | |
204 | // nothing | |
205 | } | |
206 | } |