-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_code.rb
74 lines (57 loc) · 1.7 KB
/
extract_code.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby
require 'open3'
require 'optparse'
def run_command(cmd)
stdout, stderr, status = Open3.capture3(cmd)
unless status.success?
puts "Error running command: #{cmd}"
puts stderr
exit 1
end
stdout
end
def escape_underscores(string)
string.gsub("_", "\\_")
end
#parameters
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: script.rb [options]"
opts.on("-c", "--compare [COMMIT]", "Compare to a specific commit or branch (default is 'master')") do |c|
options[:compare] = c || "master"
end
opts.on("-d", "--directory DIRECTORY", "Directory of the Git repository") do |d|
options[:directory] = d
end
end.parse!
# go to directory
if options[:directory]
Dir.chdir(options[:directory])
puts "Running for this directory: #{options[:directory]}"
end
# git diff defautls to master, but when we want to compare to certain branch, we can pass it as param
comparison_target = options[:compare] || "master"
diff_output = run_command("git diff #{comparison_target}")
parsed_output = ""
current_file = nil
lines_to_skip = 0
diff_output.each_line do |line|
if line.start_with?('diff --git')
parsed_output += "\\end{lstlisting}\n\n" unless parsed_output.empty?
match = line.match(/^diff --git a\/(.+?) b\/(.+?)$/)
if match
current_file = escape_underscores(match[1])
parsed_output += "\\begin{lstlisting}[caption=#{current_file}, firstnumber=1]\n"
end
lines_to_skip = 4
elsif lines_to_skip > 0
lines_to_skip -= 1
else
parsed_output += line
end
end
parsed_output += "\\end{lstlisting}\n" unless parsed_output.empty?
File.open("code.txt", "w") do |file|
file.write(parsed_output)
end
puts "Diff output written to code.txt"