Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1293 dpurdie 1
###############################################################################
2
# Codestriker: Copyright (c) 2001,2002,2003 David Sitsky.  All rights reserved.
3
# sits@users.sourceforge.net
4
#
5
# This program is free software; you can redistribute it and modify it under
6
# the terms of the GPL.
7
 
8
# Subversion repository access package.
9
 
10
package Codestriker::Repository::Subversion;
11
use IPC::Open3;
12
 
13
use strict;
14
use Fatal qw / open close /;
15
 
16
# Constructor, which takes as a parameter the repository url.
17
sub new {
18
    my ($type, $repository_url, $user, $password) = @_;
19
    # Determine if there are additional parameters required for user
20
    # authentication.
1302 dpurdie 21
 
1293 dpurdie 22
    my @userCmdLine = ();
23
    if (defined($user) && defined($password)) {
24
        push @userCmdLine, '--username';
1299 dpurdie 25
        push @userCmdLine, $user;
26
        push @userCmdLine, '--password';
27
        push @userCmdLine, $password;
1293 dpurdie 28
    }
29
 
1302 dpurdie 30
    my $repotag = '';
31
    if ( $repository_url =~ m~(.*)\[(.*)\]$~ )
32
    {
33
        $repository_url = $1;
34
        $repotag = $2;
35
    }
36
 
1293 dpurdie 37
    # Sanitise the repository URL.
38
    $repository_url = sanitise_url_component($repository_url);
39
 
40
    my $self = {};
41
    $self->{repository_url} = $repository_url;
1302 dpurdie 42
    $self->{repotag} = $repotag;
1293 dpurdie 43
    $self->{userCmdLine} = \@userCmdLine;
44
    $self->{repository_string} = $repository_url;
45
    $self->{repository_string} .= ";$user" if defined $user;
46
    $self->{repository_string} .= ";$password" if defined $password;
47
    if ($self->{repository_string} !~ /^svn:/) {
1299 dpurdie 48
        $self->{repository_string} = "svn:" . $self->{repository_string};
1293 dpurdie 49
    }
50
 
51
    bless $self, $type;
52
}
53
 
54
# Sanitise a Subversion URL component, by replacing spaces with %20 and @
55
# symbols with %40, so that there is no confused with pegged revisions.  Also
56
# remove any leading and trailing slashes.
57
sub sanitise_url_component {
58
    my $url = shift;
59
    $url =~ s/\/$//;
60
    $url =~ s/^\///;
61
    $url =~ s/ /%20/g;
62
    $url =~ s/\@/%40/g;
63
    return $url;
64
}
65
 
66
# Retrieve the data corresponding to $filename and $revision.  Store each line
67
# into $content_array_ref.
68
sub retrieve ($$$\$) {
69
    my ($self, $filename, $revision, $content_array_ref) = @_;
70
 
71
    # Sanitise the filename.
72
    $filename = sanitise_url_component($filename);
73
 
74
    my $read_data;
75
    my $read_stdout_fh = new FileHandle;
76
    open($read_stdout_fh, '>', \$read_data);
77
    my @args = ();
78
    push @args, 'cat';
79
    push @args, '--non-interactive';
80
    push @args, '--no-auth-cache';
1299 dpurdie 81
    push @args, '--trust-server-cert';
1293 dpurdie 82
    push @args, @{ $self->{userCmdLine} };
83
    push @args, '--revision';
84
    push @args, $revision;
85
    push @args, $self->{repository_url} . '/' . $filename;
86
    Codestriker::execute_command($read_stdout_fh, undef,
1299 dpurdie 87
                                 $Codestriker::svn, @args);
1293 dpurdie 88
 
89
    # Process the data for the topic.
90
    open($read_stdout_fh, '<', \$read_data);
91
    for (my $i = 1; <$read_stdout_fh>; $i++) {
1299 dpurdie 92
        $_ = Codestriker::decode_topic_text($_);
93
        chop;
94
        $$content_array_ref[$i] = $_;
1293 dpurdie 95
    }
96
}
97
 
98
# Retrieve the "root" of this repository.
99
sub getRoot ($) {
100
    my ($self) = @_;
101
    return $self->{repository_url};
102
}
103
 
