MTrack/SCM/Git/WorkingCopy.php
[web.mtrack] / MTrack / SCM / Git / WorkingCopy.php
1 <?php
2
3
4
5 class MTrack_SCM_Git_WorkingCopy extends MTrackSCMWorkingCopy
6 {
7     private $repo;
8     public $push = true;
9   
10     function __construct(MTrack_Repo $repo) {
11         $cfg = HTML_FlexyFramework::get()->MTrack;
12         if (empty($cfg['workingdir'])) {
13             throw new Exception("MTrack[workingdir] is not set")
14         }
15         $this->dir = $cfg['workingdir'];
16         $this->repo = $repo;
17         
18         MTrackSCM::run('git', 'string',
19             array('clone', $this->repo->repopath, $this->dir)
20         );
21     }
22   
23     function __destruct() {
24         if ($this->push) {
25           echo stream_get_contents($this->git('push'));
26         }
27         mtrack_rmdir($this->dir);
28     }
29   
30     function getFile($path)
31     {
32          return $this->repo->file($path);
33     }
34   
35     function addFile($path)
36     {
37          $this->git('add', $path);
38     }
39   
40     function delFile($path)
41     {
42          $this->git('rm', '-f', $path);
43     }
44   
45     function commit(MTrackChangeset $CS)
46     {
47         if ($CS->when) {
48           $d = strtotime($CS->when);
49           putenv("GIT_AUTHOR_DATE=$d -0000");
50         } else {
51           putenv("GIT_AUTHOR_DATE=");
52         }
53         $reason = trim($CS->reason);
54         if (!strlen($reason)) {
55           $reason = 'Changed';
56         }
57         putenv("GIT_AUTHOR_NAME=$CS->who");
58         putenv("GIT_AUTHOR_EMAIL=$CS->who");
59         stream_get_contents($this->git('commit', '-a',
60           '-m', $reason
61           )
62         );
63     }
64   
65     function git()
66     {
67         $args = func_get_args();
68         $a = array("--git-dir=$this->dir/.git", "--work-tree=$this->dir");
69         foreach ($args as $arg) {
70           $a[] = $arg;
71         }
72         print_r($a);
73         return MTrackSCM::run('git', 'read', $a);
74     }
75 }