git-remote-mediawiki: put long code into a subroutine
[gitweb.git] / contrib / mw-to-git / git-remote-mediawiki.perl
index d1cddabd69bd5a619c82ac0ecf3a30c35a548c8a..bc31ba49fcb862d1f11d5284f332d4eef17cb289 100755 (executable)
@@ -22,7 +22,6 @@
 binmode STDOUT, ":encoding(UTF-8)";
 
 use URI::Escape;
-use IPC::Open2;
 
 # Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced
 use constant SLASH_REPLACEMENT => "%2F";
 $wiki_name =~ s/^.*@//;
 
 # Commands parser
-my @cmd;
 while (<STDIN>) {
        chomp;
-       @cmd = split(/ /);
-       if (defined($cmd[0])) {
-               # Line not blank
-               if ($cmd[0] eq "capabilities") {
-                       die("Too many arguments for capabilities\n") if (defined($cmd[1]));
-                       mw_capabilities();
-               } elsif ($cmd[0] eq "list") {
-                       die("Too many arguments for list\n") if (defined($cmd[2]));
-                       mw_list($cmd[1]);
-               } elsif ($cmd[0] eq "import") {
-                       die("Invalid arguments for import\n") if ($cmd[1] eq "" || defined($cmd[2]));
-                       mw_import($cmd[1]);
-               } elsif ($cmd[0] eq "option") {
-                       die("Too many arguments for option\n") if ($cmd[1] eq "" || $cmd[2] eq "" || defined($cmd[3]));
-                       mw_option($cmd[1],$cmd[2]);
-               } elsif ($cmd[0] eq "push") {
-                       mw_push($cmd[1]);
-               } else {
-                       print STDERR "Unknown command. Aborting...\n";
-                       last;
-               }
-       } else {
-               # blank line: we should terminate
+
+       if (!parse_command($_)) {
                last;
        }
 
 
 ########################## Functions ##############################
 
+sub parse_command {
+       my ($line) = @_;
+       my @cmd = split(/ /, $line);
+       if (!defined $cmd[0]) {
+               return 0;
+       }
+       if ($cmd[0] eq "capabilities") {
+               die("Too many arguments for capabilities\n")
+                   if (defined($cmd[1]));
+               mw_capabilities();
+       } elsif ($cmd[0] eq "list") {
+               die("Too many arguments for list\n") if (defined($cmd[2]));
+               mw_list($cmd[1]);
+       } elsif ($cmd[0] eq "import") {
+               die("Invalid arguments for import\n")
+                   if ($cmd[1] eq "" || defined($cmd[2]));
+               mw_import($cmd[1]);
+       } elsif ($cmd[0] eq "option") {
+               die("Too many arguments for option\n")
+                   if ($cmd[1] eq "" || $cmd[2] eq "" || defined($cmd[3]));
+               mw_option($cmd[1],$cmd[2]);
+       } elsif ($cmd[0] eq "push") {
+               mw_push($cmd[1]);
+       } else {
+               print STDERR "Unknown command. Aborting...\n";
+               return 0;
+       }
+       return 1;
+}
+
 # MediaWiki API instance, created lazily.
 my $mediawiki;
 
@@ -337,7 +344,8 @@ sub get_mw_pages {
 sub run_git {
        my $args = shift;
        my $encoding = (shift || "encoding(UTF-8)");
-       open(my $git, "-|:$encoding", "git " . $args);
+       open(my $git, "-|:$encoding", "git " . $args)
+           or die "Unable to open: $!\n";
        my $res = do {
                local $/ = undef;
                <$git>