104
# Return a URL which views the specified file and revision.
105
sub getViewUrl ($$$) {
106
    my ($self, $filename, $revision) = @_;
107
 
108
    # Lookup the file viewer from the configuration.
109
    my $viewer = $Codestriker::file_viewer->{$self->toString()};
110
    if (! (defined $viewer)) {
1299 dpurdie 111
        $viewer = $Codestriker::file_viewer->{$self->{repository_string}};
1293 dpurdie 112
    }
113
 
114
    return (defined $viewer) ? $viewer . "/" . $filename : "";
115
}
116
 
117
# Return a string representation of this repository.
118
sub toString ($) {
119
    my ($self) = @_;
120
    return "svn:" . $self->getRoot();
121
}
122
 
123
# Given a Subversion URL, determine if it refers to a directory or a file.
124
sub is_file_url {
125
    my ($self, $url) = @_;
126
    my $file_url;
127
 
128
    eval {
1299 dpurdie 129
        my @args = ();
130
        push @args, 'info';
131
        push @args, '--non-interactive';
132
        push @args, '--no-auth-cache';
133
        push @args, '--trust-server-cert';
134
        push @args, @{ $self->{userCmdLine} };
135
        push @args, '--xml';
136
        push @args, $self->{repository_url} . '/' . $url;
137
        my $read_data;
138
        my $read_stdout_fh = new FileHandle;
139
        open($read_stdout_fh, '>', \$read_data);
1293 dpurdie 140
 
1299 dpurdie 141
        Codestriker::execute_command($read_stdout_fh, undef,
142
                                     $Codestriker::svn, @args);
143
        open($read_stdout_fh, '<', \$read_data);
144
        while (<$read_stdout_fh>) {
145
            if (/kind\s*\=\s*\"(\w+)\"/) {
146
                $file_url = $1 eq "File";
147
                last;
148
            }
149
        }
1293 dpurdie 150
    };
151
    if ($@ || !(defined $file_url)) {
1299 dpurdie 152
        # The above command failed, try using the older method which only works
153
        # in an English locale.  This supports Subversion 1.2 or earlier
154
        # releases, which don't support the --xml flag for the info command.
155
        my @args = ();
156
        push @args, 'cat';
157
        push @args, '--non-interactive';
158
        push @args, '--no-auth-cache';
159
        push @args, '--trust-server-cert';
160
        push @args, @{ $self->{userCmdLine} };
161
        push @args, '--revision';
162
        push @args, 'HEAD';
163
        push @args, $self->{repository_url} . '/' . $url;
1293 dpurdie 164
 
1299 dpurdie 165
        my $read_stdout_data;
166
        my $read_stdout_fh = new FileHandle;
167
        open($read_stdout_fh, '>', \$read_stdout_data);
1293 dpurdie 168
 
1299 dpurdie 169
        my $read_stderr_data;
170
        my $read_stderr_fh = new FileHandle;
171
        open($read_stderr_fh, '>', \$read_stderr_data);
1293 dpurdie 172
 
1299 dpurdie 173
        Codestriker::execute_command($read_stdout_fh, $read_stderr_fh,
174
                                     $Codestriker::svn, @args);
175
        $file_url = 1;
176
        open($read_stderr_fh, '<', \$read_stderr_data);
177
        while(<$read_stderr_fh>) {
178
            if (/^svn:.* refers to a directory/) {
179
                $file_url = 0;
180
                last;
181
            }
182
        }
1293 dpurdie 183
    }
184
 
185
    return $file_url;
186
}
187
 
