Sha256: 94d84cc7fe02f9877270703b8ab10da285a18415df6cfea7468dae85961d1e1f

Contents?: true

Size: 1.45 KB

Versions: 56

Compression:

Stored size: 1.45 KB

Contents

#!/usr/bin/env ruby
# Installed by the git-story gem

require 'tempfile'

class CommitMesssageParser
  def initialize
    @line_index         = 0
    @story_number_found = false
    @message_data       = []
  end

  attr_reader :line_index

  def story_number_found?
    @story_number_found
  end

  def parse(template)
    @message_data = template.readlines
    @message_data.each do |line|
      line =~ /\[.*#\d+.*\]/ and @story_number_found = true
      if line =~ /^\s*#/
        break
      else
        @line_index += 1
      end
    end
  ensure
    @line_index = [ @line_index - 1, 0 ].max
    return self
  end

  def data
    @message_data[0..@line_index]
  end

  def footer
    @message_data[(@line_index + 1)..-1]
  end

  def total
    data + footer
  end
end

story_numbers =
  `git branch --no-color`.sub!(/^\* .*?(?:_(\d+(?:_\d+)*))$/) {
    break $1.split(/_/)
  } || []
story_numbers.map!(&:chomp)
unless story_numbers.empty?
  Tempfile.open('commit') do |output|
    File.open(ARGV.first) do |template|
      message_parsed = CommitMesssageParser.new.parse(template)
      if message_parsed.story_number_found?
        output.puts message_parsed.total
      else
        output.puts message_parsed.data, "",
          "[#{story_numbers.map { |story_number| "##{story_number}" } * ' '}]",
          "", message_parsed.footer
      end
      output.rewind
      File.open(ARGV.first, 'w') do |message|
        message.write output.read
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
git-story-workflow-1.3.0 lib/git/story/prepare-commit-msg
git-story-workflow-1.2.0 lib/git/story/prepare-commit-msg
git-story-workflow-1.1.0 lib/git/story/prepare-commit-msg
git-story-workflow-1.0.0 lib/git/story/prepare-commit-msg
git-story-workflow-0.11.3 lib/git/story/prepare-commit-msg
git-story-workflow-0.11.2 lib/git/story/prepare-commit-msg
git-story-workflow-0.11.1 lib/git/story/prepare-commit-msg
git-story-workflow-0.11.0 lib/git/story/prepare-commit-msg
git-story-workflow-0.10.0 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.12 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.11 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.10 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.9 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.8 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.7 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.6 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.5 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.4 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.3 lib/git/story/prepare-commit-msg
git-story-workflow-0.9.2 lib/git/story/prepare-commit-msg