Skip to content

Commit 67b728e

Browse files
committed
feat: Revamp do-list. Closes #50
1 parent 6779834 commit 67b728e

File tree

8 files changed

+186
-42
lines changed

8 files changed

+186
-42
lines changed

pkg/lib/commands/do-add.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ do-add() {
2121

2222
for repoSpec in "${pkgs[@]}"; do
2323
if [[ -d "$repoSpec" && "${repoSpec::1}" == / ]]; then
24-
log.warn "Identifier '$repoSpec' is a directory, not a package. Skipping"
25-
continue
24+
die "Identifier '$repoSpec' is a directory, not a package"
2625
fi
2726

2827
util.extract_data_from_input "$repoSpec" "$with_ssh"
@@ -31,6 +30,10 @@ do-add() {
3130
local package="$REPLY3"
3231
local ref="$REPLY4"
3332

33+
if [ "${package%/*}" = 'local' ]; then
34+
die "Cannot install packages owned by username 'local' because that conflicts with linked packages"
35+
fi
36+
3437
log.info "Installing '$repoSpec'"
3538
do-plumbing-clone "$uri" "$site/$package" $ref
3639
do-plumbing-add-deps "$site/$package"

pkg/lib/commands/do-list.sh

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
11
# shellcheck shell=bash
22

33
do-list() {
4-
local should_show_outdated='no'
4+
local flag_outdated='no'
5+
local flag_simple='no'
56

67
for arg; do
78
case "$arg" in
89
--outdated)
9-
should_show_outdated='yes'
10+
flag_outdated='yes'
11+
;;
12+
--simple)
13+
flag_simple='yes'
1014
;;
1115
esac
1216
done
1317

1418
local has_invalid_packages='no'
1519

16-
if [ "$should_show_outdated" = 'yes' ]; then
17-
local packages
18-
readarray -t packages < <(do-list)
19-
20-
for package in "${packages[@]}"; do
21-
local package_path="$BPM_PACKAGES_PATH/$package"
22-
23-
if [ ! -L "$package_path" ]; then
24-
ensure.cd "$package_path"
25-
git remote update &>/dev/null
20+
for namespace_path in "$BPM_PACKAGES_PATH"/*; do
21+
local glob_suffix=
22+
if [ "${namespace_path##*/}" = 'local' ]; then
23+
glob_suffix="/*"
24+
else
25+
glob_suffix="/*/*"
26+
fi
2627

27-
if git symbolic-ref --short -q HEAD >/dev/null; then
28-
# shellcheck disable=SC1083
29-
if [ "$(git rev-list --count HEAD...HEAD@{upstream})" -gt 0 ]; then
30-
printf "%s\n" "$package"
31-
fi
32-
fi
33-
fi
34-
done
35-
else
36-
for package_path in "$BPM_PACKAGES_PATH"/*/*/*; do
37-
util.extract_data_from_package_dir "$package_path"
28+
for pkg_path in "$namespace_path"$glob_suffix; do
29+
util.extract_data_from_package_dir "$pkg_path"
3830
local site="$REPLY1"
3931
local user="$REPLY2"
4032
local repository="$REPLY3"
41-
local package="$user/$repository"
4233

