Created
January 4, 2014 09:31
-
-
Save hitode909/8253435 to your computer and use it in GitHub Desktop.
リポジトリ内で指定した単語が書かれた時期を調べる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'shellwords' | |
class AnnotateLine | |
attr_reader :content, :meta | |
def initialize | |
@meta = {} | |
@content = nil | |
end | |
def add_line line | |
line.chomp! | |
if line =~ /^\t/ | |
@content = line.gsub /^\t/, '' | |
else | |
parsed = line.match /^([^ ]+) (.*)$/ | |
@meta[parsed[1]] = parsed[2] | |
end | |
end | |
def committer_time | |
Time.at @meta['committer-time'].to_i | |
end | |
end | |
class Finder | |
def initialize directory, word | |
@directory = directory | |
@word = word | |
end | |
def dates | |
annotates.map{|annotate| | |
annotate.committer_time | |
}.sort | |
end | |
private | |
def annotates | |
matched_files = `git --no-pager grep --name-only --cached --word-regexp -I --fixed-strings -e #{ Shellwords.escape(@word)} -- #{Shellwords.escape(@directory)}` | |
matched_files.each_line.map{|line| | |
path = line.chomp | |
annotates_for_file_word File.join(@directory, path), @word | |
}.flatten | |
end | |
def annotates_for_file_word file, word | |
annotate = `git annotate --line-porcelain #{file}` | |
raw_lines = annotate.split(/\n/) | |
lines = [] | |
annotate = AnnotateLine.new | |
while raw_lines.length > 0 | |
annotate.add_line raw_lines.shift | |
if annotate.content | |
lines << annotate | |
annotate = AnnotateLine.new | |
end | |
end | |
lines.select{|line| | |
line.content.include? word | |
} | |
end | |
end | |
word, directory = *ARGV | |
unless word and directory | |
warn "usage: ruby commit-dates-for-word-of-repository.rb WORD REPOSITORY" | |
exit 1 | |
end | |
Dir.chdir directory | |
finder = Finder.new(directory, word) | |
finder.dates.each{|date| | |
puts date | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment