Last active
January 30, 2024 15:56
-
-
Save coderberry/382a7fad7623214e54da800c73799611 to your computer and use it in GitHub Desktop.
Calculate the diff of a PR
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
#!/usr/bin/env ruby | |
# Count lines of code in a pull request | |
# Usage: | |
# ./b/cloc_pr 22621 | |
# | |
# Code: +1212 / -208 | |
# Blank Lines: +241 / -18 | |
# Files: +17 / -0 | |
# Modified Files: 38 | |
# Comments: +287 / -18 | |
# | |
def count_lines(pull_number) | |
diff = `gh pr diff #{pull_number} --color=never` | |
# Initialize counters | |
data = { | |
loggers_added: 0, | |
code_lines_added: 0, | |
code_lines_removed: 0, | |
blank_lines_added: 0, | |
blank_lines_removed: 0, | |
files_added: 0, | |
files_removed: 0, | |
modified_files: 0, | |
comment_lines_added: 0, | |
comment_lines_removed: 0 | |
} | |
# Analyze the diff | |
diff.each_line do |line| | |
if line =~ /\+\s*#/ | |
data[:comment_lines_added] += 1 | |
elsif line =~ /-\s*#/ | |
data[:comment_lines_removed] += 1 | |
elsif line =~ /\+\s*logger\./ | |
data[:loggers_added] += 1 | |
elsif line.strip == '+' | |
data[:blank_lines_added] += 1 | |
elsif line.strip == '-' | |
data[:blank_lines_removed] += 1 | |
elsif line.start_with?('--- /dev/null') | |
data[:files_added] += 1 | |
elsif line.start_with?('+++ b/dev/null') | |
data[:files_removed] += 1 | |
elsif line.start_with?('+++ b/') | |
data[:modified_files] += 1 | |
elsif line.start_with?('+') | |
data[:code_lines_added] += 1 | |
elsif line.start_with?('-') | |
data[:code_lines_removed] += 1 | |
end | |
end | |
data | |
end | |
def minus(s) = "\e[1m\e[31m-#{s}\e[0m" | |
def plus(s) = "\e[1m\e[32m+#{s}\e[0m" | |
# Usage | |
pull_number = ARGV[0] | |
data = count_lines(pull_number) | |
puts "Code: #{plus data[:code_lines_added]} / #{minus data[:code_lines_removed]}" | |
puts "Blank Lines: #{plus data[:blank_lines_added]} / #{minus data[:blank_lines_removed]}" | |
puts "Files: #{plus data[:files_added]} / #{minus data[:files_removed]}" | |
puts "Modified Files: #{data[:modified_files]}" | |
puts "Comments: #{plus data[:comment_lines_added]} / #{minus data[:comment_lines_removed]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment