-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtg.php
executable file
·141 lines (129 loc) · 4.23 KB
/
tg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/php
<?php
/**
* Team Gource Command Line Utility
* @author: Tamas Kalman <[email protected]>
* @web: https://github.com/ktamas77/team-gource
*/
$config = yaml_parse(file_get_contents(__DIR__ . 'tg.conf'));
function updateRepo($org, $repo) {
if (!is_dir('repos')) {
mkdir('repos');
}
if (!is_dir("repos/$repo")) {
exec("git clone [email protected]:$org/$repo.git repos/$repo");
} else {
$currentDir = getcwd();
chdir("repos/$repo");
exec("git pull origin master");
chdir($currentDir);
}
}
function getGourceLog($repo) {
if (!is_dir('tmp')) {
mkdir('tmp');
}
if (!is_dir('logs')) {
mkdir('logs');
}
$logName = tempnam("tmp/", "gource-repo-$repo-");
exec("gource repos/$repo --output-custom-log $logName");
return $logName;
}
function appendMasterLog($masterLog, $logName) {
$logData = file_get_contents($logName);
file_put_contents($masterLog, $logData, FILE_APPEND);
unlink($logName);
}
function filterExcludedNamesFromLog($logName, $excluded) {
$logData = file($logName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$newData = "";
foreach ($excluded as &$item) {
$item = str_replace(" ", "\ ", $item);
}
$excluded = implode("|", $excluded);
$pattern = "/[0-9]+\|($excluded)\|[A-Z]\|/";
foreach ($logData as $logLine) {
preg_match($pattern, $logLine, $matches);
if (!isset($matches[1])) {
$newData .= $logLine . "\n";
}
}
file_put_contents($logName, $newData);
}
function addRepoToLog($logName, $repo, $org) {
$logData = file($logName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$newData = "";
foreach ($logData as $logLine) {
$newData .= preg_replace("/(\|[A-Z]\|)\//", "$1/$org/$repo/", $logLine) . "\n";
}
file_put_contents($logName, $newData);
}
function filterNonMembersFromLog($logName, $members) {
$logData = file($logName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$fullTeam = [];
foreach ($members as $member) {
if (isset($member['aliases'])) {
foreach ($member['aliases'] as $alias) {
$fullTeam[$alias] = true;
}
}
}
$nonTeam = [];
$newData = "";
$pattern = "/[0-9]+\|(.*)\|[A-Z]\|/";
foreach ($logData as $logLine) {
preg_match($pattern, $logLine, $matches);
$name = $matches[1];
if (isset($fullTeam[$name])) {
$newData .= $logLine . "\n";
} else {
if (!isset($nonTeam[$name])) {
print "Non-team member found: $name\n";
}
$nonTeam[$name] = true;
}
}
file_put_contents($logName, $newData);
}
function sortMasterLog($masterLog) {
$logData = file($masterLog, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
asort($logData);
$newData = "";
foreach ($logData as $line) {
$newData .= $line . "\n";
}
file_put_contents($masterLog, $newData);
}
$teams = $config['teams'];
foreach ($teams as $team) {
$teamName = $team['name'];
$teamCollection = $team['collection'];
$excluded = [];
if (isset($team['excluded'])) {
$excluded = $team['excluded'];
}
print "Team: $teamName\n";
print "Collection: $teamCollection\n";
$repos = $config['collections'];
$members = $team['members'];
foreach ($repos as $repo) {
if ($repo['name'] === $teamCollection) {
$teamOrg = $repo['organization'];
$teamRepos = $repo['repos'];
$masterLog = tempnam("tmp/", "gource-master-log-$teamCollection");
foreach ($teamRepos as $repoName) {
print "updating repository for $repoName\n";
updateRepo($teamOrg, $repoName);
print "extracting logs for $repoName\n";
$logName = getGourceLog($repoName);
filterExcludedNamesFromLog($logName, $excluded);
filterNonMembersFromLog($logName, $members);
addRepoToLog($logName, $repoName, $teamOrg);
appendMasterLog($masterLog, $logName);
}
sortMasterLog($masterLog);
rename($masterLog, "logs/gource-master-log-$teamCollection.log");
}
}
}