1<?php
2/**
3  @author  Alexander 'E-Razor' Krause <alexander.krause@erazor-zone.de>
4  @url     http://wiki.erazor-zone.de/wiki:projects:php:dokuwiki:plugins:darcs
5  @date    2006.03.19
6*/
7class Darcs_Repository {
8  var $patch_list=array();
9  var $patch_count=0;
10  var $darcs_bin='darcs';
11  var $last_ec;
12  var $last_cmd;
13  var $repos_dir;
14  var $stdout=array();
15
16  function set_repository($path) {
17    if (!is_dir($path)) {
18      $this->last_ec=-1;
19      return -1;
20    }
21    $this->repos_dir=$path;
22  }
23
24  function get_patches() {
25    $retval=0;
26    $this->stdout=array();
27    /*
28    $ darcs pull --dry-run --all -quiet
29    0:Would pull the following changes:
30    1:Sat Aug 27 17:48:13 UTC 2005  Andreas Gohr <andi@splitbrain.org>
31    2:  * index lookup function added
32    4:  description
33    5:
34    6:Making no changes:  this is a dry run.
35    */
36    chdir($this->repos_dir);
37    $this->last_cmd=$this->darcs_bin.' pull --dry-run --all --quiet 2>&1';
38    exec($this->last_cmd,&$this->stdout,&$this->retval);
39    reset($this->stdout);
40    next($this->stdout);
41    $this->patch_count=0;
42    $this->patch_list=array();
43
44    $stdoutStr=implode("\n",$this->stdout);
45    $myRegEx='/((\w+ \w+ [ 0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} \w{2,4} [0-9]{4})  (.+)\n'.
46             '(  \* (.+)\n)?'.
47	     '(  .+(\n  .+)*)?)+/';
48
49    preg_match_all($myRegEx,$stdoutStr,$found,PREG_SET_ORDER);
50    foreach($found as $item) {
51      $cPatch=&$this->patch_list[$this->patch_count];
52      $cPatch['date']=$item[2];
53      $cPatch['author']=$item[3];
54      $cPatch['name']=$item[5];
55      $cPatch['description']=$item[6];
56      $this->patch_count++;
57    }
58    return $this->retval;
59  }
60
61  function apply_patches() {
62    $this->stdout=array();
63
64    chdir($this->repos_dir);
65    $this->last_cmd=$this->darcs_bin.' pull --all --quiet 2>&1';
66    exec($this->last_cmd,&$this->stdout,&$this->retval);
67    return $this->retval;
68  }
69
70  function darcs_check($options='') {
71    $this->stdout=array();
72    chdir($this->repos_dir);
73    $this->last_cmd=$this->darcs_bin.' check  2>&1';
74    exec($this->last_cmd,&$this->stdout,&$this->retval);
75    return $this->retval;
76  }
77
78  function darcs_get($options='') {
79    $this->stdout=array();
80    chdir($this->repos_dir);
81    $this->last_cmd=$this->darcs_bin.' get '.$options.'  2>&1';
82    exec($this->last_cmd,&$this->stdout,&$this->retval);
83    return $this->retval;
84  }
85
86}
87