| 1 | package SVN::Rami; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.10.1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | #use File::Spec; # Will use later for Windows file names. | ||||||||||
| 9 | #use Path::Class; # Will use later for Windows file names. | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | SVN::Rami - Automates merging to multiple branches | ||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | Version 0.1 | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | our $VERSION = '0.1'; | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | Should be invoked from the command line: | ||||||||||
| 28 | |||||||||||
| 29 | perl -e "use SVN::Rami;" -c <version-number> | ||||||||||
| 30 | |||||||||||
| 31 | This is version 0.1, still mainly a proof-of-concept. | ||||||||||
| 32 | |||||||||||
| 33 | =head1 SUBROUTINES/METHODS | ||||||||||
| 34 | |||||||||||
| 35 | =head2 No publicly available subroutines at this time | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | # | ||||||||||
| 40 | #Reads a two-column CSV file and converts it to key-value pairs. | ||||||||||
| 41 | #For example, if the file contains one line consisting of "a,b", | ||||||||||
| 42 | #this method returns a mapping of "a" to "b". | ||||||||||
| 43 | #The file is assumed to have no headers. | ||||||||||
| 44 | # | ||||||||||
| 45 | #BUG: the file must be formatted precisely right: even an empty | ||||||||||
| 46 | #line in the middle of the file will result in an incorrect mapping. | ||||||||||
| 47 | # | ||||||||||
| 48 | #TODO: use Text::CSV instead. | ||||||||||
| 49 | # | ||||||||||
| 50 | sub load_csv_as_map { | ||||||||||
| 51 | 0 | ▶ | my $filename = shift; | ||||||||
|
Subroutine
load_csv_as_map: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 52 | 0 | local $/ = undef; # Slurp mode | |||||||||
| 53 | 0 | ▶ | open(my $handle, '<', $filename) or die "Could not read $filename\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $_ = <$handle>; | |||||||||
| 55 | 0 | close $handle; | |||||||||
| 56 | 0 | my @contents = split( /,|\R/ ); | |||||||||
| 57 | |||||||||||
| 58 | # Hack: the first two items are column headings. | ||||||||||
| 59 | 0 | (undef, undef, my @result) = @contents; | |||||||||
| 60 | 0 | return @result; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =head2 function2 | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | ▶ | sub function2 { | |||||||||
|
Subroutine
function2: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | =head1 AUTHOR | ||||||||||
| 71 | |||||||||||
| 72 | Dan Richter, C<< <dan.richter at trdpnt.com> >> | ||||||||||
| 73 | |||||||||||
| 74 | =head1 BUGS | ||||||||||
| 75 | |||||||||||
| 76 | Please report any bugs or feature requests to C<bug-svn-rami at rt.cpan.org>, or through | ||||||||||
| 77 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=SVN-Rami>. I will be notified, and then you'll | ||||||||||
| 78 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | =head1 SUPPORT | ||||||||||
| 84 | |||||||||||
| 85 | You can find documentation for this module with the perldoc command. | ||||||||||
| 86 | |||||||||||
| 87 | perldoc SVN::Rami | ||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | You can also look for information at: | ||||||||||
| 91 | |||||||||||
| 92 | =over 4 | ||||||||||
| 93 | |||||||||||
| 94 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 95 | |||||||||||
| 96 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=SVN-Rami> | ||||||||||
| 97 | |||||||||||
| 98 | =item * CPAN Ratings | ||||||||||
| 99 | |||||||||||
| 100 | L<https://cpanratings.perl.org/d/SVN-Rami> | ||||||||||
| 101 | |||||||||||
| 102 | =item * Search CPAN | ||||||||||
| 103 | |||||||||||
| 104 | L<https://metacpan.org/release/SVN-Rami> | ||||||||||
| 105 | |||||||||||
| 106 | =back | ||||||||||
| 107 | |||||||||||
| 108 | =head1 SEE ALSO | ||||||||||
| 109 | |||||||||||
| 110 | SVK::Merge | ||||||||||
| 111 | |||||||||||
| 112 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 113 | |||||||||||
| 114 | This software is copyright (c) 2023 by Dan Richter. | ||||||||||
| 115 | |||||||||||
| 116 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 117 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | shift; # HACK: currently the first argument is always -c | ||||||||||
| 124 | my $source_revision = shift; | ||||||||||
| 125 | if ( ! $source_revision ) { | ||||||||||
| 126 | print "Usage: $0 -c <revision_number>\n"; | ||||||||||
| 127 | exit 1; | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | my $commit_message_file = 'c:\dev\430\message.txt'; | ||||||||||
| 132 | |||||||||||
| 133 | my $repo = 'default'; # TODO: support more than one repo. | ||||||||||
| 134 | my $rami_home = glob("~/.rami/repo/$repo"); # TODO: use File::HomeDir | ||||||||||
| 135 | my $conf_dir = "$rami_home/conf"; | ||||||||||
| 136 | |||||||||||
| 137 | die "Expected directory $conf_dir\n" unless -d $conf_dir; | ||||||||||
| 138 | |||||||||||
| 139 | my %branch_to_path_on_filesystem = load_csv_as_map("$conf_dir/paths.csv"); | ||||||||||
| 140 | |||||||||||
| 141 | # We need the list of branches to be in order. | ||||||||||
| 142 | my @branch_to_url_array = load_csv_as_map("$conf_dir/urls.csv"); | ||||||||||
| 143 | my %branch_to_url = @branch_to_url_array; | ||||||||||
| 144 | my @branches = grep( !/^http/, @branch_to_url_array); # HACK: remove URLs, leaving only versions, IN ORDER! | ||||||||||
| 145 | |||||||||||
| 146 | # | ||||||||||
| 147 | # This utility function simply dumps data to a file. | ||||||||||
| 148 | # Example: write_file('foo.txt', 'Hello world') creates a file | ||||||||||
| 149 | # named foo.txt whose complete contents are "Hello world". | ||||||||||
| 150 | # Note that you can pass a multi-line string as the second argument. | ||||||||||
| 151 | # | ||||||||||
| 152 | sub write_file { | ||||||||||
| 153 | 0 | ▶ | my $filename = shift; | ||||||||
|
Subroutine
write_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 154 | 0 | my $contents = shift; | |||||||||
| 155 | |||||||||||
| 156 | 0 | ▶ | open(my $handle, '>', $filename) or die "Could not write to $filename\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | print $handle $contents; | |||||||||
| 158 | 0 | close $handle; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | # | ||||||||||
| 162 | # Usage: find_revision($revision, %branch_to_url) | ||||||||||
| 163 | # Queries SVN for information about the revision. | ||||||||||
| 164 | # Returns a hash that maps 'branch' to the branch on which the revision | ||||||||||
| 165 | # was committed (e.g., 6.2) and that maps 'commit_message' to the | ||||||||||
| 166 | # comment the user wrote to explain the commit. | ||||||||||
| 167 | # | ||||||||||
| 168 | sub find_revision { | ||||||||||
| 169 | 0 | ▶ | my $rev = shift; | ||||||||
|
Subroutine
find_revision: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 170 | 0 | my %branch_to_url = @_; | |||||||||
| 171 | 0 | foreach my $branch (keys %branch_to_url) { | |||||||||
| 172 | 0 | my $url = $branch_to_url{$branch}; | |||||||||
| 173 | 0 | $_ = `svn log -c $rev $url`; | |||||||||
| 174 | |||||||||||
| 175 | # If the revision was NOT on $branch, then the output | ||||||||||
| 176 | # will be a single line of dashes: ---------- | ||||||||||
| 177 | # | ||||||||||
| 178 | # But if the revision WAS on $branch, then | ||||||||||
| 179 | # the output will be something like this: | ||||||||||
| 180 | # ------------------------------------------------------------------------ | ||||||||||
| 181 | # r84187 | bob.smith | 2023-03-27 10:53:40 -0400 (Mon, 27 Mar 2023) | 4 lines | ||||||||||
| 182 | # | ||||||||||
| 183 | # Fixed memory leak | ||||||||||
| 184 | # ------------------------------------------------------------------------ | ||||||||||
| 185 | 0 | ▶ | if ( m/^-----+\R* # match a bunch of dashes followed by a newline. | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | ^r.*?lines?\R+ # match line: r84187 | badelman | ... | 4 lines | ||||||||||
| 187 | ^(.*?)\R+ # Match the commit message. | ||||||||||
| 188 | ^-------+\R*\Z # Match a bunch of dashes followed by a newline. | ||||||||||
| 189 | /msx ) { | ||||||||||
| 190 | |||||||||||
| 191 | #if ( m/^-+\r?\n?.*?lines(\r?\n)+(.*?)\r?\n-+(\r?\n)*$/sx ) { | ||||||||||
| 192 | 0 | return ('branch'=>$branch, 'commit_message'=>$1 ); | |||||||||
| 193 | } | ||||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | # Revision not found. Return an empty hash. | ||||||||||
| 197 | 0 | return (); | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | my %revision_details = find_revision($source_revision, %branch_to_url); | ||||||||||
| 201 | if ( ! %revision_details ) { | ||||||||||
| 202 | die "Unable to find revision $source_revision\n"; | ||||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | my $source_branch = $revision_details{'branch'}; | ||||||||||
| 206 | # The revision comment. We will later append something like "merge r 123 from 2.2.2" | ||||||||||
| 207 | my $base_commit_message = $revision_details{'commit_message'}; | ||||||||||
| 208 | $base_commit_message =~ s/(\r\n\R\s)+$//g; # Remove trailing whitespace/newlines. | ||||||||||
| 209 | |||||||||||
| 210 | |||||||||||
| 211 | my $found_branch = 0; # Will be true when we loop to the source branch. | ||||||||||
| 212 | #my $previous_branch = ''; | ||||||||||
| 213 | foreach my $target_branch (@branches) { | ||||||||||
| 214 | if ($target_branch eq $source_branch) { | ||||||||||
| 215 | $found_branch = 1; | ||||||||||
| 216 | # Perhaps instead of if-else we could use "redo", which is similar to Java's "continue" | ||||||||||
| 217 | } elsif ( $found_branch ) { # If we found the branch last time... | ||||||||||
| 218 | print "------------------------ Merging r $source_revision from $source_branch to $target_branch\n"; | ||||||||||
| 219 | my $source_url = $branch_to_url{$source_branch}; | ||||||||||
| 220 | my $working_dir = $branch_to_path_on_filesystem{$target_branch}; | ||||||||||
| 221 | |||||||||||
| 222 | # Get our working directory to the correct revision. | ||||||||||
| 223 | chdir($working_dir); | ||||||||||
| 224 | system("svn revert -R ."); # or die "Failed to revert $working_dir"; | ||||||||||
| 225 | system("svn up"); # or die "Failed to update $working_dir"; | ||||||||||
| 226 | |||||||||||
| 227 | # Write the commit message to a file. | ||||||||||
| 228 | my $commit_message = "$base_commit_message (merge r $source_revision from $source_branch)"; | ||||||||||
| 229 | write_file($commit_message_file, $commit_message); | ||||||||||
| 230 | |||||||||||
| 231 | my $merge_command = "svn merge --accept postpone -c $source_revision $source_url ."; | ||||||||||
| 232 | print "$merge_command\n"; | ||||||||||
| 233 | my $output_from_merge = `$merge_command`; | ||||||||||
| 234 | print "$output_from_merge\n"; | ||||||||||
| 235 | if ($output_from_merge =~ /Summary of conflicts/) { # If there were merge conflicts | ||||||||||
| 236 | print "Failed to merge r $source_revision from $source_branch to $target_branch.\n"; | ||||||||||
| 237 | print "Merge conflicts in $working_dir\n"; | ||||||||||
| 238 | die; | ||||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | my $commit_command = "svn commit --file $commit_message_file"; | ||||||||||
| 242 | print "$commit_command\n"; | ||||||||||
| 243 | my $output_from_commit = `$commit_command`; | ||||||||||
| 244 | my $target_revision; | ||||||||||
| 245 | print "$output_from_commit\n"; | ||||||||||
| 246 | if ($output_from_commit =~ /Committed revision (\d+)\./) { | ||||||||||
| 247 | $target_revision = $1; | ||||||||||
| 248 | } else { | ||||||||||
| 249 | die "Failed to commit r $source_revision from $source_branch to $target_branch.\n"; | ||||||||||
| 250 | } | ||||||||||
| 251 | |||||||||||
| 252 | $source_branch = $target_branch; | ||||||||||
| 253 | $source_revision = $target_revision; | ||||||||||
| 254 | } else { | ||||||||||
| 255 | print "Skipping branch $target_branch because we don't need to merge it.\n"; | ||||||||||
| 256 | } | ||||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | if ( ! $found_branch ) { | ||||||||||
| 260 | die "Unrecognized branch $source_branch\n"; | ||||||||||
| 261 | } | ||||||||||
| 262 | |||||||||||
| 263 | |||||||||||
| 264 | 1; # End of SVN::Rami | ||||||||||