Skip to content

Instantly share code, notes, and snippets.

@cgarrovillo
Last active May 1, 2024 23:13
Show Gist options
  • Save cgarrovillo/c6b04e84db6b077ff192298c7aa19191 to your computer and use it in GitHub Desktop.
Save cgarrovillo/c6b04e84db6b077ff192298c7aa19191 to your computer and use it in GitHub Desktop.
verify that the total insertions/deletions for both unstaged (tracked & untracked) and staged files are under 400 lines
git_diff_summary() {
# Check if the current directory is a git repository
git rev-parse --is-inside-work-tree > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Not a git repository."
return 1 # Exit the function if not in a git repository
fi
echo "Staged Changes:"
local last_line_staged=$(git diff --staged --stat | tail -n 1)
local colored_line_staged=$(echo "$last_line_staged" | \
sed -e 's/\([0-9]*\) insertion(+)/\x1b[32m&\x1b[0m/g' \
-e 's/\([0-9]*\) deletion(-)/\x1b[31m&\x1b[0m/g')
echo "$colored_line_staged"
echo "Unstaged Changes:"
local last_line_unstaged=$(git diff --stat | tail -n 1)
local colored_line_unstaged=$(echo "$last_line_unstaged" | \
sed -e 's/\([0-9]*\) insertion(+)/\x1b[32m&\x1b[0m/g' \
-e 's/\([0-9]*\) deletion(-)/\x1b[31m&\x1b[0m/g')
echo "$colored_line_unstaged"
# Count new file lines separately and accurately
local new_file_lines=$(git ls-files --others --exclude-standard | grep -vE '^\./\.' | xargs -I{} sh -c 'test -f "{}" && wc -l "{}"' | awk '{total += $1} END {print total}')
new_file_lines=${new_file_lines:-0}
# Extract total insertions and deletions
local staged_insertions=$(echo "$last_line_staged" | awk '{print $4}' | grep -o '[0-9]*')
local staged_deletions=$(echo "$last_line_staged" | awk '{print $6}' | grep -o '[0-9]*')
local unstaged_insertions=$(echo "$last_line_unstaged" | awk '{print $4}' | grep -o '[0-9]*')
local unstaged_deletions=$(echo "$last_line_unstaged" | awk '{print $6}' | grep -o '[0-9]*')
staged_insertions=${staged_insertions:-0}
staged_deletions=${staged_deletions:-0}
unstaged_insertions=${unstaged_insertions:-0}
unstaged_deletions=${unstaged_deletions:-0}
# Calculate total changes without double-counting
local total_changes=$((staged_insertions + staged_deletions + unstaged_insertions + unstaged_deletions + new_file_lines))
# Echo total changes in color
if [ $total_changes -lt 400 ]; then
echo -e "\x1b[32mTotal changes: $total_changes\x1b[0m"
else
echo -e "\x1b[31mTotal changes: $total_changes\x1b[0m"
fi
}
@cgarrovillo
Copy link
Author

image image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment