forked from lucaswinningham/dot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
95 lines (79 loc) · 2.33 KB
/
.functions
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
#!/usr/bin/env bash
git_color() {
local git_status="$(git status 2> /dev/null)"
if [[ $git_status == *"Changes not staged for commit"* ]]; then
echo -e $RED
elif [[ $git_status == *"Changes to be committed"* ]]; then
echo -e $YEL
elif [[ $git_status == *"Your branch is ahead of"* ]]; then
echo -e $WHI
elif [[ $git_status == *"nothing to commit, working tree clean"* ]]; then
echo -e $GRN
else
echo -e $PUR
fi
}
git_branch() {
local git_directory="$(git rev-parse --show-toplevel 2> /dev/null)"
local git_status="$(git status 2> /dev/null)"
local on_branch="On branch ([^${IFS}]*)"
local on_commit="HEAD detached at ([^${IFS}]*)"
if [[ $PWD == $git_directory ]]; then
if [[ $git_status =~ $on_branch ]]; then
local branch=${BASH_REMATCH[1]}
echo "$branch"
elif [[ $git_status =~ $on_commit ]]; then
local commit=${BASH_REMATCH[1]}
echo "$commit"
fi
fi
}
git_default_branch() {
git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
}
git_b() {
local current=$(git rev-parse --abbrev-ref HEAD)
local branches=$(git for-each-ref --format='%(refname)' refs/heads/ | sed 's|refs/heads/||')
local longest_branch_name_size=0
for branch in $branches; do
local branch_name_size=${#branch}
if [ $branch_name_size -gt $longest_branch_name_size ]; then
longest_branch_name_size=$branch_name_size
fi
done
for branch in $branches; do
local note=$(git config branch.$branch.note)
local prefix=" "
local branch_name_size=${#branch}
local num_spacers=$(($longest_branch_name_size-$branch_name_size+2))
local spacer=""
if [[ ! -z $note ]]; then
spacer=$(seq 1 $num_spacers | sed "s/.*/\./" | tr -d '\n')
fi
if [ $branch == $current ]; then
prefix="* $GRN"
fi
echo "$prefix$branch$RES$spacer$BLU$note$RES"
done
}
git_cob() {
git checkout -b $1 && git_note "${@:2}"
}
git_mm() {
local branch=$(git_branch)
git co $(git_default_branch) && git p && git co "$branch" && git m $(git_default_branch)
}
git_note() {
if [ $# -eq 0 ]; then
git config branch."$(git_branch)".note
else
git config --replace-all branch."$(git_branch)".note "$@"
fi
}
export -f git_color
export -f git_branch
export -f git_default_branch
export -f git_b
export -f git_cob
export -f git_mm
export -f git_note