-
Notifications
You must be signed in to change notification settings - Fork 7
/
.bash_profile__git
94 lines (79 loc) · 3.01 KB
/
.bash_profile__git
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
###########
### GIT ###
###########
# get the current branch name
function __git__branch_name {
echo $(git symbolic-ref HEAD 2>/dev/null | awk -Frefs\/heads\/ '{print $NF}')
#echo $(git symbolic-ref HEAD 2>/dev/null | awk -F\/ '{print $NF}')
}
# get the current remote name (http://stackoverflow.com/a/24210877)
function __git__remote_name {
remote_name=$(git branch -vv --no-color | grep -E '^\*' | awk '{print $4}' | awk -F "[" '{print $2}' | awk -F "/" '{print $1}')
if [[ $remote_name != "" ]]; then
echo $remote_name
else
echo "origin"
fi
#echo $(git symbolic-ref HEAD 2>/dev/null | awk -Frefs\/heads\/ '{print $NF}')
#echo $(git symbolic-ref HEAD 2>/dev/null | awk -F\/ '{print $NF}')
}
# check & get the formatted branch name
function __git__dirty {
status=$(git status 2> /dev/null | tail -n 1)
if [[ $status != "nothing to commit, working tree clean" && $status != "nothing to commit, working directory clean" ]]; then
echo $COLOR_MAGENTA$(__git__branch_name)$COLOR_YELLOW
else
echo $(__git__branch_name)
fi
}
# check & get if you have unpulled changes
function __git__unpulled {
logdiff=$(git log HEAD..$(__git__remote_name)/$(__git__branch_name) --oneline 2> /dev/null) || return
if [[ $logdiff != "" ]]; then
echo "("$COLOR_MAGENTA"'git pull $(__git__remote_name) $(__git__branch_name)'"$COLOR_YELLOW" required)"
fi
}
# check & get if you have unpushed changes
function __git__unpushed {
logdiff=$(git log $(__git__remote_name)/$(__git__branch_name)..HEAD --oneline 2> /dev/null) || return
if [[ $logdiff != "" ]]; then
echo "("$COLOR_MAGENTA"'git push $(__git__remote_name) $(__git__branch_name)'"$COLOR_YELLOW" required)"
fi
}
# check & get if you need to create a new branch
function __git__new_branch {
brinfo=$(git branch -r | grep "$(__git__remote_name)/"$(__git__branch_name))
if [[ $brinfo == "" ]]; then
echo "("$COLOR_RED"new branch"$COLOR_YELLOW" | "$COLOR_MAGENTA"'git push $(__git__remote_name) $(__git__branch_name)'"$COLOR_YELLOW" required)"
fi
}
# get the status of current branch
function __git__status {
unpulled=$(__git__unpulled)
unpushed=$(__git__unpushed)
newbrach=$(__git__new_branch)
if [[ $newbrach != "" ]]; then
echo $newbrach
fi
if [[ $unpulled != "" && $unpushed != "" ]]; then
echo "("$COLOR_RED"'git push $(__git__remote_name) $(__git__branch_name) --force-with-lease'"$COLOR_YELLOW" required)"
else
if [[ $unpulled != "" ]]; then
echo $unpulled
fi
if [[ $unpushed != "" ]]; then
echo $unpushed
fi
fi
}
# compose a useful string containing git information
function gitify {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo -e "\n[GIT $(__git__remote_name) $(__git__dirty)] $(__git__status)"
}
#####################
### GIT - UTILITY ###
#####################
function git__update_date {
git commit --amend --date "`date $@ -R`" --no-edit
}