2 require_once 'Interface/CommitHookBridge.php';
3 require_once 'Config.php';
4 require_once '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
14 var $commits = array();
15 var $fileActions = array(); // file=> delete / modify etc..
17 * fills up repo, files, log, commits by running log on the STDIN
19 function __construct(MTrackRepo $repo)
21 self::$GIT = MTrackConfig::get('tools', 'git');
23 while (($line = fgets(STDIN)) !== false) {
25 list($old, $new, $ref) = explode(' ', trim($line), 3);
26 $this->commits[] = $new;
28 $fp = $this->run(self::$GIT, 'log', '--no-color', '--name-status',
29 '--date=rfc', $ref, "$old..$new");
34 if (!preg_match("/^commit\s+(\S+)$/", $line)) {
35 throw new Exception("unexpected output from git log: $line");
37 // read key: value properties like Author: / Date:
38 while (($line = fgets($fp)) !== false) {
40 if (!strlen($line)) break;
41 if (preg_match("/^(\S+):\s*(.*)\s*$/", $line, $M)) {
42 $props[$M[1]] = $M[2];
45 // read the commit log.
46 while (($line = fgets($fp)) !== false) {
48 if (strncmp($line, ' ', 4)) {
51 $this->log[] = substr($line, 4);
56 if (preg_match("/^(.+)\s+(\S+)\s*$/", $line, $M)) {
59 $this->files[$file] = $new;
60 $this->fileActions[$file] = $st;
63 } while (($line = fgets($fp)) !== false);
68 function enumChangedOrModifiedFileNames()
71 foreach($this->files as $f=>$com) {
72 if ($this->fileActions[$f] == 'D') {
80 function getCommitMessage()
82 $log = join("\n", $this->log);
83 $log = preg_replace('/\[([a-fA-F0-9]+)\]/',
84 "[changeset:" . $this->repo->getBrowseRootName() . ",\$1]", $log);
88 function getFileStream($path)
90 $rev = $this->files[$path];
92 // There may be a better way...
93 // ls-tree to determine the hash of the file from this change:
94 $fp = $this->run(self::$GIT, 'ls-tree', '-r', $rev, $path);
97 list($mode, $type, $hash, $name) = preg_split("/\s+/", $line);
98 // now we can cat that blob
99 return $this->run(self::$GIT, 'cat-file', 'blob', $hash);
102 function getChangesetDescriptor()
105 foreach ($this->commits as $ref) {
106 $cs[] = '[changeset:' . $this->repo->getBrowseRootName() . ",$ref]";
108 return join(", ", $cs);