bbb4f9d89154a07cc50a0c3ba68b9f01ada4d0d0
[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', $ref, "$old..$new");
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     }
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->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->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 }