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     function commit( $CS)
60     {
61          
62         if ($CS->when) {
63             $d = strtotime($CS->when);
64             putenv("GIT_AUTHOR_DATE=$d -0000");
65         } else {
66             putenv("GIT_AUTHOR_DATE=");
67         }
68         
69         $reason = trim($CS->reason);
70         if (!strlen($reason)) {
71             $reason = 'Changed';
72         }
73         
74         putenv("GIT_AUTHOR_NAME=$CS->name");
75         putenv("GIT_AUTHOR_EMAIL=$CS->email");
76         return stream_get_contents($this->git('commit', '-a', '-m', $reason )  );
77     }
78   
79     function git()
80     {
81         $args = func_get_args();
82         $a = array("--git-dir=$this->dir/.git", "--work-tree=$this->dir");
83         foreach ($args as $arg) {
84           $a[] = $arg;
85         }
86         //print_r($a);
87         return MTrackSCM::run('git', 'read', $a);
88     }
89 }