-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwatch-git-diff
More file actions
executable file
·111 lines (89 loc) · 2.98 KB
/
watch-git-diff
File metadata and controls
executable file
·111 lines (89 loc) · 2.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# watch-git-diff - Continuously display the last git commit diff with color
# Usage: watch-git-diff [check_interval_seconds] [additional_git_diff_args]
# Default check interval in seconds
INTERVAL=${1:-2}
shift 2>/dev/null
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Not in a git repository"
exit 1
fi
# Get the initial HEAD commit hash
LAST_KNOWN_COMMIT=$(git rev-parse HEAD 2>/dev/null)
# Use a more compatible approach for terminal handling
setup_display() {
# Function to clear screen without flickering
clear_screen() {
clear
}
}
# Function to check if there's a new commit
check_for_new_commit() {
# Get current HEAD commit
CURRENT_COMMIT=$(git rev-parse HEAD 2>/dev/null)
# Compare with last known commit
if [[ "$CURRENT_COMMIT" != "$LAST_KNOWN_COMMIT" ]]; then
LAST_KNOWN_COMMIT=$CURRENT_COMMIT
return 0 # New commit detected
fi
return 1 # No new commit
}
# Function to display the diff with a header
show_diff() {
clear_screen
# Get the last commit hash and message
LAST_COMMIT=$(git log -1 --pretty=format:"%h - %s (%cr) by %an")
# Print header with timestamp
echo -e "\033[1;36m=== Last Commit Diff (Updated: $(date '+%Y-%m-%d %H:%M:%S')) ===\033[0m"
echo -e "\033[1;33m$LAST_COMMIT\033[0m"
echo -e "\033[1;36m=======================================================\033[0m"
echo ""
# Show the diff with color
git --no-pager diff HEAD~1 HEAD --color=always "$@"
echo ""
echo -e "\033[1;36m=== Press Ctrl+C or 'q' to exit ===\033[0m"
}
# Main execution
echo "Starting git diff watch, checking for new commits every $INTERVAL seconds..."
setup_display
# Show initial diff
show_diff "$@"
# Function to check for 'q' keypress without blocking
check_for_quit() {
# Check if input is available (non-blocking)
if read -t 0.1 -N 1 key; then
if [[ "$key" == "q" ]]; then
echo -e "\nUser pressed 'q'. Exiting..."
exit 0
fi
fi
}
# Set terminal to read input without requiring Enter key
old_stty_settings=$(stty -g)
stty -icanon min 1 time 0
# Ensure terminal settings are restored on exit
cleanup() {
stty "$old_stty_settings"
echo -e "\nExiting git diff watch"
exit 0
}
trap cleanup INT TERM EXIT
# Main loop
while true; do
# Check for new commits
if check_for_new_commit; then
# New commit detected, update the display
show_diff "$@"
echo -e "\033[1;32mNew commit detected, updated diff.\033[0m" >&2
fi
# Update status on the same line (overwrite previous status)
echo -ne "\r\033[K\033[1;36mLast check: $(date '+%H:%M:%S') | Press Ctrl+C to exit or 'q' to quit\033[0m"
# Check for 'q' keypress
check_for_quit
# Wait before next check (with smaller intervals to check for keypress)
for ((i=0; i<$INTERVAL*10; i++)); do
check_for_quit
sleep 0.1
done
done