MTrack/SCM/Git/CommitHookBridge.php
[web.mtrack] / MTrack / SCM / Git / CommitHookBridge.php
1 <?php
2
3 require_once 'MTrack/Interface/CommitHookBridge.php';
4 require_once 'MTrack/Repo.php';
5
6 // needs to be run from git-recieve (and it has to be the only thing run.
7
8 class MTrack_SCM_Git_CommitHookBridge extends  IMTrackCommitHookBridge 
9 {
10     
11     var $repo;
12     
13     var $files = array();
14     var $log = array();
15     var $commits = array();
16     var $fileActions = array(); // file=> delete / modify etc..
17     
18     
19     /**
20     * fills up repo, files, log, commits by running log on the STDIN
21     */
22     
23     function __construct($repo) 
24     {
25         $this->repo = $repo->impl();
26         while (($line = fgets(STDIN)) !== false) {
27             echo "got: $line\n";
28             list($old, $new, $ref) = explode(' ', trim($line), 3);
29             $this->commits[] = $new;
30   
31             $fp = $this->repo->git(
32                 'log', '--no-color', '--name-status',
33                 '--date=rfc',  "$old..$new"); //$ref, used to be in here??  - but it breaks stuff...
34                 
35                 
36             $props = array();
37             $line = fgets($fp);
38             if (!preg_match("/^commit\s+(\S+)$/", $line)) {
39                 throw new Exception("unexpected output from git log: $line");
40             }
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         //print_r($this);exit;
71     }
72
73
74     function enumChangedOrModifiedFileNames()
75     {
76         $ret = array();
77         foreach($this->files as $f=>$com) {
78             if ($this->fileActions[$f] == 'D') {
79                 continue;
80             }
81             $ret[] = $f;
82         }
83         return $ret;
84     }
85
86     function getCommitMessage()
87     {
88         $log = join("\n", $this->log);
89         $log = preg_replace('/\[([a-fA-F0-9]+)\]/',
90           "[changeset:" . $this->repo->getBrowseRootName() . ",\$1]", $log);
91         return $log;
92     }
93
94     function getFileStream($path)
95     {
96         $rev = $this->files[$path];
97
98         // There may be a better way...
99         // ls-tree to determine the hash of the file from this change:
100         $fp = $this->repo->git( 'ls-tree', '-r', $rev, $path);
101         $line = fgets($fp);
102         $fp = null;
103         list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
104         // now we can cat that blob
105         return $this->repo->git( 'cat-file', 'blob', $hash);
106     }
107
108     function getChangesetDescriptor()
109     {
110         $cs = array();
111         foreach ($this->commits as $ref) {
112           $cs[] = '[changeset:' . $this->repo->getBrowseRootName() . ",$ref]";
113         }
114         return join(", ", $cs);
115     }
116 }