Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify getting branches and revision

  • Loading branch information...
commit e853ff5ac82d47cc9fec784e51a334b26486f529 1 parent d7109df
David Soria Parra dsp authored
Showing with 37 additions and 173 deletions.
  1. +37 −173 lib/Git/PostReceiveHook.php
210 lib/Git/PostReceiveHook.php
View
@@ -78,140 +78,26 @@ public function process()
}
}
}
- $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 @@ private function getTagInfo($tag)
}
/**
- * 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,50 @@ private function getBugs($log)
* @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));
- $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 +425,10 @@ private function sendCommitMail($revision, $branches)
}
}
+ $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 +449,11 @@ private function isRevExistsInBranches($revision, array $branches) {
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);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.