lib/gitmine/gitmine.rb in gitmine-0.1.4.pre.1 vs lib/gitmine/gitmine.rb in gitmine-0.1.5

- old
+ new

@@ -18,10 +18,11 @@ Commit.new(c) end end + # TODO specs def self.branch(branch_name) issue_id = branch_name[/^\d+/] original_branch = File.read('./.git/HEAD').match(/^ref: refs\/heads\/(.+)/)[1] config = Issue.config @@ -41,14 +42,64 @@ if config['github'] note << %{ - "See on Github":https://github.com/#{config['github']}/tree/#{branch_name}} note << %{ - "Compare on Github":https://github.com/#{config['github']}/compare/#{original_branch}...#{branch_name}} end - Issue.find(issue_id).add_note(note) + puts 'Done!' if Issue.find(issue_id).add_note(note) end + # TODO specs + def self.checkout(issue_id) + if local_branch = local_branches.select { |branch| branch[/^#{issue_id}-/] }.first + run_cmd("git checkout #{local_branch}") + return + end + + if remote_branch = remote_branches.select { |branch| branch[/^#{issue_id}-/] }.first + run_cmd("git checkout -b #{remote_branch} origin/#{remote_branch}") + return + end + + raise "Can't find branch starting with #{issue_id}" + end + + # TODO specs + def self.delete(issue_id) + if remote_branch = remote_branches.select { |branch| branch[/^#{issue_id}-/] }.first + run_cmd("git push origin :#{remote_branch}") + else + raise "Can't find branch starting with #{issue_id}" + end + end + def self.run_cmd(cmd) puts cmd exit! unless system(cmd) + end + + # TODO specs + def self.local_branches + branches = [] + `git branch`.each_line do |line| + if match = line[/\d+.*$/] + branches << match + end + end + + branches + end + + # TODO specs + def self.remote_branches + run_cmd("git fetch") + + branches = [] + `git branch -r`.each_line do |line| + if match = line.match(/origin\/(\d+.*)/) + branches << match[1] + end + end + + branches end end end