php8
[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         /*
29          
30          need to work out a 'sweet caching method'...
31          
32            // var_Dump($this->id);
33         $q = MTrackDB::q('SELECT * FROM clcache where 
34             id = ? AND rev = ?' , $this->repo->id, $this->hash );
35             
36         $ar = $q->fetchAll(PDO::FETCH_ASSOC);
37         if (!empty($ar)) {
38             require_once 'MTrack/SCM/Git/Event.php';
39             $ro = MTrack_SCM_Git_Event::newFromCommit($ar[0]['sobject'], $this->repo);
40           //  var_dump("RETURNING FROM DB");
41             return $ro;
42         }                                        
43         */
44         
45         $ent = $this->repo->history($this->name, 1, 'rev', $this->rev);
46         
47         return $ent[0];
48     
49         if ($ent) {
50             MTrackDB::q('INSERT INTO mtrack_clcache (id, rev, sobject) VALUES ( ? ,? ,? )',
51                     $this->repo->id, $this->hash , $ent[0]->commit
52             );
53          
54         }
55         
56         
57         
58         return $ent ? $ent[0] : false;
59     }
60
61   function cat()
62   {
63     // There may be a better way...
64     // ls-tree to determine the hash of the file from this change:
65     $fp = $this->repo->git('ls-tree', $this->rev, $this->name);
66     $line = fgets($fp);
67     $fp = null;
68     
69     list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
70
71     // now we can cat that blob
72     return $this->repo->git('cat-file', 'blob', $hash);
73   }
74
75   function annotate($include_line_content = false)
76   {
77         /*if ($this->repo->gitdir == $this->repo->repopath) {
78           // For bare repos, we can't run annotate, so we need to make a clone
79           // with a work tree.  This relies on local clones being a cheap operation
80           
81           $wc = new MTrackWCGit($this->repo);
82           $wc->push = false;
83           print($wc);exit;
84           $fp = $wc->git('annotate', '-p',  $this->rev, '--', $this->name,);
85         } else {
86         */
87
88
89         $fp = $this->repo->git('annotate', '-p',  $this->rev, '--', $this->name);
90
91         $i = 1;
92         $ann = array();
93         $meta = array();
94         
95         while ($line = fgets($fp)) {
96         //      echo htmlentities($line), "<br>\n";
97             if (!strncmp($line, "\t", 1)) {
98                 $A = new MTrackSCMAnnotation;
99                 $A->lineno = $i;
100                 $A->repo = $this->repo;
101                 if (isset($meta['author-mail']) &&
102                     strpos($meta['author-mail'], '@')
103                 ) {
104                     $A->changeby = $meta['author'] . ' ' . $meta['author-mail'];
105                 } else {
106                     $A->changeby = $meta['author'];
107                 }
108                 $A->rev = $meta['rev'];
109                 if ($include_line_content) {
110                     $A->line = substr($line, 1);
111                 }
112                 $ann[$i++] = $A;
113                 continue;
114             }
115             if (preg_match("/^([a-f0-9]+)\s[a-f0-9]+\s[a-f0-9]+\s[a-f0-9]+$/",    $line, $M)) {
116                 $meta['rev'] = $M[1];
117             } else if (preg_match("/^(\S+)\s*(.*)$/", $line, $M)) {
118                 $name = $M[1];
119                 $value = $M[2];
120                 $meta[$name] = $value;
121             }
122         }
123         return $ann;
124     }
125 }
126
127  
128