4334
# Users that have installed packages before the switch to namespacing by
4435
# site domain name will print incorrectly. So, we check to make sure the site
@@ -48,15 +39,53 @@ do-list() {
4839
continue
4940
fi
5041

42+
# Relative path location of the current package
43+
local id=
5144
if [ "$site" = 'local' ]; then
52-
printf "%s\n" "$site/$user"
45+
id="$site/$repository"
5346
else
54-
printf "%s\n" "$site/$package"
47+
id="$site/$user/$repository"
48+
fi
49+
50+
# The information being outputed for a particular package
51+
# Ex.
52+
# github.com/tj/git-extras
53+
# Status: Up to Date
54+
# Branch: main\n
55+
local pkg_output=
56+
57+
printf -v pkg_output "%s\n" "$id"
58+
59+
if [ "$flag_simple" = 'no' ]; then
60+
ensure.git_repository "$pkg_path" "$id"
61+
62+
local repo_branch_str= repo_outdated_str=
63+
64+
repo_branch_str="Branch: $(git -C "$pkg_path" branch --show-current)"
65+
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_branch_str"
66+
67+
if git config remote.origin.url &>/dev/null; then
68+
# shellcheck disable=SC1083
69+
if [ "$(git -C "$pkg_path" rev-list --count HEAD...HEAD@{upstream})" -gt 0 ]; then
70+
if [ -n "${NO_COLOR+x}" ] || [ "$TERM" = dumb ]; then
71+
repo_outdated_str="State: Out of date"
72+
else
73+
printf -v repo_outdated_str "\033[1;33m%s\033[0m\n" "State: Out of date"
74+
fi
75+
repo_outdated_str="State: Out of date"
76+
else
77+
repo_outdated_str="State: Up to date"
78+
fi
79+
80+
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_outdated_str"
81+
fi
5582
fi
83+
84+
printf "%s" "$pkg_output"
5685
done
57-
fi
86+
done
5887

5988
if [ "$has_invalid_packages" = 'yes' ]; then
60-
log.error "You have invalid packages. To fix this optimally, remove the '${BPM_PACKAGES_PATH%/*}' directory and reinstall all that packages that were deleted in the process"
89+
log.error "You have invalid packages. To fix this optimally, remove the '${BPM_PACKAGES_PATH%/*}' directory and reinstall all that packages that were deleted in the process. This procedure is required in response to a one-time breaking change in how packages are stored"
6190
fi
6291
}

pkg/lib/util/ensure.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ ensure.package_exists() {
3939
die "Package '$package' does not exist"
4040
fi
4141
}
42+
43+
ensure.git_repository() {
44+
local dir="$1"
45+
local id="$2"
46+
47+
if [ ! -d "$dir/.git" ]; then
48+
die "Package '$id' is not a Git repository. Unlink or otherwise remove it at '$dir'"
49+
fi
50+
}

pkg/lib/util/util.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ util.extract_data_from_package_dir() {
8989
local user="${dir%/*}"; user="${user##*/}"
9090
local repository="${dir##*/}"
9191

92-
REPLY1="$site"
93-
REPLY2="$user"
94-
REPLY3="$repository"
92+
if [ "$user" = 'local' ]; then
93+
REPLY1="$user"
94+
REPLY2=''
95+
REPLY3="$repository"
96+
else
97+
REPLY1="$site"
98+
REPLY2="$user"
99+
REPLY3="$repository"
100+
fi
95101
REPLY4=
96102
}
97103

tests/do-add.bats

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,36 @@ load 'util/init.sh'
1515
assert_line -n 0 -p "At least one package must be supplied"
1616
}
1717

