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