Skip to content

Commit 59fbcb8

Browse files
committed
Fix vcs segments
* add lib/vcs_helper.sh containing logic to return the closest vcs repository and its type (git, hg, svn) * adapt all vcs_* segments to use logic from new lib * add config vars to most of the vcs segments * remove no longer needed type checks in vcs_* segments fixes #371
1 parent 9989907 commit 59fbcb8

File tree

7 files changed

+234
-143
lines changed

7 files changed

+234
-143
lines changed

lib/vcs_helper.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Source lib to get the function get_tmux_pwd
2+
source "${TMUX_POWERLINE_DIR_LIB}/tmux_adapter.sh"
3+
4+
5+
get_vcs_type_and_root_path() {
6+
tmux_path=$(get_tmux_cwd)
7+
cd "$tmux_path"
8+
root_path=""
9+
git_root_path="$(__parse_git_root_path)"
10+
hg_root_path="$(__parse_hg_root_path)"
11+
svn_root_path="$(__parse_svn_root_path)"
12+
13+
# Return path which is the longest and therefore closest path to the current working
14+
# dir. This is will be the root path of whatever vcs solution is used.
15+
echo -n "$(__get_closest_root_path "$git_root_path" "$hg_root_path" "$svn_root_path")"
16+
}
17+
18+
__parse_git_root_path() {
19+
type git >/dev/null 2>&1
20+
if [ "$?" -ne 0 ]; then
21+
return
22+
fi
23+
24+
root_path=$(git rev-parse --show-toplevel 2>/dev/null)
25+
[ $? -ne 0 ] && return
26+
echo "${root_path}"
27+
}
28+
29+
__parse_hg_root_path() {
30+
type hg >/dev/null 2>&1
31+
if [ $? -ne 0 ]; then
32+
return
33+
fi
34+
35+
root_path=$(hg root 2>/dev/null)
36+
[ $? -ne 0 ] && return
37+
echo "${root_path}"
38+
}
39+
40+
__parse_svn_root_path() {
41+
type svn >/dev/null 2>&1
42+
if [ $? -ne 0 ]; then
43+
return
44+
fi
45+
46+
root_path=$(svn info --show-item wc-root 2>/dev/null)
47+
[ $? -ne 0 ] && return
48+
echo "${root_path}"
49+
}
50+
51+
__get_closest_root_path() {
52+
VCS_TYPES=( git hg svn )
53+
path_id=0
54+
highest=0
55+
root_path=""
56+
counter=0
57+
for path in "$@"; do
58+
[ "${#path}" -gt "$highest" ] && highest="${#path}" && root_path="${path}" && path_id=$counter
59+
counter=$((counter+1))
60+
done
61+
echo -en "${VCS_TYPES[${path_id}]}\n${root_path}"
62+
}

segments/vcs_branch.sh

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,44 @@
22

33
# Source lib to get the function get_tmux_pwd
44
source "${TMUX_POWERLINE_DIR_LIB}/tmux_adapter.sh"
5+
source "${TMUX_POWERLINE_DIR_LIB}/vcs_helper.sh"
56

67
TMUX_POWERLINE_SEG_VCS_BRANCH_MAX_LEN_DEFAULT=24
7-
8-
branch_symbol=""
9-
git_colour="5"
10-
svn_colour="220"
11-
hg_colour="45"
8+
TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL="${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL:-}"
9+
TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR:-5}"
10+
TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR:-45}"
11+
TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR:-220}"
1212

1313

1414
generate_segmentrc() {
1515
read -d '' rccontents << EORC
1616
# Max length of the branch name.
1717
export TMUX_POWERLINE_SEG_VCS_BRANCH_MAX_LEN="${TMUX_POWERLINE_SEG_VCS_BRANCH_MAX_LEN_DEFAULT}"
18+
# Default branch symbol
19+
export TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL="${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
20+
# Branch symbol for git repositories
21+
# export TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL="\${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
22+
# Branch symbol for hg/mercurial repositories
23+
# export TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL="\${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
24+
# Branch symbol for SVN repositories
25+
# export TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL="\${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
26+
# Branch symbol colour for git repositories
27+
export TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR}"
28+
# Branch symbol colour for hg/mercurial repositories
29+
export TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR}"
30+
# Branch symbol colour for SVN repositories
31+
export TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR}"
1832
EORC
1933
echo "$rccontents"
2034
}
2135

2236

