Skip to content

Commit

Permalink
Use Versionomy to handle correct version sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
reinh committed Mar 18, 2010
1 parent 7fb9aca commit 1b1ec50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec = Gem::Specification.new do |s|
s.description = s.summary
s.authors = AUTHORS
s.executables = ['git-changelog']

s.add_dependency('versionomy')
s.autorequire = GEM
s.files = %w(LICENSE README Rakefile bin/git-changelog)
end
Expand Down
86 changes: 22 additions & 64 deletions bin/git-changelog
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
#!/usr/bin/env ruby -wKU
require 'optparse'
require 'versionomy'

def warn(message)
STDERR.puts "*" * 50
STDERR.puts "Warning: #{message}"
STDERR.puts "*" * 50
end

def error(message)
STDERR.puts "*" * 50
STDERR.puts "Error: #{message}"
STDERR.puts "*" * 50
def error(msg)
STDERR.puts "[E]\t#{msg}"
exit 1
end

def ask(question, default=nil, valid_response=nil, invalid_message=nil)
loop do
print "#{question}"
print " [#{default}]" if default
print ": "
answer = STDIN.gets.chomp
answer = default if default && answer.empty?
valid = false
valid = true if valid_response.nil?
valid = true if valid_response.respond_to?(:include?) && valid_response.include?(answer)
valid = true if valid_response.respond_to?(:match) && valid_response.match(answer)
if valid
return answer
else
if valid_response.is_a?(Array)
puts invalid_message || begin
print "Invalid answer, please try again."
print " Valid answers include:\n"
puts valid_response
end
elsif valid_response.is_a?(Regexp)
puts invalid_message || "Invalid format for answer, please try again."
else
puts invalid_message || "Invalid answer, please try again."
end
end
end
end

def assert_is_git_repo
error "Not a git repository" unless is_git_repo?
end
Expand All @@ -53,39 +17,37 @@ end

# e.g. v1.4.3
def valid_version_regexp
/^v?\d+\.\d+\.\d+/
/^v?\d+\.\d+/
end

def git(*args)
out = `git #{args.join(' ')}`
raise "Failed git command" unless $?.success?
out
end

# Find all version tags
def get_tags
version_regexp = valid_version_regexp
%x{git tag}.split.grep(version_regexp).sort_by{|v| v.split('.').map{|nbr| nbr[/\d+/].to_i}}.map{|tag| tag.strip}
%x{git tag}.split.grep(valid_version_regexp).map{|tag| Versionomy.parse(tag)}.sort.reverse.map{|t| t.to_s}
end

def annotate!(options)
limit = options[:limit].nil? ? 20 : options[:limit]
limit = options[:limit] || 20

assert_is_git_repo
tags = get_tags.reverse
tags = get_tags

error "No version tags available." if tags.empty?

first = tags.index(options[:to]) || 0
last = tags.index(options[:from]) || tags.size

if options[:all]
start_index = 0
end_index = tags.length - 1
else
start_tag = options[:from] || ask("Start at which tag?", tags[0], tags)
start_index = tags.index(start_tag)
end_tag = options[:to] || ask("End at which tag?", tags[start_index + 1] || tags[start_index], tags)
end_index = tags.index(end_tag) + 1 # include end tag
end

start_index.upto(end_index-1) do |i|
start = tags[i]
tags[first..last].each_with_index do |start, i|
finish = tags[i+1]
range = ''
range << "refs/tags/#{finish}.." if finish # log until end tag if there is an end tag
range << "refs/tags/#{start}"
log = `git log --no-merges --pretty=format:"%h %s" #{range}`.strip.split("\n")
log = git(%{log --no-merges --pretty=format:"%h %s" #{range}}).strip.split("\n")
next if log.empty?
puts "#{start}"
puts "=" * start.length
Expand All @@ -97,7 +59,7 @@ end

options = {}
OptionParser.new do |opts|
opts.banner = "Git Changelog - Show a list of changes by version."
opts.banner = "git changelog - Show a list of changes by version."

opts.separator ''

Expand All @@ -119,17 +81,13 @@ OptionParser.new do |opts|
options[:limit] = false
end

opts.on('-a', '--all', 'Show all versions') do
options[:all] = true
end

opts.on('--from [VERSION]',
'Start changelog at this version. Defaults to most recent version.') do |from|
'Start changelog at this version. Defaults to first version.') do |from|
options[:from] = from
end

opts.on('--to [VERSION]',
'End changelog at this version. Defaults to second most recent version.') do |to|
'End changelog at this version. Defaults to most recent version.') do |to|
options[:to] = to
end
end.parse!
Expand Down

0 comments on commit 1b1ec50

Please sign in to comment.