summaryrefslogtreecommitdiffstats
path: root/git2rss
blob: e8ce8c35c96989cae7954ab0fca40194fc2c8114 (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
#!/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");

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

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

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

if (file_exists(__DIR__ . '/git2rss.json')) {
	$json = file_get_contents(__DIR__ . '/git2rss.json');
	$histo = json_decode($json, true);
} else {
	$histo = array();
}


// Current change
$log = exec("git log --pretty=format:%H,%at,%an,%s -1");
$entry = [
	'repo' => substr(getcwd(), strlen(LOCAL)),
	'head' => basename($_SERVER['argv'][1] ?? 'master'),
];
list($entry['hash'], $entry['time'], $entry['author'], $entry['comment']) = explode(',', $log, 4);
if (substr($entry['repo'], -4) != '.git') {
	$entry['repo'] .= '.git';
}
// print_r($entry);
$short = substr($entry['hash'], 0, 7);
echo "RSS ENTRY: ${entry['author']} pushed to ${entry['repo']} (${entry['head']},$short): ${entry['comment']}";


// 50 recent changes
$histo = array_merge([$entry], $histo);
while (count($histo) > 50) {
	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($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);