import subprocess

from CommandTemplate import CommandTemplate
from IrcMessage import IrcMessage
import SharedFunctions


class Command(CommandTemplate):
	triggers = ['update']
	helptext = "Gets the latest files from the GitHub repository, if there are any"
	adminOnly = True

	lastCommitHash = ""

	def onLoad(self):
		#Set the stored hash to the latest local one
		output = subprocess.check_output(['git', 'show', '--format=oneline', '--no-patch'])
		self.lastCommitHash = output.split(" ", 1)[0]
	
	def execute(self, message):
		"""
		:type message: IrcMessage
		"""

		replytext = u""
		#First just get anything new, if there is any
		output = subprocess.check_output(['git', 'pull'])
		if output.startswith("Already up-to-date."):
			replytext = u"No new updates"
		else:
			maxUpdatesToDisplay = 5
			#New files, new updates! Check what they are
			output = subprocess.check_output(['git', 'log', '--format=oneline'])
			outputLines = output.splitlines()
			commitMessages = []
			linecount = 0
			for line in outputLines:
				lineparts = line.split(" ", 1)
				#If we've reached a commit we've already mentioned, stop the whole thing
				if lineparts[0] == self.lastCommitHash:
					break
				linecount += 1
				#Only show the last few commit messages, but keep counting lines regardless
				if len(commitMessages) < maxUpdatesToDisplay:
					commitMessages.append(lineparts[1])
			if linecount == 1:
				replytext = u"One new commit: {}".format(commitMessages[0])
			else:
				commitMessages.reverse()  #Otherwise the messages are ordered new to old
				replytext = u"{:,} new commits: {}".format(linecount, SharedFunctions.joinWithSeparator(commitMessages))
				if linecount > maxUpdatesToDisplay:
					replytext += u"; {:,} older ones".format(linecount - maxUpdatesToDisplay)
			#Set the last mentioned hash to the newest one
			self.lastCommitHash = outputLines[0].split(" ", 1)[0]

		message.reply(replytext, "say")

@@ -290,16 +290,44 @@
 ash = %22%22
+%0A%09MAX_UPDATES_TO_DISPLAY = 5
 %0A%0A%09def o
@@ -596,26 +596,8 @@
 %22%22%0A%0A
-%09%09replytext = u%22%22%0A
 %09%09#F
@@ -642,25 +642,16 @@
 s any%0A%09%09
-output = 
 subproce
@@ -689,178 +689,62 @@
 )%0A%09%09
-if output.startswith(%22Already up-to-date.%22):%0A%09%09%09replytext = u%22No new updates%22%0A%09%09else:%0A%09%09%09maxUpdatesToDisplay = 5%0A%09%09%09#New files, new updates! Check what they are
+#Check if any new updates were pulled in
 %0A%09%09
-%09
 output
+Lines
  = s
@@ -805,32 +805,8 @@
 e'%5D)
-%0A%09%09%09outputLines = output
 .spl
@@ -815,17 +815,16 @@
 lines()%0A
-%09
 %09%09commit
@@ -839,17 +839,16 @@
  = %5B%5D%0A%09%09
-%09
 linecoun
@@ -853,17 +853,16 @@
 unt = 0%0A
-%09
 %09%09for li
@@ -880,17 +880,16 @@
 tLines:%0A
-%09
 %09%09%09linep
@@ -917,17 +917,16 @@
 , 1)%0A%09%09%09
-%09
 #If we'v
@@ -989,25 +989,24 @@
 le thing%0A%09%09%09
-%09
 if lineparts
@@ -1033,17 +1033,16 @@
 itHash:%0A
-%09
 %09%09%09%09brea
@@ -1046,17 +1046,16 @@
 reak%0A%09%09%09
-%09
 linecoun
@@ -1061,17 +1061,16 @@
 nt += 1%0A
-%09
 %09%09%09#Only
@@ -1140,17 +1140,16 @@
 ardless%0A
-%09
 %09%09%09if le
@@ -1168,38 +1168,46 @@
 ages) %3C 
-maxUpdatesToDisplay
+self.MAX_UPDATES_TO_DISPLAY 
 :%0A
-%09
 %09%09%09%09comm
@@ -1240,17 +1240,112 @@
 s%5B1%5D)%0A%09%09
-%09
+if linecount == 0:%0A%09%09%09replytext = u%22No updates found, seems I'm up-to-date. I feel so hip!%22%0A%09%09el
 if linec
@@ -1355,17 +1355,16 @@
 t == 1:%0A
-%09
 %09%09%09reply
@@ -1424,16 +1424,14 @@
 )%0A%09%09
-%09
 else:%0A
-%09
 %09%09%09c
@@ -1505,17 +1505,16 @@
  old%0A%09%09%09
-%09
 replytex
@@ -1613,17 +1613,16 @@
 es))%0A%09%09%09
-%09
 if linec
@@ -1632,30 +1632,37 @@
 t %3E 
-maxUpdatesToDisplay
+self.MAX_UPDATES_TO_DISPLAY
 :%0A
-%09
 %09%09%09%09
@@ -1718,30 +1718,37 @@
 t - 
-maxUpdatesToDisplay
+self.MAX_UPDATES_TO_DISPLAY
 )%0A
-%09
 %09%09#S
@@ -1792,17 +1792,16 @@
 est one%0A
-%09
 %09%09self.l

Fix and improve new commit checking