-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_it_in_history.rb
More file actions
executable file
·50 lines (41 loc) · 1.58 KB
/
find_it_in_history.rb
File metadata and controls
executable file
·50 lines (41 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/ruby
# Author: loveky <ylzcylx@gmail.com>
# Blog : http://loveky2012.blogspot.com
# Given a file, the path of the file in the repo and a path to local repo. Check in which commit the given file was first introduced in
#Usage: find_it_in_history.rb --file <file> --repo <path/to/repo> --path_in_repo <file/path/in/repo>
require 'digest/sha1'
require 'optparse'
found = false
commit = nil
options = {}
OptionParser.new do |opts|
opts.banner = "Find the commit who first introduced the specified version of a file"
opts.on('--file FILE', 'Which file to check') do |value|
options[:file] = value
end
opts.on('--repo REPO', 'which repo to check') do |value|
options[:repo] = value
end
opts.on('--path_in_repo PATH', 'the path of the file in repo') do |value|
options[:path_in_repo] = value
end
end.parse!
file_content = File.open(options[:file]) {|f| f.read}
file_sha1 = Digest::SHA1.hexdigest("blob #{file_content.length}\0" + file_content)
puts("#{options[:file]} => #{file_sha1}")
Dir.chdir(options[:repo])
IO.popen("git whatchanged --oneline -m -- #{options[:path_in_repo]}") do |git_whatchanged|
git_whatchanged.each_line do |line|
if line[0,1] == ':'
new_blob = (/\.\.\. ([0-9a-fA-F]{7})\.\.\./.match(line))[1]
if new_blob == file_sha1[0,7]
puts "Found blob(#{file_sha1[0,7]}) in commit " + commit
found = true
elsif found == true
exit 0
end
else
commit = line[0,7]
end
end
end