Module: Buildmeister::GitUtils

Included in:
Base
Defined in:
lib/buildmeister/git_utils.rb

Instance Method Summary collapse

Instance Method Details

#current_branchObject



59
60
61
62
63
# File 'lib/buildmeister/git_utils.rb', line 59

def current_branch
  branches = `git branch`.split
  i = branches.index "*"
  branches[i + 1]
end

#generate_timed_branch(prefix) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/buildmeister/git_utils.rb', line 11

def generate_timed_branch(prefix)
  branches = local_branches
  now      = Time.now
  count    = 1

  loop do
    new_branch_name = "#{prefix}-#{now.year}-#{now.month.to_s.rjust 2, '0'}-#{now.day.to_s.rjust 2, '0'}-#{count.to_s.rjust 3, '0'}"
    unless branches.include? new_branch_name
      `git checkout -b #{new_branch_name}`
      puts "Created #{new_branch_name}"
      return true
    end

    count += 1
  end
end

#local_branchesObject



51
52
53
# File 'lib/buildmeister/git_utils.rb', line 51

def local_branches
  `git branch`.split.reject { |name| name == "*" }
end

#new_experimentalObject



7
8
9
# File 'lib/buildmeister/git_utils.rb', line 7

def new_experimental
  generate_timed_branch('experimental')
end

#new_hotfixObject



3
4
5
# File 'lib/buildmeister/git_utils.rb', line 3

def new_hotfix
  generate_timed_branch('hotfix')
end

#pull_bin(bin_name = args.first) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/buildmeister/git_utils.rb', line 28

def pull_bin(bin_name = args.first)
  raise "Multiple projects are loaded. Please use the -p flag to select one project." if projects.size > 1
  
  project = projects.first
  bin     = project.bins.named(bin_name)
  
  raise ArgumentError, "#{bin_name} is not a valid bin! Must be in #{project.bins.map(&:name).join(', ')}" unless bin

  `git fetch origin`

  branches        = remote_branches
  ticket_numbers  = bin.tickets.map { |tkt| tkt.id.to_s }

  branches_to_pull = branches.select do |branch_name|
    ticket_numbers.map { |tkt_number| branch_name =~ %r|origin/#{tkt_number}| }.any?
  end

  branches_to_pull.each do |branch|
    result = `git pull origin #{branch.gsub("origin/", "")}`
    puts result
  end
end

#remote_branchesObject



55
56
57
# File 'lib/buildmeister/git_utils.rb', line 55

def remote_branches
  `git branch -r`.split.reject { |name| name == "*" }
end