Class: CookbookRelease::GitUtilities

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitUtilities

Returns a new instance of GitUtilities.



12
13
14
15
16
17
18
19
20
# File 'lib/cookbook-release/git-utilities.rb', line 12

def initialize(options={})
  @tag_prefix = options[:tag_prefix] || ''
  cwd = options[:cwd] || Dir.pwd
  @sub_dir = options[:sub_dir] # if nil takes the cwd
  @shellout_opts = {
    cwd: cwd
  }
  @g = Git.open(cwd)
end

Instance Attribute Details

#no_promptObject

Returns the value of attribute no_prompt.



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

def no_prompt
  @no_prompt
end

Class Method Details

.git?(dir) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.git?(dir)
  File.directory?(::File.join(dir, '.git'))
end

Instance Method Details

#_compute_last_releaseObject



43
44
45
46
47
48
49
50
51
# File 'lib/cookbook-release/git-utilities.rb', line 43

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
  tag.stdout.split('-').first
end

#choose_remoteObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cookbook-release/git-utilities.rb', line 84

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



39
40
41
# File 'lib/cookbook-release/git-utilities.rb', line 39

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

#clean_index?Boolean

Returns:

  • (Boolean)


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

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, short_sha = true) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cookbook-release/git-utilities.rb', line 66

def compute_changelog(since, short_sha = true)
  @g.log(500).object(@sub_dir).between(since, 'HEAD').map do |commit|
    message = commit.message.lines.map(&:chomp).compact.delete_if(&:empty?)
    Commit.new(
      author: commit.author.name,
      email: commit.author.email,
      subject: message.delete_at(0),
      hash: short_sha ? commit.sha[0,7] : commit.sha,
      body: message.empty? ? nil : message.join("\n"),
      is_merge_commit: commit.parents.length > 1
    )
  end.reject { |commit| commit[:is_merge_commit] }
end

#compute_last_releaseObject



57
58
59
60
61
62
63
64
# File 'lib/cookbook-release/git-utilities.rb', line 57

def compute_last_release
  last = _compute_last_release
  unless last
    $stderr.puts "No last release found, defaulting to 0.1.0"
    last = '0.1.0'
  end
  last.to_version
end

#has_any_release?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cookbook-release/git-utilities.rb', line 53

def has_any_release?
  !!_compute_last_release
end

#push_tag(version) ⇒ Object



97
98
99
100
101
102
# File 'lib/cookbook-release/git-utilities.rb', line 97

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



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

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

#tag(version) ⇒ Object



80
81
82
# File 'lib/cookbook-release/git-utilities.rb', line 80

def tag(version)
  @g.add_tag("#{@tag_prefix}#{version}")
end