MTrack/SCM/Git/WorkingCopy.php
[web.mtrack] / MTrack / SCM / Git / WorkingCopy.php
1 <?php
2
3 /***
4  *
5  * working directories?
6  *
7  * This is very dodgy..
8  * = either we have a temporary working directory created for each session/user etc..
9  *
10  * = what happens when multiple people try to access a working directory..
11  *
12  * = what happens if the same person is doing multiple things on the same workign directory..
13  *
14  * 
15  *
16  *
17  */
18 require_once 'MTrack/SCM/WorkingCopy.php';
19
20  
21 class MTrack_SCM_Git_WorkingCopy extends MTrack_SCM_WorkingCopy
22 {
23     private $repo;
24     public $push = false; /// 
25   
26     function __construct(MTrack_Repo $repo) {
27         
28         $this->dir = $this->generateTempDir();;
29         
30         $this->repo = $repo;
31         
32         MTrackSCM::run('git', 'string',
33             array('clone', $this->repo->repopath, $this->dir)
34         );
35     }
36     
37     function push()
38     {
39         return stream_get_contents($this->git('push'));
40     }
41     
42     
43   
44     function getFile($path)
45     {
46          return $this->repo->file($path);
47     }
48   
49     function addFile($path)
50     {
51          $this->git('add', $path);
52     }
53   
54     function delFile($path)
55     {
56          $this->git('rm', '-f', $path);
57     }
58     /**
59      * @param {StdClass} $CS
60      *   ->when (optional)
61      *   ->reason (optional)???
62      *   ->name (required)
63      *   ->email (required)
64      */
65   
66     function commit( $CS)
67     {
68          
69         if ($CS->when) {
70             $d = strtotime($CS->when);
71             putenv("GIT_AUTHOR_DATE=$d -0000");
72         } else {
73             putenv("GIT_AUTHOR_DATE=");
74         }
75         
76         $reason = trim($CS->reason);
77         if (!strlen($reason)) {
78             $reason = 'Changed';
79         }
80          
81         return stream_get_contents(
82             $this->git('commit', '-a',
83                        '--author="' . $CS->name . ' <'. $CS->email . '>"' , '-m', $reason )
84         );
85     }
86   
87     function git()
88     {
89         $args = func_get_args();
90         $a = array("--git-dir=$this->dir/.git", "--work-tree=$this->dir");
91         foreach ($args as $arg) {
92           $a[] = $arg;
93         }
94          print_r($a);
95         return MTrackSCM::run('git', 'read', $a);
96     }
97 }