blob: 2b936c7b85823868faf5950da97b18e151522891 (
plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#!/bin/bash
pidof transmission-daemon >/dev/null && exit
# Directories containing Git repositories
DOTFILES_REPOS="$HOME/.dotfiles"
SUCKLESS_REPOS="$HOME/.local/src/suckless"
PRIVATE_REPOS="$HOME/Private/repos"
PUBLIC_REPOS="$HOME/Public/repos"
# Icon indicators
DOTFILES_ICON="⚙️"
SUCKLESS_ICON="🛠"
PRIVATE_ICON="🏠"
PUBLIC_ICON="🏢"
# Function to parse Git status and format symbols
get_git_status_symbols() {
git status --porcelain | awk '
{
if ($1 == "??") {
changes["?"]++
} else if ($1 ~ /^[MADR]$/) {
changes[$1]++
}
}
END {
for (change in changes) {
printf "%s%s", change, changes[change]
}
}'
}
# Function to check for unpushed commits
get_unpushed_commits() { git cherry -v 2>/dev/null | wc -l; }
# Function to check for unpulled commits
get_unpulled_commits() { git rev-list --count HEAD..@{upstream}; }
# Function to check Git repository status for multiple repos
check_multi_repo_status() {
local dir="$1"
local icon="$2"
local status=""
local changed_repos=""
while IFS= read -r git_dir; do
local repo_dir="${git_dir%/.git}"
cd "$repo_dir" || continue
changes=$(get_git_status_symbols)
unpushed=$(get_unpushed_commits)
unpulled=$(get_unpulled_commits)
if [ -n "$changes" ] || [ "$unpushed" -gt 0 ] || [ "$unpulled" -gt 0 ]; then
status+="$icon$changes"
[ "$unpushed" -gt 0 ] && status+="↑$unpushed"
[ "$unpulled" -gt 0 ] && status+="↓$unpulled"
status+=" "
changed_repos+="$repo_dir"
fi
done < <(find "$dir" -mindepth 2 -maxdepth 2 -type d -name ".git" 2>/dev/null)
printf "%s%s" "$status" "$changed_repos"
}
# Function to check Git repository status for a single repository
check_single_repo_status() {
local dir="$1"
local icon="$2"
local repo_status=""
local changed_repo=""
if [ -d "$dir/.git" ]; then
cd "$dir" || return
changes=$(get_git_status_symbols)
unpushed=$(get_unpushed_commits)
unpulled=$(get_unpulled_commits)
if [ -n "$changes" ] || [ "$unpushed" -gt 0 ] || [ "$unpulled" -gt 0 ]; then
repo_status+="$icon$changes"
[ "$unpushed" -gt 0 ] && repo_status+="↑$unpushed"
[ "$unpulled" -gt 0 ] && repo_status+="↓$unpulled"
repo_status+=" "
changed_repo="$dir"
fi
fi
printf "%s%s" "$repo_status" "$changed_repo"
}
# Check statuses for repositories
dotfiles_status=$(check_single_repo_status "$DOTFILES_REPOS" "$DOTFILES_ICON" | awk -F' ' '{print $1}')
dotfiles_changes=$(check_single_repo_status "$DOTFILES_REPOS" "$DOTFILES_ICON" | awk -F' ' '{print $2}')
suckless_status=$(check_single_repo_status "$SUCKLESS_REPOS" "$SUCKLESS_ICON" | awk -F' ' '{print $1}')
suckless_changes=$(check_single_repo_status "$SUCKLESS_REPOS" "$SUCKLESS_ICON" | awk -F' ' '{print $2}')
private_status=$(check_multi_repo_status "$PRIVATE_REPOS" "$PRIVATE_ICON" | awk -F' ' '{print $1}')
private_changes=$(check_multi_repo_status "$PRIVATE_REPOS" "$PRIVATE_ICON" | awk -F' ' '{print $2}')
public_status=$(check_multi_repo_status "$PUBLIC_REPOS" "$PUBLIC_ICON" | awk -F' ' '{print $1}')
public_changes=$(check_multi_repo_status "$PUBLIC_REPOS" "$PUBLIC_ICON" | awk -F' ' '{print $1}')
[ -f /tmp/gitsync ] && rm -f /tmp/gitsync
# Combine statuses
output=""
[ -n "$dotfiles_status" ] && output+="$dotfiles_status "
[ -n "$suckless_status" ] && output+="$suckless_status "
[ -n "$private_status" ] && output+="$private_status "
[ -n "$public_status" ] && output+="$public_status "
# Trim trailing spaces and display output
output="${output%"${output##*[! ]}"}"
[ -n "$output" ] && (cat /tmp/gitsync 2>/dev/null || echo "$output")
openrepos() {
all_changed_repos="$dotfiles_changes"$'\n'"$suckless_changes"$'\n'"$private_changes"$'\n'"$public_changes"
[ -n "$all_changed_repos" ] && exec "$TERMINAL" -e opensessions "$(echo "$all_changed_repos" | grep -v '^$')"
}
# Handle button actions
case "$BLOCK_BUTTON" in
1) openrepos ;;
3) notify-send " Git module" "\- Shows git repositories changes
- Left click opens changed repositories" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; # Launch editor for the script
esac
|