#!/usr/bin/env ruby # # git-remote-monitor(1) # # The MIT License # Copyright © 2009 # * Magnus Bergmark # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # Add ../lib/ as a load path in case we are doing development $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') REQUIRED_GEMS = ['rubygems', 'meow'] begin REQUIRED_GEMS.each { |g| require g } rescue LoadError => e failed_gem = e.to_s.slice(/-- (.*)$/, 1) $stderr.puts "ERROR: Could not load #{failed_gem}." $stderr.puts "#{$0} needs the following gems:" $stderr.puts REQUIRED_GEMS.collect { |g| "\t* #{g}" }.join("\n") Kernel.exit(1) end require 'git_remote_monitor' require 'getoptlong' require 'rdoc/usage' $usage = %Q{ = Synopsis Monitors the current branch's remote for changes at a fixed interval. Changes will be fetched and you will be notified about how many commits you are behind. = Usage git remote-monitor [OPTIONS] == Options -h, --help: displays this usage information -i TIME, --interval=TIME: time, in seconds, between each fetch. Defaults to 60 seconds. == Daemon Since this utility is in an early stage, no daemon capabilities have yet been added. It is recommended that you start the command in the background (append a single & to the end of the command line) so you can keep using your term to other things while the program is running. } # # Usage information # # Shamefully stolen from RDoc#usage. We cannot use RDoc::usage because # RubyGems will call this from a wrapper, and #usage is hardcoded to look # at the top-level file instead of the current one. I have changed this # code to instead just parse a string. def usage_and_exit! markup = SM::SimpleMarkup.new flow_convertor = SM::ToFlow.new flow = markup.convert($usage, flow_convertor) options = RI::Options.instance formatter = options.formatter.new(options, "") formatter.display_flow(flow) exit(0) end # # Command line arguments parser # def parse_command_line_arguments! opts = GetoptLong.new( [ '--help', '-h', GetoptLong::NO_ARGUMENT ], [ '--interval', '-i', GetoptLong::REQUIRED_ARGUMENT] ) opts.each do |opt, arg| case opt when '--help' usage_and_exit! when '--interval' interval = arg.to_i unless interval > 10 puts "ERROR: Interval must be larger than 10" exit 1 end $interval = interval end end end # Default values for the command line options $interval = 60 begin parse_command_line_arguments! notifier = GitRemoteMonitor::Notifier.new # Hardcoded values for now monitor = GitRemoteMonitor::Monitor.new(Dir.pwd, $interval, notifier) monitor.start_monitoring! rescue => e $stderr.puts "-- Unhandled Exception! ------" $stderr.puts e $stderr.puts e.backtrace Kernel.exit(1) end