Skip to content

Instantly share code, notes, and snippets.

@baturin
Created December 15, 2010 16:56
Show Gist options
  • Save baturin/742247 to your computer and use it in GitHub Desktop.
Save baturin/742247 to your computer and use it in GitHub Desktop.
git-repo-cohesion
<?php
require_once('php-github-api/lib/phpGitHubApi.php');
$github = new phpGitHubApi();
$repositories = array(
array('jquery', 'jquery'),
array('madrobby', 'scriptaculous'),
array('jashkenas', 'coffee-script'),
array('yui', 'yui3'),
array('sstephenson', 'prototype'),
array('mootools', 'mootools-core')
);
function array_slice_key($array, $key)
{
return array_map(
function($value) use ($key) { return $value[$key]; }, $array
);
}
function array_keys_exists($keys, $array)
{
$subarray = $array;
foreach ($keys as $key) {
if (!array_key_exists($key, $subarray)) {
return false;
}
$subarray = $subarray[$key];
}
return true;
}
function getContributorsCohesion($github, $repository_1, $repository_2)
{
list($user_1, $reponame_1) = $repository_1;
list($user_2, $reponame_2) = $repository_2;
return count(array_intersect(
array_slice_key($github->getRepoApi()->getRepoContributors($user_1, $reponame_1), 'login'),
array_slice_key($github->getRepoApi()->getRepoContributors($user_2, $reponame_2), 'login')
));
}
function getWatchersCohesion($github, $repository_1, $repository_2)
{
list($user_1, $reponame_1) = $repository_1;
list($user_2, $reponame_2) = $repository_2;
return count(array_intersect(
$github->getRepoApi()->getRepoWatchers($user_1, $reponame_1),
$github->getRepoApi()->getRepoWatchers($user_2, $reponame_2)
));
}
function getRepositoryName($repository)
{
return implode('/', $repository);
}
function printCohesion($cohesion)
{
foreach ($cohesion as $repository_1 => $repositories) {
foreach ($repositories as $repository_2 => $cohesionValue) {
echo implode(',', array(
$repository_1,
$repository_2,
$cohesionValue
)) . PHP_EOL;
}
}
}
$repoApi = $github->getRepoApi();
$watchersCohesion = array();
$contributorsCohesion = array();
foreach ($repositories as $repository_1) {
$repository_1_name = getRepositoryName($repository_1);
foreach ($repositories as $repository_2) {
$repository_2_name = getRepositoryName($repository_2);
if ($repository_1_name != $repository_2_name &&
!array_keys_exists(array($repository_1_name, $repository_2_name), $watchersCohesion) &&
!array_keys_exists(array($repository_2_name, $repository_1_name), $watchersCohesion)
) {
$watchersCohesion[$repository_1_name][$repository_2_name] =
getWatchersCohesion($github, $repository_1, $repository_2);
$contributorsCohesion[$repository_1_name][$repository_2_name] =
getContributorsCohesion($github, $repository_1, $repository_2);
}
}
}
echo 'Watchers cohesion' . PHP_EOL;
printCohesion($watchersCohesion);
echo 'Contributors cohesion' . PHP_EOL;
printCohesion($contributorsCohesion);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment