b16178c21711e6da7676f9d2fa0f3498f3c2faa6
[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     var $props = array(); // date etc..
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             if (preg_match('/^0+$/',$old)) {
32                 continue; // it's a new branch - ignore..
33             }
34   
35             $fp = $this->repo->git(
36                 'log', '--no-color', '--name-status',
37                 '--date=rfc',  "$old..$new"); //$ref, used to be in here??  - but it breaks stuff...
38                 
39                 
40             $props = array();
41             $line = fgets($fp);
42             if (!preg_match("/^commit\s+(\S+)$/", $line)) {
43                 throw new Exception("unexpected output from git log: $line");
44             }
45             $this->props = array();
46             // read key: value properties like Author: / Date: 
47             while (($line = fgets($fp)) !== false) {
48                 $line = rtrim($line);
49                 if (!strlen($line)) break;
50                 if (preg_match("/^(\S+):\s*(.*)\s*$/", $line, $M)) {
51                     $this->props[$M[1]] = $M[2];
52                 }
53             }
54             // read the commit log.
55             while (($line = fgets($fp)) !== false) {
56                 $line = rtrim($line);
57                 if (strncmp($line, '    ', 4)) {
58                     break;
59                 }
60                 $this->log[] = substr($line, 4);
61             }
62             
63             
64             do {
65                 if (preg_match("/^(.+)\s+(\S+)\s*$/", $line, $M)) {
66                     $st = $M[1];
67                     $file = $M[2];
68                     $this->files[$file] = $new;
69                     $this->fileActions[$file] = $st;
70                 }
71               
72             } while (($line = fgets($fp)) !== false);
73         }
74         //print_r($this);exit;
75     }
76
77
78     function enumChangedOrModifiedFileNames()
79     {
80         $ret = array();
81         foreach($this->files as $f=>$com) {
82             if ($this->fileActions[$f] == 'D') {
83                 continue;
84             }
85             $ret[] = $f;
86         }
87         return $ret;
88     }
89
90     function getCommitMessage()
91     {
92         $log = join("\n", $this->log);
93         $log = preg_replace('/\[([a-fA-F0-9]+)\]/',
94           "[changeset:" . $this->repo->getBrowseRootName() . ",\$1]", $log);
95         return $log;
96     }
97
98     function getFileStream($path)
99     {
100         $rev = $this->files[$path];
101
102         // There may be a better way...
103         // ls-tree to determine the hash of the file from this change:
104         $fp = $this->repo->git( 'ls-tree', '-r', $rev, $path);
105         $line = fgets($fp);
106         $fp = null;
107         list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
108         // now we can cat that blob
109         return $this->repo->git( 'cat-file', 'blob', $hash);
110     }
111
112     function getChangesetDescriptor()
113     {
114         $cs = array();
115         foreach ($this->commits as $ref) {
116           $cs[] = '[changeset:' . $this->repo->getBrowseRootName() . ",$ref]";
117         }
118         return join(", ", $cs);
119     }
120 }