summaryrefslogtreecommitdiff
path: root/gitnoti.py
blob: 5d5f186f3be3ff667f97bf409ed8d437ccc977aa (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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python

# gitnotipy - An IRC bot which announces changes to a collection of git repositories
# Copyright (C) 2009-2010  Jon Bergli Heier

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import git, os, sys, time, pyinotify, netrc
from optparse import OptionParser
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.internet.task import LoopingCall

have_bitly = False
try:
	import bitly
	have_bitly = True
except:
	pass

parser = OptionParser()
parser.add_option('-s', '--host')
parser.add_option('-p', '--port', type = 'int', default = 6667)
parser.add_option('-n', '--nick', default = 'git')
parser.add_option('-d', '--dir')
parser.add_option('-c', '--channel')
parser.add_option('-i', '--interval', type = 'int', default = 10)
parser.add_option('-u', '--url')

(options, args) = parser.parse_args()

if not options.host or not options.dir or not options.channel or not options.nick:
	parser.print_help()
	sys.exit(1)

if have_bitly and not netrc.netrc().authenticators('bitly'):
	have_bitly = False

root = options.dir

if root[-1] == '/':
	root = root[:-1]

repos = None

class GitnoitpyAmbiguousException(Exception): pass

class NotifyRepo(object):
	def __init__(self, bot, path):
		self.path = path
		self.bot = bot
		self.repo = git.Repo(path)
		flags = pyinotify.EventsCodes.ALL_FLAGS
		self.wdd = bot.wm.add_watch([os.path.join(path, 'refs/heads'), os.path.join(path, 'refs/tags')], flags['IN_MODIFY'] | flags['IN_CREATE'])
		self.heads = dict([(h.name, h.commit.hexsha) for h in self.repo.heads])
		self.tags = [t.name for t in self.repo.tags]

def repo_commit_msg(repo, branch, commits):
	reponame = os.path.splitext(os.path.basename(repo.working_dir))[0]

	deletions = insertions = 0
	files = []

	for c in commits:
		deletions += c.stats.total['deletions']
		insertions += c.stats.total['insertions']
		files.extend((f for f in c.stats.files.iterkeys() if not f in files))

	stat = '%d files,' % len(files)
	if len(commits) > 1:
		stat = '%d commits, %s' % (len(commits), stat)
	stat += ' \00305--%d\017' % deletions
	stat += ' \00303++%d\017' % insertions

	commit = commits[0]
	msg = '\002%s%s\002 pushed to \002%s\002 by \002%s\002 (%s) %s' % (
		reponame,
		('/'+branch) if branch else '',
		commit.hexsha[:7],
		commit.committer.name if commit.author.name.encode('utf8') == commit.committer.name else '%s/%s' % (commit.committer.name, commit.author.name.encode('utf8')),
		stat,
		commit.summary.encode('utf8')
	)

	if options.url:
		msg += ' | '
		url = options.url % {'repo': reponame, 'commit': commit.hexsha}
		if have_bitly:
			short_url = None
			try:
				bitly_auth = netrc.netrc().authenticators('bitly')
				bitly_api = bitly.Api(login = bitly_auth[0], apikey = bitly_auth[2])
				short_url = bitly_api.shorten(url)
			except:
				short_url = None

			if short_url:
				url = short_url.encode('utf-8')
		msg += url

	return msg

class ReposNotifyEvent(pyinotify.ProcessEvent):
	def new_repo(self, event):
		flags = pyinotify.EventsCodes.ALL_FLAGS
		heads = os.path.join(event.pathname, 'refs/heads')
		tags = os.path.join(event.pathname, 'refs/tags')

		i = 0
		# Wait max 10 seconds for refs/heads/ and refs/tags/ to appear.
		while not os.access(heads, os.F_OK) and not os.access(tags, os.F_OK) and i < 10:
			time.sleep(1)
			i += 1
		if not os.access(heads, os.F_OK):
			self.bot.gitmsg('Repo %s was found but couldn''t locate refs/heads/ or refs/tags/ (repo NOT added).' % os.path.splitext(event.name)[0])
			return

		repos[event.pathname] = NotifyRepo(self.bot, event.pathname)
		self.bot.gitmsg('New repo: %s' % os.path.splitext(event.name)[0])

	def updated_repo(self, event):
		pathname = event.pathname
		while len(pathname) > 1 and not pathname in repos:
			pathname = os.path.dirname(pathname)
		if len(pathname) == 1 or not pathname in repos:
			return

		# Check for lock file
		if event.pathname.endswith('.lock'):
			newname = event.pathname[:-5]
			i = 0
			# Wait max 10 seconds until file is moved
			while not os.access(newname, os.F_OK) and os.access(event.pathname, os.F_OK) and i < 10:
				time.sleep(1)
				i += 1

			# Assume deleted if neither file exists.
			if not os.access(newname, os.F_OK) and not os.access(event.pathname, os.F_OK):
				if os.path.dirname(newname).endswith('refs/heads'):
					del repos[pathname].heads[os.path.basename(newname)]
					self.bot.gitmsg('Branch \002%s\002 from repo \002%s\002 was deleted.' %
							(os.path.basename(newname), os.path.splitext(os.path.basename(pathname))[0]))
				elif os.path.dirname(newname).endswith('refs/tags'):
					repos[pathname].tags.remove(os.path.basename(newname))
					self.bot.gitmsg('Tag \002%s\002 from repo \002%s\002 was deleted.' %
							(os.path.basename(newname), os.path.splitext(os.path.basename(pathname))[0]))
				return

		l = repos[pathname]
		repo = l.repo
		if not repo.heads: # No branches
			return
		for h in repo.heads:
			last = l.heads[h.name] if h.name in l.heads else None
			if h.commit.hexsha != last:
				if last: # Check against last commit
					commits = list(repo.iter_commits('%s..%s' % (last, h.name)))
				elif len(h.commit.parents): # Check against parent commit
					commits = list(repo.iter_commits('%s..%s' % (h.commit.parents[0], h.name)))
				else: # Initial commit or orphan branch was pushed
					commits = list(repo.iter_commits(h.name))

				if not len(commits): # No commits
					continue

				msg = repo_commit_msg(repo, h.name, commits)
				self.bot.gitmsg(msg)
				l.heads[h.name] = h.commit.hexsha

		for t in (t for t in repo.tags if t.name not in l.tags):
			reponame = os.path.splitext(os.path.basename(repo.working_dir))[0]
			self.bot.gitmsg('New tag \002%s\002 for repo \002%s\002 points to \002%s\002' % (t.name, reponame, t.commit.hexsha[:7]))
			l.tags.append(t.name)

	def process_IN_CREATE(self, event):
		if os.path.dirname(event.pathname) == root:
			# Ignore files in root directory
			if os.path.isdir(event.pathname):
				self.new_repo(event)
		else:
			self.updated_repo(event)

	def process_IN_DELETE(self, event):
		if event.pathname in repos:
			self.bot.wm.rm_watch(repos[event.pathname].wdd.values())
			del repos[event.pathname]
			self.bot.gitmsg('Removed repo: %s' % os.path.splitext(event.name)[0])

	def process_IN_MODIFY(self, event):
		self.updated_repo(event)

def check_notifies(bot):
	bot.notifier.process_events()
	while bot.notifier.check_events():
		bot.notifier.read_events()
		bot.notifier.process_events()

class Bot(irc.IRCClient):
	nickname = options.nick

	def initial_add(self):
		global repos
		if not repos:
			repos = {}
			for path in (x for x in os.listdir(root) if x.endswith('.git')):
				fullpath = os.path.join(root, path)
				repos[fullpath] = NotifyRepo(self, fullpath)

	def gitmsg(self, msg):
		self.say(options.channel, msg)

	def signedOn(self):
		self.join(options.channel)
		self.wm = pyinotify.WatchManager()
		flags = pyinotify.EventsCodes.ALL_FLAGS
		self.wm.add_watch(root, flags['IN_DELETE'] | flags['IN_CREATE'] | flags['IN_ONLYDIR'])
		self.rne = ReposNotifyEvent()
		self.rne.bot = self
		self.notifier = pyinotify.Notifier(self.wm, self.rne, timeout = options.interval)

		self.initial_add()

		self.repeater = LoopingCall(check_notifies, self)
		self.repeater.start(options.interval)

	def get_repo(self, name, target):
		repo = [v for k, v in repos.iteritems() if os.path.basename(k).startswith(name)]
		if len(repo) == 1:
			return repo[0].repo
		elif len(repo) == 0:
			self.msg(target, 'No repo found.')
		else:
			s = [r for r in repo if os.path.splitext(os.path.basename(r.path))[0] == name]
			# Check for equal match if we're giving an ambiguous name
			if len(s) == 1:
				return s[0].repo
			self.msg(target, 'Ambiguous name: %s' % (', '.join([os.path.splitext(os.path.basename(x.path))[0] for x in repo])))

		return None

	def get_commits(self, repo, name):
		branch = None
		try:
			if '..' in name:
				commits = list(repo.iter_commits(name))
			else:
				try:
					commits = [repo.commit(name)]
					branches = [b.name for b in repo.heads if b.name == name]
					if len(branches) == 1:
						branch = branches[0]
				except git.exc.BadObject:
					commits = []
			if not len(commits):
				branches = self.get_branch(repo, name)
				tags = self.get_tags(repo, name)
				if len(branches) and len(tags):
					raise GitnoitpyAmbiguousException('Ambiguous name: %s (\002branches\002 %s tags \002%s\002' % (
						name, ', '.join((x.name for x in branches)), ', '.join((x.name for x in tags))))
				elif len(branches) > 1:
					raise GitnoitpyAmbiguousException('Ambiguous name: %s' % (', '.join((x.name for x in branches))))
				elif len(tags) > 1:
					raise GitnoitpyAmbiguousException('Ambiguous name: %s' % (', '.join((x.name for x in tags))))
				elif len(branches):
					commits, branch = [branches[0].commit], branches[0].name
				elif len(tags):
					commits = [tags[0].commit]
		except ValueError:
			return [], None
		except git.exc.BadObject:
			return [], None
		except git.exc.GitCommandError:
			return [], None

		return commits, branch

	def get_branch(self, repo, branch):
		return sorted((h for h in repo.heads if h.name.startswith(branch)), cmp = lambda a, b: cmp(a.name, b.name))

	def get_tags(self, repo, tag):
		return sorted((t for t in repo.tags if t.name.startswith(tag)), cmp = lambda a, b: cmp(a.name, b.name))

	def privmsg(self, user, channel, message):
		private = channel == self.nickname
		nick = user.split('!')[0]
		target = nick if private else channel
		messagelist = message.split()
		if len(messagelist) < 2:
			return
		if messagelist[0].startswith(self.nickname):
			cmd = messagelist[1].lower()
			if cmd == 'list':
				s = 'Repos: %s' % ', '.join([os.path.splitext(os.path.basename(x))[0] for x in repos.keys()])
				self.msg(target, s)
			elif cmd == 'show':
				if len(messagelist) < 3:
					self.msg(target, 'Usage: %s show REPO [COMMIT|BRANCH|TAG|RANGE]' % self.nickname)
					return

				repo = self.get_repo(messagelist[2].lower(), target)
				if not repo:
					return

				if len(messagelist) > 3:
					try:
						commits, branch = self.get_commits(repo, messagelist[3])
					except GitnoitpyAmbiguousException as e:
						self.msg(target, e.message)
						return
				else:
					commits, branch = self.get_commits(repo, 'master')

				if not commits:
					self.msg(target, 'No commits found.')
					return

				msg = repo_commit_msg(repo, branch, commits)
				self.msg(target, msg)
			elif cmd == 'branches':
				if not len(messagelist) == 3:
					self.msg(target, 'Usage: %s branches REPO' % self.nickname)
					return

				repo = self.get_repo(messagelist[2].lower(), target)
				if not repo:
					return

				reponame = os.path.splitext(os.path.basename(repo.working_dir))[0]
				self.msg(target, '\002%s\002 has branches %s' % (reponame, ', '.join(['\002%s\002' % h.name for h in repo.heads])))
			elif cmd == 'tags':
				if not len(messagelist) == 3:
					self.msg(target, 'Usage: %s tags REPO' % self.nickname)
					return

				repo = self.get_repo(messagelist[2].lower(), target)
				if not repo:
					return

				reponame = os.path.splitext(os.path.basename(repo.working_dir))[0]
				if not len(repo.tags):
					self.msg(target, '\002%s\002 has no tags.' % reponame)
				else:
					self.msg(target, '\002%s\002 has tags %s' % (reponame, ', '.join(['\002%s\002' % t.name for t in repo.tags])))

class BotFactory(protocol.ReconnectingClientFactory):
	protocol = Bot

if __name__ == '__main__':
	f = BotFactory()
	reactor.connectTCP(options.host, options.port, f)
	reactor.run()