188
# The getDiff operation, pull out a change set based on the start and end 
189
# revision number, confined to the specified moduled_name.
190
sub getDiff {
191
    my ($self, $start_tag, $end_tag, $module_name, $stdout_fh, $stderr_fh) = @_;
192
 
1300 dpurdie 193
 
194
    #
1302 dpurdie 195
    #   If modulename conatins the repotag - then remove it
196
    #
197
    if ( $self->{repotag} )
198
    {
199
        if ( $module_name =~ m~$self->{repotag}/(.*)~ )
200
        {
201
            $module_name = $1;
202
        }
203
    }
204
 
205
    #
1300 dpurdie 206
    #   Check Module Name
207
    #
208
    my @errors;
209
    $self->{getDiffError} = undef;
210
    unless ( $module_name =~ m~/(tags|branches|trunk)(/|$)~ )
211
    {
212
        push @errors,
213
            "Module does not contain 'tags', 'trunk' or 'branches'",
214
            "Module must specify a development branch or trunk";
215
    }
216
 
217
    if ( $module_name =~ m~^/~ )
218
    {
219
        push @errors, "Module must not start with a '/'"
220
    }
221
 
222
 
223
    if ( @errors )
224
    {
225
        $self->{getDiffError} = join ('<br>', @errors);
226
        return $Codestriker::OK;
227
    }
228
 
229
 
1293 dpurdie 230
    # Sanitise the URL, and determine if it refers to a directory or filename.
231
    $module_name = sanitise_url_component($module_name);
232
    my $directory;
233
    if ($self->is_file_url($module_name)) {
1299 dpurdie 234
        $module_name =~ /(.*)\/[^\/]+/;
235
        $directory = $1;
1293 dpurdie 236
    } else {
1299 dpurdie 237
        $directory = $module_name;
1293 dpurdie 238
    }
239
 
240
    # Execute the diff command.
241
    my $read_stdout_data;
242
    my $read_stdout_fh = new FileHandle;
243
    open($read_stdout_fh, '>', \$read_stdout_data);
244
 
1300 dpurdie 245
    #
246
    #   Determine the commad to use
247
    #   If user provides two numbers, then module is a full path
248
    #   Otherwise assume that the user has provided named tags
249
    #
250
    #
1293 dpurdie 251
    my @args = ();
252
    push @args, 'diff';
253
    push @args, '--non-interactive';
254
    push @args, '--no-auth-cache';
1299 dpurdie 255
    push @args, '--trust-server-cert';
1293 dpurdie 256
    push @args, @{ $self->{userCmdLine} };
1300 dpurdie 257
 
258
    if ( $start_tag =~ m~^\d+$~ )
259
    {
260
        my $rtag = $start_tag;
261
        $rtag .= (':' . $end_tag) if $end_tag;
262
        push @args, '-r';
263
        push @args, $rtag;
264
        push @args, '--old';
265
        push @args, $self->{repository_url};
266
        push @args, $module_name;
267
    }
268
    else
269
    {
270
        my $clean_module_name = $module_name;
271
        $clean_module_name =~ s~/trunk$~~;
272
        $clean_module_name =~ s~/branches/.*~~;
273
        $clean_module_name =~ s~/tags/.*~~;
274
        push @args, '--old';
275
        push @args, join ('/', $self->{repository_url}, $clean_module_name, 'tags', $start_tag);
276
 
277
        if ( $end_tag )
278
        {
279
            push @args, '--new';
280
            push @args, join ('/', $self->{repository_url}, $clean_module_name, 'tags', $end_tag);
281
        }
282
        else
283
        {
284
            push @args, '--new';
285
            push @args, join ('/', $self->{repository_url}, $module_name);
286
        }
287
    }
1293 dpurdie 288
    Codestriker::execute_command($read_stdout_fh, $stderr_fh,
1299 dpurdie 289
                                 $Codestriker::svn, @args);
1293 dpurdie 290
 
1300 dpurdie 291
    my $rv = open($read_stdout_fh, '<', \$read_stdout_data);
292
    if ( $rv ) {
293
        while(<$read_stdout_fh>) {
294
            my $line = $_;
1299 dpurdie 295
 
1300 dpurdie 296
            # If the user specifies a path (a branch in Subversion), the
297
            # diff file does not come back with a path rooted from the
298
            # repository base making it impossible to pull the entire file
299
            # back out. This code attempts to change the diff file on the
300
            # fly to ensure that the full path is present. This is a bug
301
            # against Subversion, so eventually it will be fixed, so this
302
            # code can't break when the diff command starts returning the
303
            # full path.
304
            if ($line =~ /^--- / || $line =~ /^\+\+\+ / ||
305
                $line =~ /^Index: /) {
306
                # Check if the bug has been fixed.
307
                if ($line =~ /^\+\+\+ $module_name/ == 0 && 
308
                    $line =~ /^--- $module_name/ == 0 &&
309
                    $line =~ /^Index: $module_name/ == 0) {
310
                        $line =~ s/^--- /--- $directory\// or
311
                        $line =~ s/^Index: /Index: $directory\// or
312
                        $line =~ s/^\+\+\+ /\+\+\+ $directory\//;
313
                }
1299 dpurdie 314
            }
1300 dpurdie 315
 
316
            print $stdout_fh $line;
1299 dpurdie 317
        }
1293 dpurdie 318
    }
319
 
320
    return $Codestriker::OK;
321
}
322
 
323
1;