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