File: main.cpp

package info (click to toggle)
svn-all-fast-export 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 304 kB
  • sloc: cpp: 2,471; makefile: 22
file content (271 lines) | stat: -rw-r--r-- 9,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 *  Copyright (C) 2007  Thiago Macieira <thiago@kde.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QTextStream>
#include <QDebug>

#include <limits.h>
#include <stdio.h>

#include "CommandLineParser.h"
#include "ruleparser.h"
#include "repository.h"
#include "svn.h"

QHash<QByteArray, QByteArray> loadIdentityMapFile(const QString &fileName)
{
    QHash<QByteArray, QByteArray> result;
    if (fileName.isEmpty())
        return result;

    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        fprintf(stderr, "Could not open file %s: %s",
                qPrintable(fileName), qPrintable(file.errorString()));
        return result;
    }

    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        int comment_pos = line.indexOf('#');
        if (comment_pos != -1)
            line.truncate(comment_pos);
        line = line.trimmed();
        int space = line.indexOf(' ');
        if (space == -1)
            continue;           // invalid line

        // Support git-svn author files, too
        // - svn2git native:  loginname Joe User <user@example.com>
        // - git-svn:         loginname = Joe User <user@example.com>
        int rightspace = space;
        if (line.indexOf(" = ") == space)
            rightspace += 2;

        QByteArray realname = line.mid(rightspace).trimmed();
        line.truncate(space);
        result.insert(line, realname);
    };
    file.close();

    return result;
}

QSet<int> loadRevisionsFile( const QString &fileName, Svn &svn )
{
    QRegExp revint("(\\d+)\\s*(?:-\\s*(\\d+|HEAD))?");
    QSet<int> revisions;
    if(fileName.isEmpty())
        return revisions;

    QFile file(fileName);
    if( !file.open(QIODevice::ReadOnly)) {
        fprintf(stderr, "Could not open file %s: %s\n", qPrintable(fileName), qPrintable(file.errorString()));
        return revisions;
    }

    bool ok;
    while(!file.atEnd()) {
        QByteArray line = file.readLine().trimmed();
        revint.indexIn(line);
        if( revint.cap(2).isEmpty() ) {
            int rev = revint.cap(1).toInt(&ok);
            if(ok) {
                revisions.insert(rev);
            } else {
                fprintf(stderr, "Unable to convert %s to int, skipping revision.\n", qPrintable(QString(line)));
            }
        } else if( revint.captureCount() == 2 ) {
            int rev = revint.cap(1).toInt(&ok);
            if(!ok) {
                fprintf(stderr, "Unable to convert %s (%s) to int, skipping revisions.\n", qPrintable(revint.cap(1)), qPrintable(QString(line)));
                continue;
            }
            int lastrev = 0;
            if(revint.cap(2) == "HEAD") {
                lastrev = svn.youngestRevision();
                ok = true;
            } else {
                lastrev = revint.cap(2).toInt(&ok);
            }
            if(!ok) {
                fprintf(stderr, "Unable to convert %s (%s) to int, skipping revisions.\n", qPrintable(revint.cap(2)), qPrintable(QString(line)));
                continue;
            }
            for(; rev <= lastrev; ++rev )
                revisions.insert(rev);
        } else {
            fprintf(stderr, "Unable to convert %s to int, skipping revision.\n", qPrintable(QString(line)));
        }
    }
    file.close();
    return revisions;
}

static const CommandLineOption options[] = {
    {"--identity-map FILENAME", "provide map between svn username and email"},
    {"--revisions-file FILENAME", "provide a file with revision number that should be processed"},
    {"--rules FILENAME[,FILENAME]", "the rules file(s) that determines what goes where"},
    {"--add-metadata", "if passed, each git commit will have svn commit info"},
    {"--add-metadata-notes", "if passed, each git commit will have notes with svn commit info"},
    {"--resume-from revision", "start importing at svn revision number"},
    {"--max-rev revision", "stop importing at svn revision number"},
    {"--dry-run", "don't actually write anything"},
    {"--debug-rules", "print what rule is being used for each file"},
    {"--commit-interval NUMBER", "if passed the cache will be flushed to git every NUMBER of commits"},
    {"--stats", "after a run print some statistics about the rules"},
    {"--svn-branches", "Use the contents of SVN when creating branches, Note: SVN tags are branches as well"},
    {"-h, --help", "show help"},
    {"-v, --version", "show version"},
    CommandLineLastOption
};

