summaryrefslogtreecommitdiff
path: root/modules/blobs/tree.sh
blob: f08db42a053c977b8e8094265b73b993ed5e28f2 (plain)
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
#!/usr/bin/env bash
name=bolbs/tree
desc='Recursively show the directory tree of a blob ID'
copyright=('2010, 2016 Luke Shumaker')
license=('AGPLv3+')
# This file is part of rvs.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

usage="DIR_ID [NAME [PREFIX [LAST]]]"
. "${0%/*}/_shlib.sh"

main() {
	local id name prefix has_prefix last
	case $# in
		0) errusage;;
		1) id=$1; name=.;;
		2) id=$1; name=$2;; prefix=''; has_prefix=false;;
		3) id=$1; name=$2;; prefix=$3; has_prefix=true;;
		3) id=$1; name=$2;; prefix=$3; has_prefix=true; last=$4;;
		*) errusage;;
	esac

	local a b c
	if locale -k LC_MESSAGES | grep -xF 'messages-codeset="UTF-8"' >/dev/null; then
		a='├── '
		b='│   '
		c='└── '
	else
		a='|-- '
		b='|   '
		c='`-- '
	fi

	local pfix_self pfix_chld
	if ! $has_prefix; then
		pfix_self=''
		pfix_chld=''
	else
		pfix_self="$prefix$a"
		pfix_chld="$prefix$b"
		if [[ -n "$last" ]]; then
			pfix_self="$prefix$c"
		fi
	fi

	printf "%s$(_ '%s\t(%s)')\n" "$pfix_self" "$name" "$id"
	if [[ "$id" == d:* ]]; then
		local lastname
		lastname="$("$RVS" get.d "$id" /dev/stdout | sed -n '$/.*\t//p')"
		local perm user group id name
		while read -r perm user group id name; do
			if [[ "$name" != "$lastname" ]]; then
				"$RVS" tree "$id" "$name" "$pfix_chld"
			else
				"$RVS" tree "$id" "$name" "$pfix_chld" t
			fi
		done < <("$RVS" get.d "$id" /dev/stdout)
	fi

}

main "$@"