-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathlist.sh
More file actions
121 lines (109 loc) · 2.95 KB
/
list.sh
File metadata and controls
121 lines (109 loc) · 2.95 KB
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
#!/usr/bin/env bash
# List command
# shellcheck disable=SC2154 # _arg_* set by parse_args, _ctx_* set by resolve_*
cmd_list() {
parse_args "--porcelain" "$@"
local porcelain="${_arg_porcelain:-0}"
resolve_repo_context || exit 1
local repo_root="$_ctx_repo_root"
local records
records=$(list_worktree_records "$repo_root")
# Machine-readable output (porcelain)
if [ "$porcelain" -eq 1 ]; then
# Output: path<tab>branch<tab>status
local is_main="" path="" branch="" status="" linked_rows="" line
while IFS= read -r line; do
case "$line" in
"")
[ -z "$path" ] && continue
if [ "$is_main" = "1" ]; then
printf "%s\t%s\t%s\n" "$path" "$branch" "$status"
else
linked_rows="${linked_rows}${path}"$'\t'"${branch}"$'\t'"${status}"$'\n'
fi
is_main=""
path=""
branch=""
status=""
;;
"is_main "*)
is_main="${line#is_main }"
;;
"path "*)
path="${line#path }"
;;
"branch "*)
branch="${line#branch }"
;;
"status "*)
status="${line#status }"
;;
esac
done <<EOF
$records
EOF
if [ -n "$path" ]; then
if [ "$is_main" = "1" ]; then
printf "%s\t%s\t%s\n" "$path" "$branch" "$status"
else
linked_rows="${linked_rows}${path}"$'\t'"${branch}"$'\t'"${status}"$'\n'
fi
fi
if [ -n "$linked_rows" ]; then
printf "%s" "$linked_rows" | LC_ALL=C sort -t "$(printf '\t')" -k2,2 -k1,1
fi
return 0
fi
# Human-readable output - table format
echo "Git Worktrees"
echo ""
printf "%-30s %s\n" "BRANCH" "PATH"
printf "%-30s %s\n" "------" "----"
local is_main="" path="" branch="" status="" linked_rows="" line
while IFS= read -r line; do
case "$line" in
"")
[ -z "$path" ] && continue
if [ "$is_main" = "1" ]; then
printf "%-30s %s\n" "$branch [main repo]" "$path"
else
linked_rows="${linked_rows}${branch}"$'\t'"${path}"$'\n'
fi
is_main=""
path=""
branch=""
status=""
;;
"is_main "*)
is_main="${line#is_main }"
;;
"path "*)
path="${line#path }"
;;
"branch "*)
branch="${line#branch }"
;;
"status "*)
status="${line#status }"
;;
esac
done <<EOF
$records
EOF
if [ -n "$path" ]; then
if [ "$is_main" = "1" ]; then
printf "%-30s %s\n" "$branch [main repo]" "$path"
else
linked_rows="${linked_rows}${branch}"$'\t'"${path}"$'\n'
fi
fi
if [ -n "$linked_rows" ]; then
printf "%s" "$linked_rows" | LC_ALL=C sort -t "$(printf '\t')" -k1,1 -k2,2 | while IFS=$'\t' read -r branch path; do
[ -z "$path" ] && continue
printf "%-30s %s\n" "$branch" "$path"
done
fi
echo ""
echo ""
echo "Tip: Use 'git gtr list --porcelain' for machine-readable output"
}