-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbashrc_snippet.sh
79 lines (65 loc) · 2.13 KB
/
bashrc_snippet.sh
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
#!/bin/bash
# Configuration variables
GIT_SHOW_USER_INFO=true
GIT_SHOW_BRANCH=true
GIT_SHOW_COMMITS=true
GIT_SHOW_STAGED_COMMITS=true
GIT_SHOW_FILES_DIFF_COUNT=true
GIT_SHOW_UNCOMMITTED_CHANGES=true
GIT_SHOW_UNTRACKED_FILES=true
is_git_repository() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || [ -d .git ]
}
get_git_info() {
local output=""
if is_git_repository; then
local current_branch
current_branch=$(git symbolic-ref --short HEAD 2>/dev/null)
if [ "$GIT_SHOW_USER_INFO" = true ]; then
local user_name
user_name=$(git config user.name)
local user_email
user_email=$(git config user.email)
output+="($user_name <$user_email>) "
fi
if [ "$GIT_SHOW_BRANCH" = true ]; then
output+="$current_branch "
fi
if [ "$GIT_SHOW_COMMITS" = true ]; then
local commits_ahead
commits_ahead=$(git rev-list --count --left-only HEAD...@'{u}' 2>/dev/null)
if [ "$commits_ahead" != "0" ]; then
output+="↑$commits_ahead "
fi
fi
if [ "$GIT_SHOW_STAGED_COMMITS" = true ]; then
local commits_behind
commits_behind=$(git rev-list --count --right-only HEAD...@'{u}' 2>/dev/null)
if [ "$commits_behind" != "0" ]; then
output+="↓$commits_behind "
fi
fi
if [ "$GIT_SHOW_FILES_DIFF_COUNT" = true ]; then
local files_diff_count
files_diff_count=$(git diff --name-only "$current_branch" "origin/$current_branch" 2>/dev/null | wc -l | tr -d '[:space:]')
if [ "$files_diff_count" != "0" ]; then
output+="Δ$files_diff_count "
fi
fi
if [ "$GIT_SHOW_UNCOMMITTED_CHANGES" = true ]; then
git diff --quiet 2>/dev/null || output+="+ "
fi
if [ "$GIT_SHOW_UNTRACKED_FILES" = true ]; then
local untracked_files
untracked_files=$(git status -u 2>/dev/null | awk '/^ \(use "git add/,/^nothing added/{if (!/^ \(use "git add/ && !/^nothing added/ && !/^$/) print}' | sed '/^$/d' | wc -l | tr -d '[:space:]')
if [ "$untracked_files" != "0" ]; then
output+="✚$untracked_files "
fi
fi
else
output="Not a Git repository"
fi
iterm2_set_user_var gitInfo "$output"
}
# Call get_git_info when the shell prompt is displayed
PROMPT_COMMAND="get_git_info; $PROMPT_COMMAND"