Skip to content

Commit 959cdb8

Browse files
committed
feat: List command now accepts specific packages to list
1 parent e04cde3 commit 959cdb8

File tree

3 files changed

+169
-75
lines changed

3 files changed

+169
-75
lines changed

pkg/lib/commands/do-list.sh

Lines changed: 103 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# shellcheck shell=bash
22

3+
has_invalid_packages='no'
4+
35
do-list() {
4-
local flag_outdated='no'
56
local flag_simple='no'
67
local flag_fetch='no'
78

89
util.setup_mode
910

11+
local -a pkgs=()
1012
for arg; do
1113
case "$arg" in
1214
--simple)
@@ -18,92 +20,121 @@ do-list() {
1820
-*)
1921
die "Flag '$arg' not recognized"
2022
;;
23+
*)
24+
pkgs+=("$arg")
25+
;;
2126
esac
2227
done
2328

24-
local has_invalid_packages='no'
29+
if (( ${#pkgs[@]} > 0 )); then
30+
for repoSpec in "${pkgs[@]}"; do
31+
util.extract_data_from_input "$repoSpec"
32+
local site="$REPLY2"
33+
local package="$REPLY3"
34+
local ref="$REPLY4"
2535

26-
for namespace_path in "$BPM_PACKAGES_PATH"/*; do
27-
local glob_suffix=
28-
if [ "${namespace_path##*/}" = 'local' ]; then
29-
glob_suffix="/*"
30-
else
31-
glob_suffix="/*/*"
32-
fi
33-
34-
for pkg_path in "$namespace_path"$glob_suffix; do
35-
util.extract_data_from_package_dir "$pkg_path"
36-
local site="$REPLY1"
37-
local user="$REPLY2"
38-
local repository="$REPLY3"
39-
40-
# Users that have installed packages before the switch to namespacing by
41-
# site domain name will print incorrectly. So, we check to make sure the site
42-
# url is actually is a domain name and not, for example, a GitHub username
43-
if [[ "$site" != *.* ]] && [ "$site" != 'local' ]; then
44-
has_invalid_packages='yes'
45-
continue
36+
if [ -n "$ref" ]; then
37+
die "Refs must be omitted when listing packages. Remove ref '@$ref'"
4638
fi
4739

48-
# Relative path location of the current package
49-
local id=
50-
if [ "$site" = 'local' ]; then
51-
id="$site/$repository"
40+
if [ -d "$BPM_PACKAGES_PATH/$site/$package" ]; then
41+
echo_package_info "$BPM_PACKAGES_PATH/$site/$package" "$site" "${package%/*}" "${package#*/}"
5242
else
53-
id="$site/$user/$repository"
43+
die "Package '$site/$package' is not installed"
44+
fi
45+
done
46+
else
47+
for namespace_path in "$BPM_PACKAGES_PATH"/*; do
48+
local glob_suffix=
49+
if [ "${namespace_path##*/}" = 'local' ]; then
50+
glob_suffix="/*"
51+
else
52+
glob_suffix="/*/*"
5453
fi
5554

56-
# The information being outputed for a particular package
57-
# Ex.
58-
# github.com/tj/git-extras
59-
# Status: Up to Date
60-
# Branch: main\n
61-
local pkg_output=
55+
for pkg_path in "$namespace_path"$glob_suffix; do
56+
util.extract_data_from_package_dir "$pkg_path"
57+
local site="$REPLY1"
58+
local user="$REPLY2"
59+
local repository="$REPLY3"
6260

63-
printf -v pkg_output "%s\n" "$id"
61+
echo_package_info "$pkg_path" "$site" "$user" "$repository"
62+
done
63+
done
64+
fi
6465

65-
if [ "$flag_simple" = 'no' ]; then
66-
if [ ! -d "$pkg_path/.git" ]; then
67-
die "Package '$id' is not a Git repository. Unlink or otherwise remove it at '$pkg_path'"
68-
fi
66+
if [ "$has_invalid_packages" = 'yes' ]; then
67+
log.error "Some packages are installed in an outdated format. To fix this optimally, remove the '${BPM_PACKAGES_PATH%/*}' directory and reinstall all the packages that were deleted in the process. This procedure is required in response to a one-time breaking change in how packages are stored"
68+
fi
69+
}
70+
71+
echo_package_info() {
72+
local pkg_path="$1"
73+
local site="$2"
74+
local user="$3"
75+
local repository="$4"
76+
77+
# Users that have installed packages before the switch to namespacing by
78+
# site domain name will print incorrectly. So, we check to make sure the site
79+
# url is actually is a domain name and not, for example, a GitHub username
80+
if [[ "$site" != *.* ]] && [ "$site" != 'local' ]; then
81+
has_invalid_packages='yes'
82+
return
83+
fi
84+
85+
# Relative path location of the current package
86+
local id=
87+
if [ "$site" = 'local' ]; then
88+
id="$site/$repository"
89+
else
90+
id="$site/$user/$repository"
91+
fi
92+
93+
# The information being outputed for a particular package
94+
# Ex.
95+
# github.com/tj/git-extras
96+
# Status: Up to Date
97+
# Branch: main\n
98+
local pkg_output=
99+
100+
printf -v pkg_output "%s\n" "$id"
101+
102+
if [ "$flag_simple" = 'no' ]; then
103+
if [ ! -d "$pkg_path/.git" ]; then
104+
die "Package '$id' is not a Git repository. Unlink or otherwise remove it at '$pkg_path'"
105+
fi
69106

70-
local repo_branch_str= repo_revision_str= repo_outdated_str=
71-
72-
repo_branch_str="Branch: $(git -C "$pkg_path" branch --show-current)"
73-
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_branch_str"
74-
75-
if git -C "$pkg_path" config remote.origin.url &>/dev/null; then
76-
if [ "$flag_fetch" = yes ]; then
77-
local git_output=
78-
if ! git_output="$(git -C "$pkg_path" fetch)"; then
79-
printf "%s\n" "$git_output"
80-
fi
81-
fi
82-
83-
local git_tag= git_sha1=
84-
git_sha1="$(git -C "$pkg_path" rev-parse --short HEAD)"
85-
if git_tag="$(git -C "$pkg_path" describe --exact-match --tags 2>/dev/null)"; then
86-
repo_revision_str="Revision: $git_tag ($git_sha1)"
87-
else
88-
repo_revision_str="Revision: $git_sha1"
89-
fi
90-
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_revision_str"
91-
92-
# shellcheck disable=SC1083
93-
if [ "$(git -C "$pkg_path" rev-list --count HEAD...HEAD@{upstream})" -gt 0 ]; then
94-
repo_outdated_str="State: Out of date"
95-
else
96-
repo_outdated_str="State: Up to date"
97-
fi
98-
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_outdated_str"
107+
local repo_branch_str= repo_revision_str= repo_outdated_str=
108+
109+
repo_branch_str="Branch: $(git -C "$pkg_path" branch --show-current)"
110+
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_branch_str"
111+
112+
if git -C "$pkg_path" config remote.origin.url &>/dev/null; then
113+
if [ "$flag_fetch" = yes ]; then
114+
local git_output=
115+
if ! git_output="$(git -C "$pkg_path" fetch)"; then
116+
printf "%s\n" "$git_output"
99117
fi
100118
fi
101119

102-
printf "%s" "$pkg_output"
103-
done
104-
done
120+
local git_tag= git_sha1=
121+
git_sha1="$(git -C "$pkg_path" rev-parse --short HEAD)"
122+
if git_tag="$(git -C "$pkg_path" describe --exact-match --tags 2>/dev/null)"; then
123+
repo_revision_str="Revision: $git_tag ($git_sha1)"
124+
else
125+
repo_revision_str="Revision: $git_sha1"
126+
fi
127+
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_revision_str"
105128

106-
if [ "$has_invalid_packages" = 'yes' ]; then
107-
log.error "Some packages are installed in an outdated format. To fix this optimally, remove the '${BPM_PACKAGES_PATH%/*}' directory and reinstall all the packages that were deleted in the process. This procedure is required in response to a one-time breaking change in how packages are stored"
129+
# shellcheck disable=SC1083
130+
if [ "$(git -C "$pkg_path" rev-list --count HEAD...HEAD@{upstream})" -gt 0 ]; then
131+
repo_outdated_str="State: Out of date"
132+
else
133+
repo_outdated_str="State: Up to date"
134+
fi
135+
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_outdated_str"
136+
fi
108137
fi
138+
139+
printf "%s" "$pkg_output"
109140
}

