Class: Ding::Git

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/ding/models/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#run_cmd

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={})
  @options = options
  raise "#{repo} is NOT a git repository" unless git_repo?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/ding/models/git.rb', line 5

def options
  @options
end

Instance Method Details

#branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ding/models/git.rb', line 17

def branch_exists?(branch)
  ! %x(git branch --list #{branch}).empty?
end

#branches(pattern) ⇒ Object



12
13
14
15
# File 'lib/ding/models/git.rb', line 12

def branches(pattern)
  merged = options[:merged] ? '--merged' : '--no-merged'
  %x(git branch --remote --list #{remote_version(pattern)} #{merged}).split
end

#checkout(branch) ⇒ Object



21
22
23
# File 'lib/ding/models/git.rb', line 21

def checkout(branch)
  raise "Unable to checkout #{branch}" unless run_cmd "git checkout #{branch}"
end

#create_branch(branch, checkout = true) ⇒ Object



25
26
27
28
# File 'lib/ding/models/git.rb', line 25

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_branchObject



30
31
32
# File 'lib/ding/models/git.rb', line 30

def current_branch
  %x(git rev-parse --abbrev-ref HEAD)
end

#delete_branch(branch) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ding/models/git.rb', line 34

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

#merge_branch(branch) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/ding/models/git.rb', line 48

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



57
58
59
60
61
62
# File 'lib/ding/models/git.rb', line 57

def push(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

#updateObject



64
65
66
# File 'lib/ding/models/git.rb', line 64

def update
  raise "Error synchronising with the remote" unless run_cmd "git up"
end