summaryrefslogtreecommitdiffstats
path: root/git2rss
blob: bf966bdd087cc7ecb18ec6c8ce1a7056f085e514 (plain)
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
#!/usr/bin/env php
<?php

/*
 * This file is part of git2rss.
 *
 * (c) Remi Collet <remi@remirepo.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

if (PHP_SAPI != 'cli') die ("CLI only");

// Configuration
define('LOCAL', dirname(__DIR__) . '/');
define('REMOTE', 'https://git.remirepo.net/cgit/');
define('FEEDSIZE', 50);

require __DIR__ . '/vendor/autoload.php';

use Suin\RSSWriter\Channel;
use Suin\RSSWriter\Feed;
use Suin\RSSWriter\Item;

// Current change
exec('git log --format=fuller -1', $out);
$log = exec("git log --pretty=format:%H,%at,%cn,%an,%s -1");
$entry = [
	'repo' => substr(getcwd(), strlen(LOCAL)),
	'head' => basename($_SERVER['argv'][1] ?? 'master'),
	'desc' => implode("\n", $out),
];
$log = exec("git log --pretty=format:%H,%at,%cn,%an,%s -1");
list(
	$entry['hash'],
	$entry['time'],
	$entry['commiter'],
	$entry['author'],
	$entry['comment']
) = explode(',', $log, 5);

if (substr($entry['repo'], -4) != '.git') {
	$entry['repo'] .= '.git';
}
$short = substr($entry['hash'], 0, 7);
printf("GIT2RSS Welcome %s\n", $entry['commiter']);

// Load previous changes
if (file_exists(__DIR__ . '/git2rss.json')) {
	$json = file_get_contents(__DIR__ . '/git2rss.json');
	$histo = json_decode($json, true);
	printf("GIT2RSS %d entries loaded\n", count($histo));
} else {
	$histo = array();
}

echo "GIT2RSS new entry: ${entry['author']} pushed to ${entry['repo']} (${entry['head']},$short): ${entry['comment']}\n";

// 50 recent changes, and save
$histo = array_merge([$entry], $histo);
while (count($histo) > FEEDSIZE) {
	array_pop($histo);
}
file_put_contents(__DIR__ . '/git2rss.json', json_encode($histo, JSON_PRETTY_PRINT));

// Generate RSS
$feed = new Feed();
$channel = new Channel();
$channel
	->title("Remi's RPM git repostiories")
	->description('Change')
	->url(REMOTE)
	->language('en-US')
	->copyright('Copyright 2005-2017, Remi Collet')
	->pubDate(time())
	->lastBuildDate(time())
	->ttl(60)
	->appendTo($feed);

foreach ($histo as $entry) {
	$short = substr($entry['hash'], 0, 7);
	$msg = "${entry['author']} pushed to ${entry['repo']} (${entry['head']},$short): ${entry['comment']}";

	$item = new Item();
	$item
		->title($msg)
		->description($entry['desc'] ?? $msg)
		->url(REMOTE . "${entry['repo']}/commit/?id=${entry['hash']}")
		->author($entry['author'])
		->pubDate($entry['time'])
		->guid("${entry['repo']}_${entry['hash']}", true)
		->appendTo($channel);
}

// Save RSS
file_put_contents(__DIR__ . '/index.html', $feed);
printf("GIT2RSS %d entries saved\n", count($histo));

// standard usage
passthru('git update-server-info');