pkg/lib/util/util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Subcommands:
310310
Installs a package from a local directory. These have a
311311
namespace of 'local'
312312
313-
list [--simple] [--fetch]
313+
list [--simple] [--fetch] [package...]
314314
List installed packages
315315
316316
package-path <package>

tests/do-list.bats

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ load 'util/init.sh'
1717
test_util.create_package 'username/p1'
1818
test_util.create_package 'username2/p2'
1919
test_util.create_package 'username2/p3'
20-
test_util.mock_add "username/p1"
21-
test_util.mock_add "username2/p2"
20+
test_util.mock_add 'username/p1'
21+
test_util.mock_add 'username2/p2'
2222

2323
run do-list --simple
2424

@@ -28,6 +28,69 @@ load 'util/init.sh'
2828
refute_line "$site/username2/p3"
2929
}
3030

31+
@test "properly simple list for packages specified as arguments" {
32+
local site='github.com'
33+
34+
test_util.create_package 'username/p1'
35+
test_util.create_package 'username2/p2'
36+
test_util.create_package 'username2/p3'
37+
test_util.mock_add 'username/p1'
38+
test_util.mock_add 'username2/p2'
39+
test_util.mock_add 'username2/p3'
40+
41+
run do-list --simple 'username/p1' 'username2/p2'
42+
43+
assert_success
44+
assert_line -n 0 "$site/username/p1"
45+
assert_line -n 1 "$site/username2/p2"
46+
refute_line "$site/username2/p3"
47+
}
48+
49+
@test "properly non-simple list for packages specified as arguments" {
50+
local site='github.com'
51+
52+
test_util.create_package 'username/p1'
53+
test_util.create_package 'username2/p2'
54+
test_util.create_package 'username2/p3'
55+
test_util.mock_add 'username/p1'
56+
test_util.mock_add 'username2/p2'
57+
test_util.mock_add 'username2/p3'
58+
59+
run do-list 'username/p1' 'username2/p2'
60+
61+
assert_success
62+
assert_output -e "$site/username/p1
63+
Branch: master
64+
Revision: ([a-z0-9]*)
65+
State: Up to date
66+
$site/username2/p2
67+
Branch: master
68+
Revision: ([a-z0-9]*)
69+
State: Up to date"
70+
}
71+
72+
@test "error if non-existant packages are specified as arguments" {
73+
local site='github.com'
74+
local pkg='username/p1'
75+
76+
run do-list --simple "$pkg"
77+
78+
assert_failure
79+
assert_line -p "Package '$site/$pkg' is not installed"
80+
}
81+
82+
@test "error if ref is specified in arguments" {
83+
local site='github.com'
84+
85+
test_util.create_package 'username/p1'
86+
test_util.mock_add 'username/p1'
87+
88+
run do-list --simple 'username/p1@v0.1.0'
89+
90+
assert_failure
91+
assert_line -p "Refs must be omitted when listing packages. Remove ref '@v0.1.0'"
92+
}
93+
3194
@test "properly list for local packages in simple list" {
3295
local site='github.com'
3396
local pkg='somepath/project2'

0 commit comments

Comments
 (0)