com karma: Simplify getting branches and revision: lib/Git/PostReceiveHook.php

From: Date: Wed, 21 Mar 2012 18:20:39 +0000
Subject: com karma: Simplify getting branches and revision: lib/Git/PostReceiveHook.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-68129@lists.php.net to get a copy of this message
Commit: a6bcc1358deb70b85644ed594db3cbd8f5d65a35 Author: David Soria Parra <dsp@php.net> Wed, 21 Mar 2012 19:20:39 +0100 Parents: 42b7508bdc7867645728fb9e765aecadae333609 Branches: dsp/playground Link: http://git.php.net/?p=karma.git;a=commitdiff;h=a6bcc1358deb70b85644ed594db3cbd8f5d65a35 Log: Simplify getting branches and revision Changed paths: M lib/Git/PostReceiveHook.php

a6bcc1358deb70b85644ed594db3cbd8f5d65a35 diff --git a/lib/Git/PostReceiveHook.php b/lib/Git/PostReceiveHook.php index b7efb42..1e18195 100644 --- a/lib/Git/PostReceiveHook.php +++ b/lib/Git/PostReceiveHook.php @@ -78,140 +78,26 @@ class PostReceiveHook extends ReceiveHook } } } - $this->alreadyExistsBranches = array_diff($this->allBranches, $newBranches); //send mails per ref push + $revisions = []; + foreach ($this->refs as $ref) { if ($ref['reftype'] == self::REF_TAG) { $this->sendTagMail($ref['refname'], $ref['changetype'], $ref['old'], $ref['new']); } - } - foreach($this->refs as $ref) { - // magic populate the $this->revisions - if ($ref['changetype'] != self::TYPE_DELETED) { - $this->getBranchRevisions($ref['refname'], $ref['changetype'], $ref['old'], $ref['new']); - } - } - //send mails per commit - foreach ($this->revisions as $revision => $branches) { - // check if it commit was already in other branches - if (!$this->isRevExistsInBranches($revision, array_diff($this->allBranches, $branches))) { - $this->sendCommitMail($revision, $branches); - } + $revisions = array_merge( + $revisions, + $this->getRevisions(escapeshellarg($ref['old'] . '..' . $ref['new']))); } - } - /** - * Send mail about branch. - * Subject: [git] [branch] %PROJECT%: %STATUS% branch %BRANCH_NAME% - * Body: - * Branch %BRANCH_NAME% in %PROJECT% was %STATUS% - * Date: Thu, 08 Mar 2012 12:39:48 +0000(current mail date) - * - * Link: http://git.php.net/?p=%PROJECT_PATH%;a=log;h=%SHA_NEW%;hp=%SHA_OLD% - * - * --part1-- - * Log: - * - * --per commit-- - * Commit: %SHA% - * Author: %USER% Thu, 08 Mar 2012 12:39:48 +0000 - * Committer: %USER% Thu, 08 Mar 2012 12:39:48 +0000 - * Link: http://git.php.net/?p=%PROJECT_PATH%;a=commitdiff;h=%SHA% - * Shortlog: %COMMIT_SUBJECT% - * --/per commit-- - * - * --/part1-- - * - * @param string $name branch fullname (refs/heads/example) - * @param int $changeType delete, create or update - * @param string $oldrev old revision - * @param string $newrev new revision - * @return string mail uniq id - */ - private function sendBranchMail($name, $changeType, $oldrev, $newrev) - { - - $logString = ''; - $status = [self::TYPE_UPDATED => 'update', self::TYPE_CREATED => 'create', self::TYPE_DELETED => 'delete']; - $shortname = str_replace('refs/heads/', '', $name); - - // forced push - if ($changeType == self::TYPE_UPDATED) { - $replacedRevisions = $this->getRevisions(escapeshellarg($newrev . '..' . $oldrev)) ?: false; - } else { - $replacedRevisions = false; - } - - if ($changeType != self::TYPE_DELETED) { - - $revisions = $this->getBranchRevisions($name, $changeType, $oldrev, $newrev); - - if (count($revisions)) { - - $logString = ''; - - foreach ($revisions as $revision) { - $commitInfo = $this->getCommitInfo($revision); - $logString .= 'Commit: ' . $revision . "\n"; - $logString .= 'Author: ' . $commitInfo['author'] . ' <' . $commitInfo['author_email'] . '> ' . $commitInfo['author_date'] . "\n"; - if (($commitInfo['author'] != $commitInfo['committer']) || ($commitInfo['author_email'] != $commitInfo['committer_email'])) { - $logString .= 'Committer: ' . $commitInfo['committer'] . ' <' . $commitInfo['committer_email'] . '> ' . $commitInfo['committer_date'] . "\n"; - } - $logString .= 'Link: http://git.php.net/?p=' . $this->getRepositoryName() . ";a=commitdiff;h=" . $revision . "\n"; - $logString .= 'Shortlog: ' . $commitInfo['subject'] . "\n"; - $logString .= "\n"; - - } - } + foreach (array_unique($revisions) as $revision) { + $this->sendCommitMail($revision); } - $mail = new \Mail(); - $mail->setSubject($this->emailPrefix . 'push ' . $this->getRepositoryShortName() . ': ' . $status[$changeType] . ' branch ' . $shortname); - - $message = 'Branch ' . $shortname . ' in ' . $this->getRepositoryName() . ' was ' . $status[$changeType] . 'd' . "\n"; - $message .= 'Date: ' . date('r') . "\n"; - - if ($changeType != self::TYPE_DELETED) { - $message .= "\n"; - $message .= "Link: http://git.php.net/?p=" . $this->getRepositoryName() . ";a=log;h=" . $newrev . ($changeType != self::TYPE_CREATED ? ";hp=" . $oldrev : "") . "\n"; - $message .= "\n"; - } - - // forced push - if ($replacedRevisions) { - $message .= "Discarded revisions: \n" . implode("\n", $replacedRevisions) . "\n\n"; - } - - if ($changeType != self::TYPE_DELETED) { - - if (strlen($logString) < 8192) { - // inline log - $message .= "\nLog:\n" . $logString . "\n"; - } else { - // log attach - $logFile = 'log_' . $oldrev . '_' . $newrev . '.txt'; - $mail->addTextFile($logFile, $logString); - if ((strlen($message) + $mail->getFileLength($logFile)) > 262144) { - // changed paths attach exceeded max size - $mail->dropFile($logFile); - $message .= "\nLog: <changed paths exceeded maximum size>"; - } - } - } - - $mail->setMessage($message); - - $mail->setFrom($this->pushAuthor . '@php.net', $this->pushAuthorName); - $mail->addTo($this->mailingList); - - $mail->send(); - - return $mail->getId(); } - /** * Cache revisions per branche for use it later * @param string $branchName branch fullname @@ -359,41 +245,6 @@ class PostReceiveHook extends ReceiveHook } /** - * Find revisions for branch change - * Also cache revisions list for revisions mails - * @param string $name branch fullname (refs/heads/example) - * @param int $changeType delete, create or update - * @param string $oldrev old revision - * @param string $newrev new revision - * @return array revisions list - */ - private function getBranchRevisions($name, $changeType, $oldrev, $newrev) - { - if ($changeType == self::TYPE_UPDATED) { - // git rev-list old..new - $revisions = $this->getRevisions(escapeshellarg($oldrev . '..' . $newrev)); - } else { - // for new branch we write log about new commits only - $revisions = $this->getRevisions( - escapeshellarg($newrev) . ' --not ' . implode(' ', $this->escapeArrayShellArgs($this->alreadyExistsBranches)) - ); - - // for new branches we check if they was separated from other branches in same push - // see README.POST_RECEIVE_MAIL "commit mail" part. - foreach ($this->updatedBranches as $refname) { - if ($this->isRevExistsInBranches($this->refs[$refname]['old'], [$name])) { - $this->cacheRevisions($name, $this->getRevisions(escapeshellarg($this->refs[$refname]['old'] . '..' . $newrev))); - } - } - } - - $this->cacheRevisions($name, $revisions); - - return $revisions; - } - - - /** * Get list of revisions for $revRange * * Required already escaped string in $revRange!!! @@ -496,47 +347,51 @@ class PostReceiveHook extends ReceiveHook * @param string $revision commit revision * @param array $branches branches in current push with this commit */ - private function sendCommitMail($revision, $branches) + private function sendCommitMail($revision) { - $bnames = array_map(function($x) { - return str_replace('refs/heads/', '', $x); - }, $branches); + $info = $this->getCommitInfo($revision); + $paths = $this->getChangedPaths(escapeshellarg($revision)); + $branches = $this->getBranchesForRevision($revision); - $info = $this->getCommitInfo($revision); - $paths = $this->getChangedPaths(escapeshellarg($revision)); $pathsString = ''; foreach ($paths as $path => $action) { $pathsString .= ' ' . $action . ' ' . $path . "\n"; } - $diff = \Git::gitExec('diff-tree -c -p %s', escapeshellarg($revision)); - $mail = new \Mail(); - $mail->setSubject($this->emailPrefix . 'com ' . $this->getRepositoryShortName() . ': ' . $info['subject'] . ': '. implode(' ', array_keys($paths))); - $mail->setTimestamp(strtotime($info['author_date'])); + $diff = \Git::gitExec('diff-tree -c -p %s', escapeshellarg($revision)); - $message = ''; + $reponame = $this->getRepositoryShortName(); + $subject = $this->emailPrefix . 'com ' . $reponame . ': ' . $info['subject'] . ': '. implode(' ', array_keys($paths)); + $timestamp = strtotime($info['author_date']); + $message = ''; $message .= 'Commit: ' . $revision . "\n"; $message .= 'Author: ' . $info['author'] . ' <' . $info['author_email'] . '> ' . $info['author_date'] . "\n"; + if (($info['author'] != $info['committer']) || ($info['author_email'] != $info['committer_email'])) { $message .= 'Committer: ' . $info['committer'] . ' <' . $info['committer_email'] . '> ' . $info['committer_date'] . "\n"; } - if ($info['parents']) $message .= 'Parents: ' . $info['parents'] . "\n"; - $message .= "Branches: " . implode(' ', $bnames) . "\n"; - $message .= "\n" . "Link: http://git.php.net/?p=" . $this->getRepositoryName() . ";a=commitdiff;h=" . $revision . "\n"; + if ($info['parents']) { + $message .= 'Parents: ' . $info['parents'] . "\n"; + } - $message .= "\nLog:\n" . $info['log'] . "\n"; + $message .= "Branches: " . implode(' ', $branches) . "\n"; + $message .= "\n"; + $message .= "Link: http://git.php.net/?p=" . $this->getRepositoryName() . ";a=commitdiff;h=" . $revision . "\n"; + $message .= "\n"; + $message .= "Log:\n" . $info['log'] . "\n"; if ($bugs = $this->getBugs($info['log'])) { $message .= "\nBugs:\n" . implode("\n", $bugs) . "\n"; } - $isTrivialMerge = empty($pathsString); - if (!$isTrivialMerge && strlen($pathsString) < 8192) { + $mail = new \Mail(); + + if (strlen($pathsString) < 8192) { // inline changed paths $message .= "\nChanged paths:\n" . $pathsString . "\n"; if ((strlen($pathsString) + strlen($diff)) < 8192) { @@ -571,7 +426,10 @@ class PostReceiveHook extends ReceiveHook } } + $isTrivialMerge = count($paths) <= 0; if (!$isTrivialMerge) { + $mail->setSubject($subject); + $mail->setTimestamp($timestamp); $mail->setMessage($message); $mail->setFrom($this->pushAuthor . '@php.net', $this->pushAuthorName); @@ -592,4 +450,11 @@ class PostReceiveHook extends ReceiveHook return !(bool) \Git::gitExec('rev-list --max-count=1 %s --not %s', escapeshellarg($revision), implode(' ', $this->escapeArrayShellArgs($branches))); } + private function getBranchesForRevision($revision) { + $branches = explode("\n", \Git::gitExec('branch --contains %s', escapeshellarg($revision))); + return array_map( + function ($n) { + return trim($n, ' *'); + }, $branches); + } }
« previous php.cvs (#68129) next »