lib/git-up.rb in git-up-0.3.1 vs lib/git-up.rb in git-up-0.3.2

- old
+ new

@@ -1,16 +1,17 @@ require 'colored' require 'grit' class GitUp def initialize(args) - @repo = Grit::Repo.new(File.expand_path(".")) - @head = @repo.head end def run + get_repo + system "git fetch" + raise GitError, "`git fetch` failed" unless $? == 0 with_stash do returning_to_current_branch do col_width = @repo.branches.map { |b| b.name.length }.max + 1 @@ -41,15 +42,24 @@ rebase(remote) end end end rescue GitError => e - puts e.message.red - puts "Here's what Git said:".red - puts e.output + puts e.message + exit 1 end + def get_repo + git_dir = `git rev-parse --git-dir` + + if $? == 0 + @repo = Grit::Repo.new(File.dirname(git_dir)) + else + raise GitError, "We don't seem to be in a git repository." + end + end + def remote_for_branch(branch) remote_name = @repo.config["branch.#{branch.name}.remote"] || "origin" @repo.remotes.find { |r| r.name == "#{remote_name}/#{branch.name}" } end @@ -118,14 +128,22 @@ def on_branch?(branch_name=nil) @repo.head.respond_to?(:name) and @repo.head.name == branch_name end class GitError < StandardError - attr_reader :output + def initialize(message, output=nil) + @msg = "#{message.red}" - def initialize(message, output) - super(message) - @output = output + if output + @msg << "\n" + @msg << "Here's what Git said:".red + @msg << "\n" + @msg << output + end + end + + def message + @msg end end end