git.rb 1.46 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/env ruby

# Plugin to pull updates from git
class Git

	# This method is called when the plugin is first loaded
	def initialize( status, config, output, irc, timer )
8 9 10 11 12
		@status  = status
		@config  = config
		@output  = output
		@irc     = irc
		@timer   = timer
13 14 15 16
	end

	# Alias to pull
	def main( nick, user, host, from, msg, arguments, con )
17
		pull( nick, user, host, from, msg, arguments, con )
18 19 20
	end
	
	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
21
	def help( nick, _user, _host, _from, _msg, _arguments, con )
22 23 24
		help = [
			"This plugin allows you to pull updates from a git repo.",
			"Will only work if you cloned the bot from git.",
Cool Fire's avatar
Cool Fire committed
25 26
			"  git pull              - Pull updates.",
			"  git status            - Show some info and state."
27 28 29 30 31 32 33 34 35 36 37 38
		]

		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end
	
Cool Fire's avatar
Cool Fire committed
39
	# Pull updates from server
Cool Fire's avatar
Cool Fire committed
40
	def pull( nick, _user, host, _from, _msg, _arguments, con )
41 42 43 44 45
		if( @config.auth( host, con ) )
			res = %x( git pull ).gsub!( "\n", " " )
			@irc.notice( nick, "#{res}" )
		end
	end
Cool Fire's avatar
Cool Fire committed
46 47

	# Show some output and status
Cool Fire's avatar
Cool Fire committed
48
	def status( nick, _user, _host, _from, _msg, _arguments, _con )
Cool Fire's avatar
Cool Fire committed
49 50 51 52 53 54 55 56 57
		branch = %x( git branch | head -n 1 | awk '{print $2}' )
		status = %x( git fetch -v --dry-run 2>&1 >/dev/null | grep #{branch} )
		
		if ( status == "" )
			status = " (Local branch)"
		end

		@irc.notice( nick, "On branch #{branch}#{status}" )
	end
58
end