4 // needs to be run from git-recieve (and it has to be the only thing run.
6 require_once 'MTrackWeb/Hook/Bridge.php';
8 class MTrackWeb_Hook_Git_Bridge extends MTrackWeb_Hook_Bridge
15 var $commits = array();
16 var $fileActions = array(); // file=> delete / modify etc..
19 * fills up repo, files, log, commits by running log on the STDIN
21 function __construct($repo)
28 while (($line = fgets(STDIN)) !== false) {
30 list($old, $new, $ref) = explode(' ', trim($line), 3);
31 $this->commits[] = $new;
33 $fp = $this->repo->impl()->git('log', '--no-color', '--name-status',
34 '--date=rfc', $ref, "$old..$new");
39 if (!preg_match("/^commit\s+(\S+)$/", $line)) {
40 throw new Exception("unexpected output from git log: $line");
42 // read key: value properties like Author: / Date:
43 while (($line = fgets($fp)) !== false) {
45 if (!strlen($line)) break;
46 if (preg_match("/^(\S+):\s*(.*)\s*$/", $line, $M)) {
47 $props[$M[1]] = $M[2];
50 // read the commit log.
51 while (($line = fgets($fp)) !== false) {
53 if (strncmp($line, ' ', 4)) {
56 $this->log[] = substr($line, 4);
61 if (preg_match("/^(.+)\s+(\S+)\s*$/", $line, $M)) {
64 $this->files[$file] = $new;
65 $this->fileActions[$file] = $st;
68 } while (($line = fgets($fp)) !== false);
73 function enumChangedOrModifiedFileNames()
76 foreach($this->files as $f=>$com) {
77 if ($this->fileActions[$f] == 'D') {
85 function getCommitMessage()
87 $log = join("\n", $this->log);
88 $log = preg_replace('/\[([a-fA-F0-9]+)\]/',
89 "[changeset:" . $this->repo->getBrowseRootName() . ",\$1]", $log);
93 function getFileStream($path)
95 $rev = $this->files[$path];
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);
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);
107 function getChangesetDescriptor()
110 foreach ($this->commits as $ref) {
111 $cs[] = '[changeset:' . $this->repo->getBrowseRootName() . ",$ref]";
113 return join(", ", $cs);