2337
run_segment() {
2438
__process_settings
39+
{ read vcs_type; read root_path; } < <(get_vcs_type_and_root_path)
2540
tmux_path=$(get_tmux_cwd)
2641
cd "$tmux_path"
27-
branch=""
28-
if [ -n "${git_branch=$(__parse_git_branch)}" ]; then
29-
branch="$git_branch"
30-
elif [ -n "${svn_branch=$(__parse_svn_branch)}" ]; then
31-
branch="$svn_branch"
32-
elif [ -n "${hg_branch=$(__parse_hg_branch)}" ]; then
33-
branch="$hg_branch"
34-
fi
42+
branch="$(__parse_${vcs_type}_branch)"
3543

3644
if [ -n "$branch" ]; then
3745
echo "${branch}"
@@ -42,13 +50,6 @@ run_segment() {
4250

4351
# Show git banch.
4452
__parse_git_branch() {
45-
type git >/dev/null 2>&1
46-
if [ "$?" -ne 0 ]; then
47-
return
48-
fi
49-
50-
#git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/'
51-
5253
# Quit if this is not a Git repo.
5354
branch=$(git symbolic-ref HEAD 2> /dev/null)
5455
if [[ -z $branch ]] ; then
@@ -64,16 +65,11 @@ __parse_git_branch() {
6465
branch=${branch#refs\/heads\/}
6566
branch=$(__truncate_branch_name $branch)
6667

67-
echo -n "#[fg=colour${git_colour}]${branch_symbol} #[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}]${branch}"
68+
echo -n "#[fg=colour${TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR}]${TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL} #[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}]${branch}"
6869
}
6970

7071
# Show SVN branch.
7172
__parse_svn_branch() {
72-
type svn >/dev/null 2>&1
73-
if [ "$?" -ne 0 ]; then
74-
return
75-
fi
76-
7773
local svn_info=$(svn info 2>/dev/null)
7874
if [ -z "${svn_info}" ]; then
7975
return
@@ -85,23 +81,18 @@ __parse_svn_branch() {
8581

8682
local branch=$(echo "${svn_url}" | grep -E -o '[^/]+$')
8783
branch=$(__truncate_branch_name $branch)
88-
echo "#[fg=colour${svn_colour}]${branch_symbol} #[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}]${branch}"
84+
echo "#[fg=colour${TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR}]${TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL} #[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}]${branch}"
8985
}
9086

9187
__parse_hg_branch() {
92-
type hg >/dev/null 2>&1
93-
if [ "$?" -ne 0 ]; then
94-
return
95-
fi
96-
9788
summary=$(hg summary)
9889
if [ "$?" -ne 0 ]; then
9990
return
10091
fi
10192

10293
local branch=$(echo "$summary" | grep 'branch:' | cut -d ' ' -f2)
10394
branch=$(__truncate_branch_name $branch)
104-
echo "#[fg=colour${hg_colour}]${branch_symbol} #[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}]${branch}"
95+
echo "#[fg=colour${TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR}]${TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL} #[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}]${branch}"
10596
}
10697

10798

@@ -116,4 +107,25 @@ __process_settings() {
116107
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_MAX_LEN" ]; then
117108
export TMUX_POWERLINE_SEG_VCS_BRANCH_MAX_LEN="${TMUX_POWERLINE_SEG_VCS_BRANCH_MAX_LEN_DEFAULT}"
118109
fi
110+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL" ]; then
111+
export TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL="${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
112+
fi
113+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL" ]; then
114+
export TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL="${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
115+
fi
116+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL" ]; then
117+
export TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL="${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
118+
fi
119+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL" ]; then
120+
export TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL="${TMUX_POWERLINE_SEG_VCS_BRANCH_DEFAULT_SYMBOL}"
121+
fi
122+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR" ]; then
123+
export TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_GIT_SYMBOL_COLOUR}"
124+
fi
125+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR" ]; then
126+
export TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_HG_SYMBOL_COLOUR}"
127+
fi
128+
if [ -z "$TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR" ]; then
129+
export TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR="${TMUX_POWERLINE_SEG_VCS_BRANCH_SVN_SYMBOL_COLOUR}"
130+
fi
119131
}

segments/vcs_compare.sh

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33

44
# Source lib to get the function get_tmux_pwd
55
source "${TMUX_POWERLINE_DIR_LIB}/tmux_adapter.sh"
6+
source "${TMUX_POWERLINE_DIR_LIB}/vcs_helper.sh"
7+
8+
TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL="${TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL:-↑}"
9+
TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL="${TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL:-↓}"
10+
11+
generate_segmentrc() {
12+
read -d '' rccontents << EORC
13+
# Symbol if local branch is behind.
14+
# export TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL="${TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL}"
15+
# Symbol if local branch is ahead.
16+
# export TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL="${TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL}"
17+
EORC
18+
echo "$rccontents"
19+
}
620

7-
flat_symbol=""
821

922
run_segment() {
23+
__process_settings
24+
{ read vcs_type; read root_path; } < <(get_vcs_type_and_root_path)
1025
tmux_path=$(get_tmux_cwd)
1126
cd "$tmux_path"
12-
stats=""
13-
if [ -n "${git_stats=$(__parse_git_stats)}" ]; then
14-
stats="$git_stats"
15-
elif [ -n "${svn_stats=$(__parse_svn_stats)}" ]; then
16-
stats="$svn_stats"
17-
elif [ -n "${hg_stats=$(__parse_hg_stats)}" ]; then
18-
stats="$hg_stats"
19-
fi
27+
28+
stats="$(__parse_${vcs_type}_stats)"
2029

2130
if [ -n "$stats" ]; then
2231
echo "${stats}"
@@ -25,11 +34,6 @@ run_segment() {
2534
}
2635

2736
__parse_git_stats() {
28-
type git >/dev/null 2>&1
29-
if [ "$?" -ne 0 ]; then
30-
return
31-
fi
32-
3337
# check if git
3438
[[ -z $(git rev-parse --git-dir 2> /dev/null) ]] && return
3539

@@ -55,17 +59,20 @@ __parse_git_stats() {
5559
}
5660

5761
__parse_hg_stats() {
58-
type hg >/dev/null 2>&1
59-
if [ "$?" -ne 0 ]; then
60-
return
61-
fi
6262
# not yet implemented
63+
return
6364
}
6465

6566
__parse_svn_stats() {
66-
type svn >/dev/null 2>&1
67-
if [ "$?" -ne 0 ]; then
68-
return
69-
fi
7067
# not yet implemented
68+
return
69+
}
70+
71+
__process_settings() {
72+
if [ -z "$TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL" ]; then
73+
export TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL="${TMUX_POWERLINE_SEG_VCS_COMPARE_AHEAD_SYMBOL}"
74+
fi
75+
if [ -z "$TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL" ]; then
76+
export TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL="${TMUX_POWERLINE_SEG_VCS_COMPARE_BEHIND_SYMBOL}"
77+
fi
7178
}

segments/vcs_modified.sh

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@
33

44
# Source lib to get the function get_tmux_pwd
55
source "${TMUX_POWERLINE_DIR_LIB}/tmux_adapter.sh"
6+
source "${TMUX_POWERLINE_DIR_LIB}/vcs_helper.sh"
7+
8+
TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL="${TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL:-±}"
9+
10+
generate_segmentrc() {
11+
read -d '' rccontents << EORC
12+
# Symbol for count of modified vcs files.
13+
# export TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL="${TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL}"
14+
EORC
15+
echo "$rccontents"
16+
}
617

7-
mod_symbol=""
818

919
run_segment() {
20+
__process_settings
21+
{ read vcs_type; read root_path; } < <(get_vcs_type_and_root_path)
1022
tmux_path=$(get_tmux_cwd)
1123
cd "$tmux_path"
1224

13-
stats=""
14-
if [ -n "${git_stats=$(__parse_git_stats)}" ]; then
15-
stats="$git_stats"
16-
elif [ -n "${svn_stats=$(__parse_svn_stats)}" ]; then
17-
stats="$svn_stats"
18-
elif [ -n "${hg_stats=$(__parse_hg_stats)}" ]; then
19-
stats="$hg_stats"
20-
fi
25+
stats="$(__parse_${vcs_type}_stats)"
26+
2127
if [[ -n "$stats" && $stats -gt 0 ]]; then
22-
echo "${mod_symbol}${stats}"
28+
echo "${TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL} ${stats}"
2329
fi
2430
}
2531

2632
__parse_git_stats(){
27-
type git >/dev/null 2>&1
28-
if [ "$?" -ne 0 ]; then
29-
return
30-
fi
31-
3233
# check if git
3334
[[ -z $(git rev-parse --git-dir 2> /dev/null) ]] && return
3435

@@ -37,18 +38,13 @@ __parse_git_stats(){
3738
echo $modified
3839
}
3940
__parse_hg_stats(){
40-
type hg >/dev/null 2>&1
41-
if [ "$?" -ne 0 ]; then
41+
local modified=$(hg status -m | wc -l)
42+
if [ -z "${modified}" ]; then
4243
return
4344
fi
44-
# not yet implemented
45+
echo ${modified}
4546
}
4647
__parse_svn_stats() {
47-
type svn >/dev/null 2>&1
48-
if [ "$?" -ne 0 ]; then
49-
return
50-
fi
51-
5248
local svn_info=$(svn info 2>/dev/null)
5349
if [ -z "${svn_info}" ]; then
5450
return
@@ -60,11 +56,17 @@ __parse_svn_stats() {
6056
local conflicted=$(echo "${svn_st}" | grep -E '^!?\s*C' | wc -l)
6157

6258
#print
63-
if [[ $modified -gt 0 ]] ; then
64-
local ret="#[fg=${TMUX_POWERLINE_CUR_SEGMENT_FG}${modified}"
65-
fi
6659
if [[ $conflicted -gt 0 ]] ; then
67-
local ret="#[fg=colour${svn_colour}${conflicted} ${ret}"
60+
local ret="ϟ ${conflicted}"
61+
fi
62+
if [[ $modified -gt 0 ]] ; then
63+
local ret="${modified} ${ret}"
6864
fi
6965
echo "${ret}"
7066
}
67+
68+
__process_settings() {
69+
if [ -z "$TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL" ]; then
70+
export TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL="${TMUX_POWERLINE_SEG_VCS_MODIFIED_SYMBOL}"
71+
fi
72+
}

0 commit comments

Comments
 (0)