Class: GitUtilities

Inherits:
Object
  • Object
show all
Defined in:
lib/cookbook-release/git-utilities.rb

Constant Summary collapse

MAGIC_SEP =

This string is used to split one-line git commit summary it just needs to be unlikely in a commit message

'@+-+@+-+@+-+@'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitUtilities



10
11
12
13
14
15
# File 'lib/cookbook-release/git-utilities.rb', line 10

def initialize(options={})
  @tag_prefix = options[:tag_prefix] || ''
  @shellout_opts = {
    cwd: options[:cwd]
  }
end

Instance Attribute Details

#no_promptObject

Returns the value of attribute no_prompt.



8
9
10
# File 'lib/cookbook-release/git-utilities.rb', line 8

def no_prompt
  @no_prompt
end

Class Method Details

.git?(dir) ⇒ Boolean



17
18
19
# File 'lib/cookbook-release/git-utilities.rb', line 17

def self.git?(dir)
  !Mixlib::ShellOut.new('git status', cwd: dir).run_command.error?
end

Instance Method Details

#choose_remoteObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cookbook-release/git-utilities.rb', line 79

def choose_remote
  cmd = Mixlib::ShellOut.new("git remote", @shellout_opts)
  cmd.run_command
  cmd.error!
  remotes = cmd.stdout.split("\n")
  if remotes.size == 1 || @no_prompt
    puts "Choosing remote #{remotes.first}" if @no_prompt
    remotes.first
  else
    choose(*remotes)
  end
end

#clean_index!Object



34
35
36
# File 'lib/cookbook-release/git-utilities.rb', line 34

def clean_index!
  raise "All changes must be committed!" unless clean_index?
end

#clean_index?Boolean



26
27
28
29
30
31
32
# File 'lib/cookbook-release/git-utilities.rb', line 26

def clean_index?
  clean_index = Mixlib::ShellOut.new("git diff --exit-code", @shellout_opts)
  clean_index.run_command
  clean_staged = Mixlib::ShellOut.new("git diff --exit-code --cached", @shellout_opts)
  clean_staged.run_command
  !clean_index.error? && !clean_staged.error?
end

#compute_changelog(since) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cookbook-release/git-utilities.rb', line 58

def compute_changelog(since)
  # TODO use whole commit message instead of title only
  log_cmd = Mixlib::ShellOut.new("git log --pretty=\"format:%an <%ae>#{MAGIC_SEP}%s#{MAGIC_SEP}%h\" #{since}..HEAD", @shellout_opts)
  log_cmd.run_command
  log = log_cmd.stdout
  log.split("\n").map do |entry|
    author, subject, hash = entry.chomp.split(MAGIC_SEP)
    Commit.new({
      author: author,
      subject: subject,
      hash: hash
    })
  end.reject { |commit| commit[:subject] =~ /^Merge branch (.*) into/i }
end

#compute_last_releaseObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cookbook-release/git-utilities.rb', line 38

def compute_last_release

  tag = Mixlib::ShellOut.new([
    'git describe',
    "--tags",
    "--match \"#{@tag_prefix}[0-9]\.[0-9]*\.[0-9]*\""
  ].join(" "), @shellout_opts)
  tag.run_command
  last = tag.stdout.split('-').first
  unless last
    $stderr.puts "No last release found, defaulting to 0.1.0"
    last = '0.1.0'
  end
  last.to_version
end

#push_tag(version) ⇒ Object



92
93
94
95
96
97
# File 'lib/cookbook-release/git-utilities.rb', line 92

def push_tag(version)
  remote = choose_remote
  cmd = Mixlib::ShellOut.new("git push #{remote} #{@tag_prefix}#{version}", @shellout_opts)
  cmd.run_command
  cmd.error!
end

#reset_command(new_version) ⇒ Object



21
22
23
24
# File 'lib/cookbook-release/git-utilities.rb', line 21

def reset_command(new_version)
  remote = choose_remote
  "git tag -d #{new_version} ; git push #{remote} :#{new_version}"
end

#tag(version) ⇒ Object



73
74
75
76
77
# File 'lib/cookbook-release/git-utilities.rb', line 73

def tag(version)
  cmd = Mixlib::ShellOut.new("git tag #{@tag_prefix}#{version}", @shellout_opts)
  cmd.run_command
  cmd.error!
end