int main(int argc, char **argv)
{
    printf("Invoked as:'");
    for(int i = 0; i < argc; ++i)
        printf(" %s", argv[i]);
    printf("'\n");
    CommandLineParser::init(argc, argv);
    CommandLineParser::addOptionDefinitions(options);
    Stats::init();
    CommandLineParser *args = CommandLineParser::instance();
    if(args->contains(QLatin1String("version"))) {
        printf("Git version: %s\n", VER);
        return 0;
    }
    if (args->contains(QLatin1String("help")) || args->arguments().count() != 1) {
        args->usage(QString(), "[Path to subversion repo]");
        return 0;
    }
    if (args->undefinedOptions().count()) {
        QTextStream out(stderr);
        out << "svn-all-fast-export failed: ";
        bool first = true;
        foreach (QString option, args->undefinedOptions()) {
            if (!first)
                out << "          : ";
            out << "unrecognized option or missing argument for; `" << option << "'" << endl;
            first = false;
        }
        return 10;
    }
    if (!args->contains("rules")) {
        QTextStream out(stderr);
        out << "svn-all-fast-export failed: please specify the rules using the 'rules' argument\n";
        return 11;
    }
    if (!args->contains("identity-map")) {
        QTextStream out(stderr);
        out << "WARNING; no identity-map specified, all commits will be without email address\n\n";
    }

    QCoreApplication app(argc, argv);
    // Load the configuration
    RulesList rulesList(args->optionArgument(QLatin1String("rules")));
    rulesList.load();

    int resume_from = args->optionArgument(QLatin1String("resume-from")).toInt();
    int max_rev = args->optionArgument(QLatin1String("max-rev")).toInt();

    // create the repository list
    QHash<QString, Repository *> repositories;

    int cutoff = resume_from ? resume_from : INT_MAX;
 retry:
    int min_rev = 1;
    foreach (Rules::Repository rule, rulesList.allRepositories()) {
        Repository *repo = new Repository(rule);
        if (!repo)
            return EXIT_FAILURE;
        repositories.insert(rule.name, repo);

        int repo_next = repo->setupIncremental(cutoff);

        /*
  * cutoff < resume_from => error exit eventually
  * repo_next == cutoff => probably truncated log
  */
        if (cutoff < resume_from && repo_next == cutoff)
            /*
      * Restore the log file so we fail the next time
      * svn2git is invoked with the same arguments
      */
            repo->restoreLog();

        if (cutoff < min_rev)
            /*
      * We've rewound before the last revision of some
      * repository that we've already seen.  Start over
      * from the beginning.  (since cutoff is decreasing,
      * we're sure we'll make forward progress eventually)
      */
            goto retry;

        if (min_rev < repo_next)
            min_rev = repo_next;
    }

    if (cutoff < resume_from) {
        qCritical() << "Cannot resume from" << resume_from
                    << "as there are errors in revision" << cutoff;
        return EXIT_FAILURE;
    }

    if (min_rev < resume_from)
        qDebug() << "skipping revisions" << min_rev << "to" << resume_from - 1 << "as requested";

    if (resume_from)
        min_rev = resume_from;

    Svn::initialize();
    Svn svn(args->arguments().first());
    svn.setMatchRules(rulesList.allMatchRules());
    svn.setRepositories(repositories);
    svn.setIdentityMap(loadIdentityMapFile(args->optionArgument("identity-map")));

    if (max_rev < 1)
        max_rev = svn.youngestRevision();

    bool errors = false;
    QSet<int> revisions = loadRevisionsFile(args->optionArgument(QLatin1String("revisions-file")), svn);
    const bool filerRevisions = !revisions.isEmpty();
    for (int i = min_rev; i <= max_rev; ++i) {
        if(filerRevisions) {
            if( !revisions.contains(i) ) {
                printf(".");
                continue;
            } else {
                printf("\n");
            }
        }
        if (!svn.exportRevision(i)) {
            errors = true;
            break;
        }
    }

    foreach (Repository *repo, repositories) {
        repo->finalizeTags();
        delete repo;
    }
    Stats::instance()->printStats();
    return errors ? EXIT_FAILURE : EXIT_SUCCESS;
}