-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgblame
executable file
·34 lines (32 loc) · 1.2 KB
/
gblame
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
#!/bin/bash
# Check if the correct number of arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <file_path> <line_number>"
exit 1
fi
# Assign arguments to variables for better readability
file_path=$1
line_number=$2
# Use git blame to get the commit hash, author, and message for the specific line
git_blame_output=$(git blame -L $line_number,$line_number $file_path)
# Extract the commit hash
commit_hash=$(echo $git_blame_output | cut -d' ' -f1)
# Get the commit details
commit_details=$(git log --format="%H %an %s" | grep $commit_hash)
commit_date=$(git log --format="%at" -n 1 $commit_hash)
current_date=$(date +%s)
time_diff=$((current_date - commit_date))
# Calculate the time difference in human-readable format
if [ $time_diff -ge 86400 ]; then
time_ago="$((time_diff / 86400)) days ago"
elif [ $time_diff -ge 3600 ]; then
time_ago="$((time_diff / 3600)) hours ago"
elif [ $time_diff -ge 1800 ]; then
time_ago="$((time_diff / 60)) minutes ago"
else
time_ago="$time_diff seconds ago"
fi
echo "Commit Hash: $(echo $commit_details | cut -d' ' -f1)"
echo "Author: $(echo $commit_details | cut -d' ' -f2)"
echo "Message: $(echo $commit_details | cut -d' ' -f3-)"
echo "Date: $time_ago"