Class: Ding::Git
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #branch_exists?(branch) ⇒ Boolean
- #branch_in_context(branch) ⇒ Object
- #branches(pattern, remote = true) ⇒ Object
- #checkout(branch) ⇒ Object
- #commit_parents(branch) ⇒ Object
- #create_branch(branch, checkout = true) ⇒ Object
- #current_branch ⇒ Object
- #delete_branch(branch) ⇒ Object
-
#initialize(options = {}) ⇒ Git
constructor
A new instance of Git.
- #is_dirty? ⇒ Boolean
- #local_branches(pattern) ⇒ Object
- #merge_branch(branch) ⇒ Object
- #push(branch) ⇒ Object
- #remote_branches(pattern) ⇒ Object
- #reset_local_state ⇒ Object
- #update ⇒ Object
Methods included from Helpers
Constructor Details
#initialize(options = {}) ⇒ Git
Returns a new instance of Git.
7 8 9 10 |
# File 'lib/ding/models/git.rb', line 7 def initialize(={}) @options = raise "#{repo} is NOT a git repository" unless git_repo? end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/ding/models/git.rb', line 5 def @options end |
Instance Method Details
#branch_exists?(branch) ⇒ Boolean
26 27 28 |
# File 'lib/ding/models/git.rb', line 26 def branch_exists?(branch) ! %x(git branch --list #{branch}).empty? end |
#branch_in_context(branch) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/ding/models/git.rb', line 79 def branch_in_context(branch) if [:local] local_version(branch) else remote_version(branch) end end |
#branches(pattern, remote = true) ⇒ Object
20 21 22 23 24 |
# File 'lib/ding/models/git.rb', line 20 def branches(pattern, remote=true) merged = [:merged] ? '--merged' : '--no-merged' remote = '--remote' if remote %x(git branch #{remote} --list #{remote_version(pattern)} #{merged}).split end |
#checkout(branch) ⇒ Object
30 31 32 |
# 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) ⇒ Object
43 44 45 46 |
# 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) ⇒ Object
34 35 36 37 |
# 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 ⇒ Object
39 40 41 |
# File 'lib/ding/models/git.rb', line 39 def current_branch %x(git rev-parse --abbrev-ref HEAD) end |
#delete_branch(branch) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 #{[: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} #{[:force] ? '-f' : ''}" raise "Unable to delete #{remote_branch}" unless run_cmd branch_cmd end end |
#is_dirty? ⇒ Boolean
98 99 100 |
# File 'lib/ding/models/git.rb', line 98 def is_dirty? ! %x(git status -s).empty? end |
#local_branches(pattern) ⇒ Object
12 13 14 |
# File 'lib/ding/models/git.rb', line 12 def local_branches(pattern) branches pattern, false end |
#merge_branch(branch) ⇒ Object
62 63 64 65 66 67 68 69 |
# 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) ⇒ Object
71 72 73 74 75 76 77 |
# 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 [:force] raise "Unable to push #{branch} branch!" unless run_cmd push_cmd end |
#remote_branches(pattern) ⇒ Object
16 17 18 |
# File 'lib/ding/models/git.rb', line 16 def remote_branches(pattern) branches pattern, true end |
#reset_local_state ⇒ Object
92 93 94 95 96 |
# 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 ⇒ Object
87 88 89 90 |
# File 'lib/ding/models/git.rb', line 87 def update command = [:local] ? 'git up' : 'git fetch --all' raise "Error synchronising with the remote" unless run_cmd command end |