Class: Ding::Git

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Git

Returns a new instance of Git.



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

def initialize(options={})
  raise "#{repo} is NOT a git repository" unless git_repo?
  @options = options
end

Instance Method Details

#branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#branches(pattern) ⇒ Object



9
10
11
12
# File 'lib/ding/models/git.rb', line 9

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

#checkout(branch) ⇒ Object



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

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

#create_branch(branch) ⇒ Object



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

def create_branch(branch)
  raise "Unable to create #{branch}" unless run_cmd "git branch --no-track #{branch}"
end

#current_branchObject



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

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

#delete_branch(branch) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ding/models/git.rb', line 30

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



44
45
46
47
48
49
50
51
# File 'lib/ding/models/git.rb', line 44

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



53
54
55
56
57
58
# File 'lib/ding/models/git.rb', line 53

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



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

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