3 require_once 'MTrack/Interface/CommitHookBridge.php';
4 require_once 'MTrack/Repo.php';
6 // needs to be run from git-recieve (and it has to be the only thing run.
8 class MTrack_SCM_Git_CommitHookBridge extends IMTrackCommitHookBridge
15 var $commits = array();
16 var $fileActions = array(); // file=> delete / modify etc..
17 var $props = array(); // date etc..
20 * fills up repo, files, log, commits by running log on the STDIN
23 function __construct($repo)
25 $this->repo = $repo->impl();
26 while (($line = fgets(STDIN)) !== false) {
28 list($old, $new, $ref) = explode(' ', trim($line), 3);
29 $this->commits[] = $new;
31 if (preg_match('/^0+$/',$old)) {
32 continue; // it's a new branch - ignore..
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...
42 if (!preg_match("/^commit\s+(\S+)$/", $line)) {
43 throw new Exception("unexpected output from git log: $line");
45 $this->props = array();
46 // read key: value properties like Author: / Date:
47 while (($line = fgets($fp)) !== false) {
49 if (!strlen($line)) break;
50 if (preg_match("/^(\S+):\s*(.*)\s*$/", $line, $M)) {
51 $this->props[$M[1]] = $M[2];
54 // read the commit log.
55 while (($line = fgets($fp)) !== false) {
57 if (strncmp($line, ' ', 4)) {
60 $this->log[] = substr($line, 4);
65 if (preg_match("/^(.+)\s+(\S+)\s*$/", $line, $M)) {
68 $this->files[$file] = $new;
69 $this->fileActions[$file] = $st;
72 } while (($line = fgets($fp)) !== false);
74 //print_r($this);exit;
78 function enumChangedOrModifiedFileNames()
81 foreach($this->files as $f=>$com) {
82 if ($this->fileActions[$f] == 'D') {
90 function getCommitMessage()
92 $log = join("\n", $this->log);
93 $log = preg_replace('/\[([a-fA-F0-9]+)\]/',
94 "[changeset:" . $this->repo->getBrowseRootName() . ",\$1]", $log);
98 function getFileStream($path)
100 $rev = $this->files[$path];
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);
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);
112 function getChangesetDescriptor()
115 foreach ($this->commits as $ref) {
116 $cs[] = '[changeset:' . $this->repo->getBrowseRootName() . ",$ref]";
118 return join(", ", $cs);