Adding code, removing more code to reduce duplication

In a recent post, I said:

That second patch uses the same string you saw above in the previous patch. Yes, it would be better to have added that string to just one place. I welcome patches for that change.

Today I will show you that code. With help from Matthias Andree on IRC, I managed to figure this out.

Deleted code

This is what was deleted. Some of this is added code, but for the most part, things have been deleted.

===================================================================
--- process_mail.pl     (revision 4583)
+++ process_mail.pl     (working copy)
@@ -9,6 +9,7 @@
 #
 
 use strict;
+use branches;
 use XML::Writer;
 use constants;
 use utilities;
@@ -26,8 +27,6 @@
        
        my $Message_Subject = FreshPorts::ProcessMail::myGetMessage_Subject($message);
 
-
-       
        # the message id uniquely identifies the email, and thus, the commit in question
        # the message id should be in one of two forms (given that we are processing stuff from just two lists):
        #  201108100855.p7A8tkQt033487@svn.freebsd.org     (SVN commit)
@@ -54,29 +53,15 @@
 #      print 'List-Id: '    . $ListId          . "\n";
 #      print 'Subject: '    . $Message_Subject . "\n";
 
-       my $found = 0;  
-       if ($MessageId =~ /\@svn.freebsd.org/i || $MessageId =~ /\@svn.chruetertee.ch/i) {
-               if ($ListId =~ /SVN commit messages for the entire src tree/i                 ||
-                   $ListId =~ /SVN commit messages for the entire doc trees/i                ||
-                   $ListId =~ /SVN commit messages for the ports tree for head/i             ||
-                   $ListId =~ /SVN commit messages for all the branches of the ports tree/i) {
+       my $found = 0;
+        my $ListProperties = FreshPorts::Branches::ListProperties($ListId);
+        if (defined($ListProperties))
+        {
                        $found = 1;
-#                      print "we should invoke the SVN scripts here\n";
-                       eval "use process_svn_mail";
+            my $process = $ListProperties->{'process'};
+#            require "./$process.pm";
+            eval "use $process";
                }
-       }
-
-       if ($MessageId =~ /\@repoman.freebsd.org/i) {
-               if ($ListId =~ /CVS commit messages for the ports tree/i ||
-                   $ListId =~ /CVS commit messages for the doc and www trees/i ||
-                   $ListId =~ /\*\*OBSOLETE\*\* CVS commit messages for the entire tree/i ||
-                   $ListId =~ /\*\*OBSOLETE\*\* CVS commit messages for the src tree/i ||
-                   $ListId =~ /CVS commit messages for the projects tree/i) {
-                       $found = 1;
-#                      print "we should invoke the CVS scripts here\n";
-                       eval "use process_cvs_mail";
-               }
-       }
        
        if (!$found) {
                FreshPorts::Utilities::ReportErrorEmailNoPrint('err', "This List-Id/Message-Id combination is not known to this script. List-Id='" . 
Index: process_svn_mail.pm
===================================================================
--- process_svn_mail.pm (revision 4583)
+++ process_svn_mail.pm (working copy)
@@ -477,26 +477,17 @@
 #
 
        my ($message) = @_;
-       
+
        my $myRepoPrefix = '';
        my $myListId     = FreshPorts::ProcessMail::myGetList_Id($message);
 
-       my %KnownRepos = (
-               "SVN commit messages for the entire src tree"                => $FreshPorts::Config::Repo_SRC,
-               "SVN commit messages for the entire doc trees"               => $FreshPorts::Config::Repo_DOC,
-               "SVN commit messages for the ports tree for head"            => $FreshPorts::Config::Repo_PORTS,
-               "SVN commit messages for all the branches of the ports tree" => $FreshPorts::Config::Repo_PORTS
-       );
-       
-       while (my ($ListId, $repo) = each %KnownRepos)
+        my $ListProperties = FreshPorts::Branches::ListProperties($myListId);
+
+        if (defined($ListProperties))
        {
-               if ($myListId =~ /$ListId/i)
-               {
-                       $myRepoPrefix = $repo;
-                       last;
+                $myRepoPrefix = $ListProperties->{'repo'};
                }
-       }
-       
+
        return $myRepoPrefix;
 }
 

Some notes on the above:

  • Line 8 – includes the new module, which I will show later.
  • Lines 25-30 & 42-54 – deleted the old code, much of which formed the basis for the new module
  • lines 71-78 & 83-86 – delete more code

The new code is among that not highlighted. It’s pretty small.

New branches module

The new module is straight forward, and mostly data.

#!/usr/bin/perl
#
# Copyright (c) 2014 DVL Software
#

package FreshPorts::Branches;

require config;

# these are the branches we process
%FreshPorts::Branches::Branches = (
  'HEAD',
  'RELENG_9_1_0'
);

# these are the mailing lists associated with those branches
%FreshPorts::Branches::MailingLists = (
  'SVN commit messages for the entire src tree' => {
     'process' => 'process_svn_mail',
     'repo'    => $FreshPorts::Config::Repo_SRC,
     },
  'SVN commit messages for the entire doc trees' => {
     'process' => 'process_svn_mail',
     'repo'    => $FreshPorts::Config::Repo_DOC,
     },
  'SVN commit messages for the ports tree for head' => {
     'process' => 'process_svn_mail',
     'repo'    => $FreshPorts::Config::Repo_PORTS,
     },
  'SVN commit messages for all the branches of the ports tree' => {
    'process' => 'process_svn_mail',
    'repo'    => $FreshPorts::Config::Repo_PORTS,
     },
  'CVS commit messages for the ports tree' => {
     'process' => 'process_cvs_mail',
     'repo'    => '',
     },
  'CVS commit messages for the doc and www trees' => {
     'process' => 'process_cvs_mail',
     'repo'    => '',
     },
  '\*\*OBSOLETE\*\* CVS commit messages for the entire tree' => {
     'process' => 'process_cvs_mail',
     'repo'    => '',
     },
  '\*\*OBSOLETE\*\* CVS commit messages for the src tree' => {
     'process' => 'process_cvs_mail',
     'repo'    => '',
     },
  'CVS commit messages for the projects tree' => {
     'process' => 'process_cvs_mail',
     'repo'    => '',
     },
);

#
# given a list id, grab the properties for it
#
sub ListProperties($)
{
 my $ListId = shift;
 my $hash;

 # strip off the leading header. 
 $ListId =~ s/List-Id:\s+//;

 $hash = $FreshPorts::Branches::MailingLists{$ListId};
     
 return $hash;
}

1;

Lines 16-54 are derived from the deleted code and rearranged into one hash.

The new function, ListProperties() forms the basis of the new code added above.

The %FreshPorts::Branches::Branches variable is yet to be used in any code, but I can see it being useful very soon.

Thanks

My thanks to Matthias Andree and to Matthew Seaman for helping me with my poor Perl skills.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Comment

Scroll to Top