class Ding::Git
Attributes
options[R]
Public Class Methods
new(options={})
click to toggle source
# File lib/ding/models/git.rb, line 7 def initialize(options={}) @options = options raise "#{repo} is NOT a git repository" unless git_repo? end
Public Instance Methods
branch_exists?(branch)
click to toggle source
# File lib/ding/models/git.rb, line 26 def branch_exists?(branch) ! %x(git branch --list #{branch}).empty? end
branch_in_context(branch)
click to toggle source
# File lib/ding/models/git.rb, line 79 def branch_in_context(branch) if options[:local] local_version(branch) else remote_version(branch) end end
branches(pattern, remote=true)
click to toggle source
# File lib/ding/models/git.rb, line 20 def branches(pattern, remote=true) merged = options[:merged] ? '--merged' : '--no-merged' remote = '--remote' if remote %x(git branch #{remote} --list #{remote_version(pattern)} #{merged}).split end
checkout(branch)
click to toggle source
# File lib/ding/models/git.rb, line 30 def checkout(branch) raise "Unable to checkout #{branch}" unless run_cmd "git checkout #{branch}" end
commit_parents(branch)
click to toggle source
# File lib/ding/models/git.rb, line 43 def commit_parents(branch) remote_branch = remote_version(branch) %x(git rev-list --parents -n 1 #{remote_branch}).split.drop(1) if branch_exists?(remote_branch) end
create_branch(branch, checkout=true)
click to toggle source
# File lib/ding/models/git.rb, line 34 def create_branch(branch, checkout=true) raise "Unable to create #{branch}" unless run_cmd "git branch --no-track #{branch}" checkout(branch) if checkout end
current_branch()
click to toggle source
# File lib/ding/models/git.rb, line 39 def current_branch %x(git rev-parse --abbrev-ref HEAD) end
delete_branch(branch)
click to toggle source
# File lib/ding/models/git.rb, line 48 def delete_branch(branch) local_branch = local_version(branch) remote_branch = remote_version(branch) raise "You are not allowed to delete #{local_branch}" if Ding::SACROSANCT_BRANCHES.include?(local_branch) if branch_exists?(local_branch) branch_cmd = "git branch #{options[:force] ? '-D' : '-d'} #{local_branch}" raise "Unable to delete #{local_branch}" unless run_cmd branch_cmd end if branch_exists?(remote_branch) branch_cmd = "git push #{remote_name} :#{local_branch} #{options[:force] ? '-f' : ''}" raise "Unable to delete #{remote_branch}" unless run_cmd branch_cmd end end
is_dirty?()
click to toggle source
# File lib/ding/models/git.rb, line 98 def is_dirty? ! %x(git status -s).empty? end
local_branches(pattern)
click to toggle source
# File lib/ding/models/git.rb, line 12 def local_branches(pattern) branches pattern, false end
merge_branch(branch)
click to toggle source
# File lib/ding/models/git.rb, line 62 def merge_branch(branch) raise "Can't merge into protected branch #{current_branch}" if Ding::SACROSANCT_BRANCHES.include?(current_branch) success = !!(run_cmd "git merge -m 'Merge branch #{branch} into #{current_branch}' #{branch}") unless success run_cmd 'git merge --abort' end success end
push(branch)
click to toggle source
# File lib/ding/models/git.rb, line 71 def push(branch) raise "Can't push to protected branch #{branch}" if Ding::SACROSANCT_BRANCHES.include?(branch) checkout branch push_cmd = "git push #{remote_name} #{branch}" push_cmd << " --force" if options[:force] raise "Unable to push #{branch} branch!" unless run_cmd push_cmd end
remote_branches(pattern)
click to toggle source
# File lib/ding/models/git.rb, line 16 def remote_branches(pattern) branches pattern, true end
reset_local_state()
click to toggle source
# File lib/ding/models/git.rb, line 92 def reset_local_state run_cmd 'git rebase --abort' run_cmd 'git merge --abort' run_cmd 'git reset --hard' end
update()
click to toggle source
# File lib/ding/models/git.rb, line 87 def update command = options[:local] ? 'git up' : 'git fetch --all' raise "Error synchronising with the remote" unless run_cmd command end
Private Instance Methods
git_repo?()
click to toggle source
# File lib/ding/models/git.rb, line 104 def git_repo? run_cmd "git status" end
is_remote?(branch)
click to toggle source
# File lib/ding/models/git.rb, line 128 def is_remote?(branch) branch.start_with?(remote_prefix) end
local_version(branch)
click to toggle source
# File lib/ding/models/git.rb, line 120 def local_version(branch) if is_remote?(branch) branch.gsub(remote_name, '') else branch end end
remote_name()
click to toggle source
# File lib/ding/models/git.rb, line 132 def remote_name @remote_name ||= %x(git remote).chomp end
remote_prefix()
click to toggle source
# File lib/ding/models/git.rb, line 136 def remote_prefix "#{remote_name}/" end
remote_version(branch)
click to toggle source
# File lib/ding/models/git.rb, line 112 def remote_version(branch) if is_remote?(branch) branch else "#{remote_prefix}#{branch}" end end
repo()
click to toggle source
# File lib/ding/models/git.rb, line 108 def repo @repo || Dir.pwd end