20after4 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/202665

Change subject: mwpatch - a simple patch queue using a directory tree
......................................................................

mwpatch - a simple patch queue using a directory tree

Bug: T95375
Change-Id: I39ac15e79cafb20a31ecd903b8348c68a9e65330
---
A multiversion/mwcli.php
A multiversion/mwpatch
2 files changed, 253 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/mediawiki-config 
refs/changes/65/202665/1

diff --git a/multiversion/mwcli.php b/multiversion/mwcli.php
new file mode 100644
index 0000000..09328dd
--- /dev/null
+++ b/multiversion/mwcli.php
@@ -0,0 +1,107 @@
+<?php
+
+class MwCli {
+    protected $buffer = array();
+    protected $methods = array(
+
+    );
+
+    protected $commands;
+    protected $args = array();
+    protected $colors = array(
+        'black' => '0;30',
+        'dark_gray' => '1;30',
+        'blue' => '0;34',
+        'light_blue' => '1;34',
+        'green' => '0;32',
+        'light_green' => '1;32',
+        'cyan' => '0;36',
+        'light_cyan' => '1;36',
+        'red' => '0;31',
+        'light_red' => '1;31',
+        'purple' => '0;35',
+        'light_purple' => '1;35',
+        'brown' => '0;33',
+        'yellow' => '1;33',
+        'light_gray' => '0;37',
+        'white' => '1;37'
+    );
+
+    function __construct() {
+        $methods = array();
+        $this->methods['color'] = array_keys($this->colors);
+        foreach ($this->methods as $method=>$aliases) {
+            foreach($aliases as $alias) {
+                $methods[$alias]=$method;
+            }
+        }
+        $this->methods = $methods;
+    }
+
+    function cmd($args) {
+        $this->args = $args;
+        if (isset($args[1])) {
+            $cmd_method = "cmd_".$args[1];
+            if (method_exists($this, $cmd_method)) {
+                $method = new ReflectionMethod(get_class($this), $cmd_method);
+                if ($method->isPublic()) {
+                    $args = array_slice($args,2);
+                    $method->invokeArgs($this, $args);
+                }
+            }
+        }
+        return $this;
+    }
+
+    function txt($msg) {
+        $this->buffer[] = $msg;
+        return $this;
+    }
+
+    function color($color, $msg="") {
+        $this->buffer[] = "\033[".$this->colors[$color]."m";
+        if ($msg) {
+            $this->txt($msg)->endColor();;
+        }
+        return $this;
+    }
+
+    function endColor() {
+        $this->buffer[] = "\033[0m";
+        return $this;
+    }
+
+    function nl() {
+        $this->buffer[] = "\n";
+        return $this;
+    }
+
+    function error($msg) {
+        return $this->color('red',$msg)->nl();
+    }
+
+    function __call($name, $args) {
+        if (isset($this->methods[$name])) {
+            $method = $this->methods[$name];
+            array_unshift($args, $name);
+            return call_user_func_array(array($this, $method), $args);
+        }
+    }
+
+    function output() {
+        foreach($this->buffer as $element) {
+            echo $element;
+        }
+        $this->buffer = array();
+        return $this;
+    }
+    function record($rec, $pad=" ") {
+        $this->txt(join($pad, $rec));
+        return $this;
+    }
+    function __toString() {
+        $out = join("",$this->buffer);
+        $this->buffer = array();
+        return $out;
+    }
+}
diff --git a/multiversion/mwpatch b/multiversion/mwpatch
new file mode 100755
index 0000000..6189769
--- /dev/null
+++ b/multiversion/mwpatch
@@ -0,0 +1,146 @@
+#!/usr/bin/env php
+<?php
+require_once "mwcli.php";
+
+class MWPatch extends MwCli {
+    function __construct() {
+        parent::__construct();
+        $base = '/srv/patches';
+        $this->base_dir = $base;
+        $this->hash_dir = "$base/.hash";
+        $this->seq_file = "$base/.seq";
+    }
+    static function find_task($file) {
+        foreach ($file as $line) {
+            $line = trim($line);
+            $pos = strpos($line,'Bug: T');
+            if ($pos > -1) {
+                return trim(substr($line, $pos+5));
+            }
+        }
+        return "T0";
+    }
+
+    public function cmd_getsequence() {
+        $seq = $this->get_sequence(false);
+        return $this->white("Current sequence #")->light_blue($seq)->nl();
+    }
+
+    function get_sequence($increment=true) {
+        $seq = intval(file_get_contents($this->seq_file));
+        if ($increment) {
+            $seq = intval($seq)+1;
+            file_put_contents($this->seq_file, $seq);
+        }
+        return str_pad($seq, 4, "0", STR_PAD_LEFT);
+    }
+
+    public function cmd_list() {
+        passthru('ls /srv/patches');
+    }
+
+    public function cmd_add($file, $submodule_path) {
+        $content = file_get_contents($file);
+        $lines = explode("\n", $content);
+        $hash = hash('sha256', $content);
+
+        if (file_exists("$this->hash_dir/$hash")) {
+            $name = file_get_contents("$this->hash_dir/$hash");
+            return $this->error("Patch already added: ".$name);
+        }
+        $task = self::find_task($lines);
+        $module = $submodule_path;
+        $seq = self::get_sequence();
+        $rec = array('+', $seq, $task, $module, $file);
+        $this->green()->record($rec)->endColor()->nl();
+        $basename = basename($file);
+        $file = escapeshellarg($file);
+        $submod = str_replace('/','_', $submodule_path);
+        $name = "$seq-$hash-$task-$submod-$basename";
+        file_put_contents("$this->hash_dir/$hash", $name);
+        passthru("cp $file $this->base_dir/$name");
+    }
+
+    function find_file($pattern) {
+        $match = explode("\n",trim(shell_exec("ls $pattern"."*")));
+        return count($match) == 1 ? $match[0] : false;
+    }
+
+    function find_patch($pattern) {
+        if ($match = self::find_file("$this->hash_dir/$pattern")) {
+            return array(
+                'hash' => basename($match),
+                'file' => file_get_contents($match)
+            );
+        }
+        if ($match = self::find_patch("$this->base_dir/$pattern")) {
+            return array(
+                'hash' => hash('sha256',file_get_contents($match)),
+                'file' => basename($match)
+            );
+        }
+        return false;
+    }
+
+    public function cat($pattern) {
+        if ($patch = $this->find_patch($pattern)) {
+            echo file_get_contents($this->base_dir.'/'.$patch['file']);
+        } else {
+
+        }
+    }
+
+    public function cmd_remove($pattern) {
+        if ($patch = $this->find_patch($pattern)) {
+
+            $this->yellow("Removing $file");
+            $hash_file = $this->hash_dir.'/'.$patch['hash'];
+            $patch_file = $this->base_dir.'/'.$patch['file'];
+
+            if (!unlink($hash_file)){
+                return $this->error("Unable to remove $hash_file");
+            }
+            if (!unlink($patch_file)) {
+                return $this->error("Unable to remove $patch_file");
+            }
+            return $this->nl();
+        } else {
+            return err_nomatch($pattern);
+        }
+    }
+
+    function err_nomatch($pattern) {
+        return $this->error("There isn't a patch matching 
")->yellow("$pattern")->nl();
+    }
+
+    public function cmd_apply($pattern="all") {
+        if ($pattern == 'all') {
+            $cmd = "ls $this->base_dir -1";
+            $files = explode("\n",trim(shell_exec($cmd)));
+        } else {
+            if ($patch = $this->find_patch($pattern)) {
+                $files = array($patch['file']);
+            }
+        }
+
+        if (!$files || !count($files)) {
+            return err_nomatch($pattern);
+        }
+        foreach ($files as $file ) {
+            $apply_command = "git am -3 -k -q < $this->base_dir/$file";
+            $this->green("Applying $file")->nl()
+            ->light_gray($apply_command)
+            ->nl()->light_blue()->output();
+            passthru($apply_command, $err);
+            $this->endColor();
+            if ($err) {
+                return $this->error('Error applying patch ')->yellow($file);
+            }
+        }
+        return $this;
+    }
+}
+
+$cli = new MWPatch();
+$cli->cmd($argv)->output();
+

-- 
To view, visit https://gerrit.wikimedia.org/r/202665
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I39ac15e79cafb20a31ecd903b8348c68a9e65330
Gerrit-PatchSet: 1
Gerrit-Project: operations/mediawiki-config
Gerrit-Branch: master
Gerrit-Owner: 20after4 <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to