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         $this->dir = mtrack_make_temp_dir();
12         $this->repo = $repo;
13         
14         MTrackSCM::run('git', 'string',
15             array('clone', $this->repo->repopath, $this->dir)
16         );
17     }
18   
19     function __destruct() {
20         if ($this->push) {
21           echo stream_get_contents($this->git('push'));
22         }
23         mtrack_rmdir($this->dir);
24     }
25   
26     function getFile($path)
27     {
28          return $this->repo->file($path);
29     }
30   
31     function addFile($path)
32     {
33          $this->git('add', $path);
34     }
35   
36     function delFile($path)
37     {
38          $this->git('rm', '-f', $path);
39     }
40   
41     function commit(MTrackChangeset $CS)
42     {
43         if ($CS->when) {
44           $d = strtotime($CS->when);
45           putenv("GIT_AUTHOR_DATE=$d -0000");
46         } else {
47           putenv("GIT_AUTHOR_DATE=");
48         }
49         $reason = trim($CS->reason);
50         if (!strlen($reason)) {
51           $reason = 'Changed';
52         }
53         putenv("GIT_AUTHOR_NAME=$CS->who");
54         putenv("GIT_AUTHOR_EMAIL=$CS->who");
55         stream_get_contents($this->git('commit', '-a',
56           '-m', $reason
57           )
58         );
59     }
60   
61     function git()
62     {
63         $args = func_get_args();
64         $a = array("--git-dir=$this->dir/.git", "--work-tree=$this->dir");
65         foreach ($args as $arg) {
66           $a[] = $arg;
67         }
68         print_r($a);
69         return MTrackSCM::run('git', 'read', $a);
70     }
71 }