sync
[web.mtrack] / MTrackWeb / Hook / Git / Bridge.php
1 <?php  
2
3
4 // needs to be run from git-recieve (and it has to be the only thing run.
5 /*
6 require_once 'MTrackWeb/Hook/Bridge.php';
7
8 class MTrackWeb_Hook_Git_Bridge extends  MTrackWeb_Hook_Bridge
9 {
10     
11     
12     var $repo;
13     var $files = array();
14     var $log = array();
15     var $commits = array();
16     var $fileActions = array(); // file=> delete / modify etc..
17     
18     /**
19     * fills up repo, files, log, commits by running log on the STDIN
20     * /
21     function __construct($repo) 
22     {
23         
24         
25         
26         $this->repo = $repo;
27         
28         while (($line = fgets(STDIN)) !== false) {
29           
30             list($old, $new, $ref) = explode(' ', trim($line), 3);
31             $this->commits[] = $new;
32             
33             $fp = $this->repo->impl()->git('log', '--no-color', '--name-status',
34                             '--date=rfc', $ref, "$old..$new");
35               
36               
37             $props = array();
38             $line = fgets($fp);
39             if (!preg_match("/^commit\s+(\S+)$/", $line)) {
40                 throw new Exception("unexpected output from git log: $line");
41             }
42             // read key: value properties like Author: / Date: 
43             while (($line = fgets($fp)) !== false) {
44                 $line = rtrim($line);
45                 if (!strlen($line)) break;
46                 if (preg_match("/^(\S+):\s*(.*)\s*$/", $line, $M)) {
47                   $props[$M[1]] = $M[2];
48                 }
49             }
50             // read the commit log.
51             while (($line = fgets($fp)) !== false) {
52                 $line = rtrim($line);
53                 if (strncmp($line, '    ', 4)) {
54                   break;
55                 }
56                 $this->log[] = substr($line, 4);
57             }
58           
59           
60             do {
61                 if (preg_match("/^(.+)\s+(\S+)\s*$/", $line, $M)) {
62                     $st = $M[1];
63                     $file = $M[2];
64                     $this->files[$file] = $new;
65                     $this->fileActions[$file] = $st;
66                 }
67             
68             } while (($line = fgets($fp)) !== false);
69         }
70     }
71
72
73     function enumChangedOrModifiedFileNames()
74     {
75         $ret = array();
76         foreach($this->files as $f=>$com) {
77             if ($this->fileActions[$f] == 'D') {
78                 continue;
79             }
80             $ret[] = $f;
81         }
82         return $ret;
83     }
84
85     function getCommitMessage()
86     {
87         $log = join("\n", $this->log);
88         $log = preg_replace('/\[([a-fA-F0-9]+)\]/',
89           "[changeset:" . $this->repo->getBrowseRootName() . ",\$1]", $log);
90         return $log;
91     }
92
93     function getFileStream($path)
94     {
95         $rev = $this->files[$path];
96
97         // There may be a better way...
98         // ls-tree to determine the hash of the file from this change:
99         $fp = $this->repo->impl()->git('ls-tree', '-r', $rev, $path);
100         $line = fgets($fp);
101         $fp = null;
102         list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
103         // now we can cat that blob
104         return $this->repo->impl()->git('cat-file', 'blob', $hash);
105     }
106
107     function getChangesetDescriptor()
108     {
109         $cs = array();
110         foreach ($this->commits as $ref) {
111             $cs[] = '[changeset:' . $this->repo->getBrowseRootName() . ",$ref]";
112         }
113         return join(", ", $cs);
114     }
115 }
116
117 */