Skip to content

Commit 029ef95

Browse files
committed
fix: Allow 'remove' to remove (unlink) linked local repos. Fixes #42
1 parent 7d22a40 commit 029ef95

File tree

7 files changed

+71
-42
lines changed

7 files changed

+71
-42
lines changed

pkg/lib/commands/do-add.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ do-add() {
3030
local package="$REPLY3"
3131
local ref="$REPLY4"
3232

33-
if [ "${package%/*}" = 'local' ]; then
33+
if [ "$site" = 'local' ]; then
3434
die "Cannot install packages owned by username 'local' because that conflicts with linked packages"
3535
fi
3636

pkg/lib/commands/do-list.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ do-list() {
6363
repo_branch_str="Branch: $(git -C "$pkg_path" branch --show-current)"
6464
printf -v pkg_output "%s %s\n" "$pkg_output" "$repo_branch_str"
6565

66-
if git config remote.origin.url &>/dev/null; then
66+
if git -C "$pkg_path" config remote.origin.url &>/dev/null; then
6767
# shellcheck disable=SC1083
6868
if [ "$(git -C "$pkg_path" rev-list --count HEAD...HEAD@{upstream})" -gt 0 ]; then
6969
if [ -n "${NO_COLOR+x}" ] || [ "$TERM" = dumb ]; then

pkg/lib/commands/do-remove.sh

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,16 @@ do-remove() {
66
fi
77

88
for repoSpec; do
9-
# If is local directory
10-
if [ -d "$repoSpec" ]; then
11-
local dir=
12-
dir="$(util.readlink "$repoSpec")"
13-
dir="${dir%/}"
14-
15-
util.extract_data_from_package_dir "$dir"
16-
local site="$REPLY1"
17-
local package="$REPLY2/$REPLY3"
18-
19-
if [ "$dir" = "$BPM_PACKAGES_PATH/$site/$package" ]; then
20-
do_actual_removal "$site/$package"
21-
fi
9+
util.extract_data_from_input "$repoSpec"
10+
local site="$REPLY2"
11+
local package="$REPLY3"
12+
13+
if [ -d "$BPM_PACKAGES_PATH/$site/$package" ]; then
14+
do_actual_removal "$site/$package"
15+
elif [ -e "$BPM_PACKAGES_PATH/$site/$package" ]; then
16+
rm -f "$BPM_PACKAGES_PATH/$site/$package"
2217
else
23-
util.extract_data_from_input "$repoSpec"
24-
local site="$REPLY2"
25-
local package="$REPLY3"
26-
local ref="$REPLY4"
27-
28-
if [ -d "$BPM_PACKAGES_PATH/$site/$package" ]; then
29-
do_actual_removal "$site/$package"
30-
elif [ -e "$BPM_PACKAGES_PATH/$site/$package" ]; then
31-
rm -f "$BPM_PACKAGES_PATH/$site/$package"
32-
else
33-
die "Package '$site/$package' is not installed"
34-
fi
18+
die "Package '$site/$package' is not installed"
3519
fi
3620
done
3721
}
@@ -44,11 +28,16 @@ do_actual_removal() {
4428
do-plumbing-unlink-bins "$id"
4529
do-plumbing-unlink-completions "$id"
4630

47-
printf '%s\n' " -> Deleting Git repository"
48-
echo "${BPM_PACKAGES_PATH:?}/$id"
49-
rm -rf "${BPM_PACKAGES_PATH:?}/$id"
50-
if ! rmdir -p "${BPM_PACKAGES_PATH:?}/${id%/*}" &>/dev/null; then
51-
# Do not exit on failure
52-
:
31+
if [ "${id%%/*}" = 'local' ]; then
32+
printf '%s\n' " -> Unsymlinking directory"
33+
unlink "$BPM_PACKAGES_PATH/$id"
34+
else
35+
printf '%s\n' " -> Deleting Git repository"
36+
rm -rf "${BPM_PACKAGES_PATH:?}/$id"
37+
if ! rmdir -p "${BPM_PACKAGES_PATH:?}/${id%/*}" &>/dev/null; then
38+
# Do not exit on "failure"
39+
:
40+
fi
5341
fi
42+
5443
}

pkg/lib/util/util.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ util.extract_data_from_input() {
2525

2626
local regex="^https?://"
2727
local regex2="^git@"
28+
local regex3="^local/"
2829
if [[ "$repoSpec" =~ $regex ]]; then
2930
local http="${repoSpec%%://*}"
3031
repoSpec="${repoSpec#http?(s)://}"
@@ -46,6 +47,14 @@ util.extract_data_from_input() {
4647
REPLY2="$site"
4748
REPLY3="$package"
4849
REPLY4=
50+
elif [[ "$repoSpec" =~ $regex3 ]]; then
51+
repoSpec="${repoSpec#local\/}"
52+
IFS='@' read -r package ref <<< "$repoSpec"
53+
54+
REPLY1=
55+
REPLY2='local'
56+
REPLY3="$package"
57+
REPLY4="$ref"
4958
else
5059
repoSpec="${repoSpec%.git}"
5160

tests/do-remove.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ load 'util/init.sh'
5656
assert [ ! -d "$BPM_PACKAGES_PATH/github.com/$pkg" ]
5757
}
5858

59+
@test "properly remove (unlink) locally installed packages" {
60+
local site='github.com'
61+
local dir='project3'
62+
63+
test_util.setup_pkg "$dir"; {
64+
touch 'file.sh'
65+
}; test_util.finish_pkg
66+
test_util.fake_link "$dir"
67+
68+
assert [ -d "$BPM_PACKAGES_PATH/local/$dir" ]
69+
70+
run do-remove "local/$dir"
71+
72+
assert_success
73+
assert [ ! -d "$BPM_PACKAGES_PATH/local/$dir" ]
74+
}
75+
5976
@test "fails to remove package directory with wrong site name" {
6077
local site='github.com'
6178
local pkg='username/package'

tests/util-extract-data-from-input.bats

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,19 @@ load 'util/init.sh'
126126
assert [ "$REPLY3" = 'eankeen/proj' ]
127127
assert [ "$REPLY4" = 'v0.2.0' ]
128128
}
129+
130+
@test "works with local" {
131+
util.extract_data_from_input 'local/project2' 'no'
132+
assert [ "$REPLY1" = '' ]
133+
assert [ "$REPLY2" = 'local' ]
134+
assert [ "$REPLY3" = 'project2' ]
135+
assert [ "$REPLY4" = '' ]
136+
}
137+
138+
@test "works with local and ref" {
139+
util.extract_data_from_input 'local/project2@v0.2.0' 'no'
140+
assert [ "$REPLY1" = '' ]
141+
assert [ "$REPLY2" = 'local' ]
142+
assert [ "$REPLY3" = 'project2' ]
143+
assert [ "$REPLY4" = 'v0.2.0' ]
144+
}

tests/util/test_util.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,18 @@ test_util.fake_add() {
3232

3333
# @description Mocks a 'bpm link'
3434
test_util.fake_link() {
35-
local pkg="$1"
36-
ensure.non_zero 'pkg' "$pkg"
35+
local dir="$1"
36+
ensure.non_zero 'dir' "$dir"
3737

3838
mkdir -p "$BPM_PACKAGES_PATH/local"
3939

40-
ls -al "$BPM_ORIGIN_DIR/github.com/$pkg" >&3
41-
ls -al "$BPM_PACKAGES_PATH/local" >&3
4240
mkdir -p "$BPM_PACKAGES_PATH/local"
43-
ln -s "$BPM_ORIGIN_DIR/github.com/$pkg" "$BPM_PACKAGES_PATH/local"
41+
ln -s "$BPM_ORIGIN_DIR/github.com/$dir" "$BPM_PACKAGES_PATH/local"
4442

45-
do-plumbing-add-deps "$pkg"
46-
do-plumbing-link-bins "$pkg"
47-
do-plumbing-link-completions "$pkg"
48-
do-plumbing-link-man "$pkg"
43+
do-plumbing-add-deps "local/$dir"
44+
do-plumbing-link-bins "local/$dir"
45+
do-plumbing-link-completions "local/$dir"
46+
do-plumbing-link-man "local/$dir"
4947
}
5048

5149
# @description Creates a 'bpm package', and cd's into it

0 commit comments

Comments
 (0)