final move of files
[web.mtrack] / MTrack / SCM / Git / File.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 /* Git SCM browsing */
5 require_once 'MTrack/SCMFile.php';
6 require_once 'MTrack/SCMAnnotation.php';
7
8 class MTrack_SCM_Git_File extends MTrackSCMFile 
9 {
10     public $name;
11     public $rev;
12     public $is_dir;
13     public $repo;
14     public $hash;
15     
16     function __construct(MTrackSCM $repo, $name, $rev, $is_dir = false, $hash=false)
17     {
18         $this->repo = $repo;
19         $this->name = $name;
20         $this->rev = $rev;
21         $this->is_dir = $is_dir;
22         $this->hash = $hash;
23     }
24
25     public function getChangeEvent() // returns MTrackSCMEvent
26     {
27         
28            // var_Dump($this->repoid);
29         $q = MTrackDB::q('SELECT * FROM clcache where 
30             repoid = ? AND rev = ?' , $this->repo->repoid, $this->hash );
31             
32         $ar = $q->fetchAll(PDO::FETCH_ASSOC);
33         if (!empty($ar)) {
34             require_once 'MTrack/SCM/Git/Event.php';
35             $ro = MTrack_SCM_Git_Event::newFromCommit($ar[0]['sobject'], $this->repo);
36           //  var_dump("RETURNING FROM DB");
37             return $ro;
38         }
39         
40         $ent = $this->repo->history($this->name, 1, 'rev', $this->rev);
41         
42         if ($ent) {
43             MTrackDB::q('INSERT INTO clcache (repoid, rev, sobject) VALUES ( ? ,? ,? )',
44                     $this->repo->repoid, $this->hash , $ent[0]->commit
45             );
46          
47         }
48         
49         
50         
51         return $ent ? $ent[0] : false;
52     }
53
54   function cat()
55   {
56     // There may be a better way...
57     // ls-tree to determine the hash of the file from this change:
58     $fp = $this->repo->git('ls-tree', $this->rev, $this->name);
59     $line = fgets($fp);
60     $fp = null;
61     list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
62
63     // now we can cat that blob
64     return $this->repo->git('cat-file', 'blob', $hash);
65   }
66
67   function annotate($include_line_content = false)
68   {
69         /*if ($this->repo->gitdir == $this->repo->repopath) {
70           // For bare repos, we can't run annotate, so we need to make a clone
71           // with a work tree.  This relies on local clones being a cheap operation
72           
73           $wc = new MTrackWCGit($this->repo);
74           $wc->push = false;
75           print($wc);exit;
76           $fp = $wc->git('annotate', '-p',  $this->rev, '--', $this->name,);
77         } else {
78         */
79
80
81         $fp = $this->repo->git('annotate', '-p',  $this->rev, '--', $this->name);
82
83         $i = 1;
84         $ann = array();
85         $meta = array();
86         
87         while ($line = fgets($fp)) {
88         //      echo htmlentities($line), "<br>\n";
89             if (!strncmp($line, "\t", 1)) {
90                 $A = new MTrackSCMAnnotation;
91                 $A->lineno = $i;
92                 $A->repo = $this->repo;
93                 if (isset($meta['author-mail']) &&
94                     strpos($meta['author-mail'], '@')
95                 ) {
96                     $A->changeby = $meta['author'] . ' ' . $meta['author-mail'];
97                 } else {
98                     $A->changeby = $meta['author'];
99                 }
100                 $A->rev = $meta['rev'];
101                 if ($include_line_content) {
102                     $A->line = substr($line, 1);
103                 }
104                 $ann[$i++] = $A;
105                 continue;
106             }
107             if (preg_match("/^([a-f0-9]+)\s[a-f0-9]+\s[a-f0-9]+\s[a-f0-9]+$/",    $line, $M)) {
108                 $meta['rev'] = $M[1];
109             } else if (preg_match("/^(\S+)\s*(.*)$/", $line, $M)) {
110                 $name = $M[1];
111                 $value = $M[2];
112                 $meta[$name] = $value;
113             }
114         }
115         return $ann;
116     }
117 }
118
119  
120