18+
@test "fails when the remote repository is owned by a user with username 'local'" {
19+
test_util.mock_command do-plumbing-clone
20+
test_util.mock_command do-plumbing-add-deps
21+
test_util.mock_command do-plumbing-link-bins
22+
test_util.mock_command do-plumbing-link-completions
23+
test_util.mock_command do-plumbing-link-man
24+
25+
run do-add 'local/pkg'
26+
27+
assert_failure
28+
assert_line -n 0 -p "Cannot install packages owned by username 'local' because that conflicts with linked packages"
29+
}
30+
31+
@test "fails when input is an absolute path to a directory" {
32+
local site='github.com'
33+
local pkg='username/main'
34+
35+
test_util.mock_command do-plumbing-clone
36+
test_util.mock_command do-plumbing-add-deps
37+
test_util.mock_command do-plumbing-link-bins
38+
test_util.mock_command do-plumbing-link-completions
39+
test_util.mock_command do-plumbing-link-man
40+
41+
test_util.create_package "$pkg"
42+
run do-add "$BPM_ORIGIN_DIR/$site/$pkg"
43+
44+
assert_failure
45+
assert_line -p "Identifier '$BPM_ORIGIN_DIR/$site/$pkg' is a directory, not a package"
46+
}
47+
1848
@test "executes install steps in right order" {
1949
test_util.mock_command do-plumbing-clone
2050
test_util.mock_command do-plumbing-add-deps

tests/do-list.bats

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
load 'util/init.sh'
44

5-
@test "properly list for 2 installed packages" {
5+
@test "properly list for no installed packages" {
6+
test_util.create_package 'username/p1'
7+
8+
run do-list
9+
10+
assert_success
11+
assert_output ""
12+
}
13+
14+
@test "properly list for 2 installed packages in mode simple list" {
615
local site='github.com'
716

817
test_util.create_package 'username/p1'
@@ -11,24 +20,54 @@ load 'util/init.sh'
1120
test_util.fake_clone "$site/username/p1"
1221
test_util.fake_clone "$site/username2/p2"
1322

14-
run do-list
23+
run do-list --simple
1524

1625
assert_success
17-
assert_line "$site/username/p1"
18-
assert_line "$site/username2/p2"
26+
assert_line -n 0 "$site/username2/p2"
27+
assert_line -n 1 "$site/username/p1"
1928
refute_line "$site/username2/p3"
2029
}
2130

22-
@test "properly list for no installed packages" {
31+
@test "properly list for local packages in simple list" {
32+
local site='github.com'
33+
local pkg='somepath/project2'
34+
35+
test_util.mock_command do-plumbing-add-deps
36+
test_util.mock_command do-plumbing-link-bins
37+
test_util.mock_command do-plumbing-link-completions
38+
test_util.mock_command do-plumbing-link-man
39+
40+
test_util.create_package "$pkg"
41+
do-link "$BPM_ORIGIN_DIR/$site/$pkg"
42+
43+
run do-list --simple
44+
45+
assert_success
46+
assert_output "local/project2"
47+
}
48+
49+
@test "properly list for 2 installed packages in mode non-simple list" {
50+
local site='github.com'
51+
2352
test_util.create_package 'username/p1'
53+
test_util.create_package 'username2/p2'
54+
test_util.create_package 'username2/p3'
55+
test_util.fake_clone "$site/username/p1"
56+
test_util.fake_clone "$site/username2/p2"
2457

2558
run do-list
2659

60+
# Note that all the tests for non-simple list do not include 'state' up to date since that is not emulated
61+
# in the test
2762
assert_success
28-
assert_output ""
63+
assert_output "$site/username2/p2
64+
Branch: master
65+
$site/username/p1
66+
Branch: master"
2967
}
3068

31-
@test "properly list for local packages" {
69+
70+
@test "properly list for local packages in mode non-simple list" {
3271
local site='github.com'
3372
local pkg='somepath/project2'
3473

@@ -43,7 +82,8 @@ load 'util/init.sh'
4382
run do-list
4483

4584
assert_success
46-
assert_output "local/project2"
85+
assert_output "local/project2
86+
Branch: master"
4787
}
4888

4989
@test "properly list outdated packages" {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bats
2+
3+
load 'util/init.sh'
4+
5+
@test "directory with GitHub site" {
6+
util.extract_data_from_package_dir "cellar/packages/github.com/eankeen/bash-args"
7+
8+
assert [ "$REPLY1" = 'github.com' ]
9+
assert [ "$REPLY2" = 'eankeen' ]
10+
assert [ "$REPLY3" = 'bash-args' ]
11+
}
12+
13+
@test "directory with GitLab site" {
14+
util.extract_data_from_package_dir "cellar/packages/gitlab.com/eankeen/bash-args"
15+
16+
assert [ "$REPLY1" = 'gitlab.com' ]
17+
assert [ "$REPLY2" = 'eankeen' ]
18+
assert [ "$REPLY3" = 'bash-args' ]
19+
}
20+
21+
@test "directory installed locally" {
22+
util.extract_data_from_package_dir "cellar/packages/local/bash-args"
23+
24+
assert [ "$REPLY1" = 'local' ]
25+
assert [ "$REPLY2" = '' ]
26+
assert [ "$REPLY3" = 'bash-args' ]
27+
}

tests/util/test_util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ test_util.create_package() {
5858
ensure.non_zero 'pkg' "$pkg"
5959

6060
test_util.setup_pkg "$pkg"; {
61-
:
61+
git branch -M master
6262
}; test_util.finish_pkg
6363
}

0 commit comments

Comments
 (0)