Module: Hoe::Git2

Defined in:
lib/hoe/git2.rb

Overview

This module is a Hoe plugin. You can set its attributes in your Rakefile Hoe spec, like this:

Hoe.plugin :git

Hoe.spec "myproj" do
 self.git_release_tag_prefix  = "REL_"
 self.git_remotes            << "myremote"
end

Tasks

git:changelog:: Print the current changelog. git:manifest:: Update the manifest with Git's file list. git:tag:: Create and push a tag.

Constant Summary collapse

VERSION =

Duh.

"1.8.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#git_release_tag_prefixObject

What do you want at the front of your release tags? [default: "v"]



28
29
30
# File 'lib/hoe/git2.rb', line 28

def git_release_tag_prefix
  @git_release_tag_prefix
end

#git_remotesObject

Which remotes do you want to push tags, etc. to? [default: %w(origin)]



33
34
35
# File 'lib/hoe/git2.rb', line 33

def git_remotes
  @git_remotes
end

Instance Method Details

#__git(command, *params) ⇒ Object



128
129
130
# File 'lib/hoe/git2.rb', line 128

def __git(command, *params)
  "git #{command.shellescape} #{params.compact.shelljoin}"
end

#__run_git(command, *params) ⇒ Object



132
133
134
# File 'lib/hoe/git2.rb', line 132

def __run_git(command, *params)
  `#{__git(command, *params)}`.strip.chomp
end

#changelog_section(code) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/hoe/git2.rb', line 182

def changelog_section(code)
  name = {
    major: "major enhancement",
    minor: "minor enhancement",
    bug: "bug fix",
    unknown: "unknown"
  }[code]

  changes = $changes[code] # standard:disable Style/GlobalVars
  count = changes.size
  name += "s" if count > 1
  name.sub!(/fixs/, "fixes")

  return if count < 1

  puts "* #{count} #{name}:"
  puts
  changes.sort.each do |line|
    puts "  * #{line}"
  end
  puts
end

#define_git2_tasksObject

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hoe/git2.rb', line 40

def define_git2_tasks # :nodoc:
  return unless __run_git("rev-parse", "--is-inside-work-tree") == "true"

  desc "Print the current changelog."
  task "git:changelog" do
    tag = ENV["FROM"] || git_tags.last
    range = [tag, "HEAD"].compact.join("..")
    now = Time.new.strftime("%Y-%m-%d")

    changes =
      __run_git("log", range, "--format=tformat:%B|||%aN|||%aE|||")
        .split("|||")
        .each_slice(3)
        .map do |msg, _author, |
        msg.split("\n").reject(&:empty?)
      end

    changes = changes.flatten

    next if changes.empty?

    $changes = Hash.new { |h, k| h[k] = [] } # standard:disable Style/GlobalVars

    codes = {
      "!" => :major,
      "+" => :minor,
      "*" => :minor,
      "-" => :bug,
      "?" => :unknown
    }

    codes_re = Regexp.escape codes.keys.join

    changes.each do |change|
      if change =~ /^\s*([#{codes_re}])\s*(.*)/
        code, line = codes[$1], $2
      else
        code, line = codes["?"], change.chomp
      end

      $changes[code] << line # standard:disable Style/GlobalVars
    end

    puts "=== #{ENV["VERSION"] || "NEXT"} / #{now}"
    puts
    changelog_section :major
    changelog_section :minor
    changelog_section :bug
    changelog_section :unknown
    puts
  end

  desc "Update the manifest with Git's file list. Use Hoe's excludes."
  task "git:manifest" do
    with_config do |config, _|
      files = __run_git("ls-files").split($/)
      files.reject! { |f| f =~ config["exclude"] }

      File.open "Manifest.txt", "w" do |f|
        f.puts files.sort.join("\n")
      end
    end
  end

  desc "Create and push a TAG (default #{git_release_tag_prefix}#{version})."
  task "git:tag" do
    tag = ENV["TAG"]
    ver = ENV["VERSION"] || version
    pre = ENV["PRERELEASE"] || ENV["PRE"]
    ver += ".#{pre}" if pre
    tag ||= "#{git_release_tag_prefix}#{ver}"

    git_tag_and_push tag
  end

  task "git:tags" do
    p git_tags
  end

  task :release_sanity do
    unless __run_git("status", "--porcelain").empty?
      abort "Won't release: Dirty index or untracked files present!"
    end
  end

  task release_to: "git:tag"
end

#git_svn?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/hoe/git2.rb', line 136

def git_svn?
  File.exist?(File.join(__run_git("rev-parse", "--show-toplevel"), ".git/svn"))
end

#git_tag_and_push(tag) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hoe/git2.rb', line 140

def git_tag_and_push tag
  msg = "Tagging #{tag}."

  if git_svn?
    sh __git("svn", "tag", tag, "-m", msg)
  else
    flags =
      if __run_git("config", "--get", "user.signingkey").empty?
        nil
      else
        "-s"
      end

    sh __git("tag", flags, "-f", tag, "-m", msg)
    git_remotes.each { |remote| sh __git("push", "-f", remote, "tag", tag) }
  end
end

#git_tagsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hoe/git2.rb', line 158

def git_tags
  if git_svn?
    source = __run_git("config", "svn-remote.svn.tags")

    unless source =~ %r{refs/remotes/(.*)/\*$}
      abort "Can't discover git-svn tag scheme from #{source}"
    end

    prefix = $1

    __run_git("branch", "-r")
      .split($/)
      .collect { |t| t.strip }
      .select { |t| t =~ %r{^#{prefix}/#{git_release_tag_prefix}} }
  else
    flags = %w[--date-order --simplify-by-decoration --pretty=format:%H]
    hashes = __run_git("log", *flags).split($/).reverse
    names = __run_git("name-rev", "--tags", *hashes).split($/)
    names = names.map { |s| s[/tags\/(v.+)/, 1] }.compact
    names = names.map { |s| s.sub(/\^0$/, "") }
    names.select { |t| t =~ %r{^#{git_release_tag_prefix}} }
  end
end

#initialize_git2Object

:nodoc:



35
36
37
38
# File 'lib/hoe/git2.rb', line 35

def initialize_git2 # :nodoc:
  self.git_release_tag_prefix = "v"
  self.git_remotes = %w[origin]
end