]> andersk Git - svn-all-fast-export.git/blob - src/repository.cpp
Introduce the dry-run mode
[svn-all-fast-export.git] / src / repository.cpp
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"
19 #include <QTextStream>
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 }
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 #ifndef DRY_RUN
60         fastImport.start("git-fast-import", QStringList());
61 #else
62         fastImport.start("/bin/cat", QStringList());
63 #endif
64     }
65
66     return txn;
67 }
68
69 Repository::Transaction::~Transaction()
70 {
71 }
72
73 void Repository::Transaction::setAuthor(const QByteArray &a)
74 {
75     author = a;
76 }
77
78 void Repository::Transaction::setDateTime(uint dt)
79 {
80     datetime = dt;
81 }
82
83 void Repository::Transaction::setLog(const QByteArray &l)
84 {
85     log = l;
86 }
87
88 void Repository::Transaction::deleteFile(const QString &path)
89 {
90     deletedFiles.append(path);
91 }
92
93 QIODevice *Repository::Transaction::addFile(const QString &path, int mode, qint64 length)
94 {
95     FileProperties fp;
96     fp.mode = mode;
97     fp.mark = ++lastmark;
98
99 #ifndef DRY_RUN
100     repository->fastImport.write("blob\nmark :");
101     repository->fastImport.write(QByteArray::number(fp.mark));
102     repository->fastImport.write("\ndata ");
103     repository->fastImport.write(QByteArray::number(length));
104     repository->fastImport.write("\n", 1);
105     repository->fastImport.waitForBytesWritten(0);
106 #endif
107
108     modifiedFiles.insert(path, fp);
109     return &repository->fastImport;
110 }
111
112 void Repository::Transaction::commit()
113 {
114     // create the commit message
115     QByteArray message = log;
116     if (!message.endsWith('\n'))
117         message += '\n';
118     message += "\nsvn=" + svnprefix + "; revision=" + QByteArray::number(revnum) + "\n";
119
120     {
121         QByteArray branchRef = branch;
122         if (!branchRef.startsWith("refs/heads/"))
123             branchRef.prepend("refs/heads/");
124
125         QTextStream s(&repository->fastImport);
126         s << "commit " << branchRef << endl;
127         s << "mark :" << revnum << endl;
128         s << "committer " << author << ' ' << datetime << "-0000" << endl;
129
130         Branch &br = repository->branches[branch];
131         if (!br.isCreated) {
132             br.isCreated = true;
133             s << "from " << br.branchFrom << endl;
134         }
135
136         s << "data " << message.length() << endl;
137     }
138
139     repository->fastImport.write(message);
140
141     // write the file deletions
142     foreach (QString df, deletedFiles)
143         repository->fastImport.write("D " + df.toUtf8() + "\n");
144
145     // write the file modifications
146     QHash<QString, FileProperties>::ConstIterator it = modifiedFiles.constBegin();
147     for ( ; it != modifiedFiles.constEnd(); ++it) {
148         repository->fastImport.write("M ", 2);
149         repository->fastImport.write(QByteArray::number(it->mode, 8));
150         repository->fastImport.write(" :", 2);
151         repository->fastImport.write(QByteArray::number(it->mark));
152         repository->fastImport.write(" ", 1);
153         repository->fastImport.write(it.key().toUtf8());
154         repository->fastImport.write("\n", 1);
155     }
156
157     repository->fastImport.write("\n");
158
159     while (repository->fastImport.bytesToWrite() && repository->fastImport.waitForBytesWritten()) {
160         // nothing
161     }
162 }
This page took 0.05545 seconds and 5 git commands to generate.