Skip to content

Commit a3df445

Browse files
committed
feat: Support cloning repository directly through SSH. Closes #27
This refactors, most noticeably, the do-plumbing-clone command to support 'raw' URLs. The code will have to be cleaned up later
1 parent 02c5b1f commit a3df445

File tree

4 files changed

+128
-21
lines changed

4 files changed

+128
-21
lines changed

pkg/lib/commands/do-install.sh

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

33
do-install() {
4-
local use_ssh="false"
4+
local with_ssh="no"
55

66
case "$1" in
77
--ssh)
8-
use_ssh="true"
8+
with_ssh="yes"
99
shift
1010
;;
1111
esac
@@ -15,16 +15,16 @@ do-install() {
1515
fi
1616

1717
for repoSpec; do
18-
local site= user= repository= ref=
19-
20-
util.parse_package_full "$repoSpec"
21-
IFS=':' read -r site user repository ref <<< "$REPLY"
18+
util.construct_clone_url "$repoSpec" "$with_ssh"
19+
local uri="$REPLY1"
20+
local package="$REPLY2"
21+
local ref="$REPLY3"
2222

2323
log.info "Installing '$repoSpec'"
24-
do-plumbing-clone "$use_ssh" "$site" "$user/$repository" $ref
25-
do-plumbing-deps "$user/$repository"
26-
do-plumbing-link-bins "$user/$repository"
27-
do-plumbing-link-completions "$user/$repository"
28-
do-plumbing-link-man "$user/$repository"
24+
do-plumbing-clone 'raw' "$uri" $ref
25+
do-plumbing-deps "$package"
26+
do-plumbing-link-bins "$package"
27+
do-plumbing-link-completions "$package"
28+
do-plumbing-link-man "$package"
2929
done
3030
}

pkg/lib/commands/do-plumbing-clone.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@ do-plumbing-clone() {
66
local package="$3"
77
local ref="$4"
88

9+
if [ "$use_ssh" = raw ]; then
10+
local uri="$site"
11+
ref="$package"
12+
13+
ensure.nonZero 'uri' "$uri"
14+
ensure.nonZero 'ref' "$ref"
15+
16+
if [ -e "$BPM_PACKAGES_PATH/$package" ]; then
17+
die "Package '$package' is already present"
18+
fi
19+
20+
local -a gitArgs=(--recursive)
21+
22+
if [ -z "${BPM_FULL_CLONE+x}" ]; then
23+
gitArgs+=(--depth=1)
24+
fi
25+
26+
if [ -n "$ref" ]; then
27+
gitArgs+=(--branch "$ref")
28+
fi
29+
30+
gitArgs+=("$uri")
31+
gitArgs+=("$BPM_PACKAGES_PATH/$package")
32+
33+
git clone "${gitArgs[@]}"
34+
35+
return
36+
fi
37+
38+
# TODO: replace this
939
ensure.nonZero 'use_ssh' "$use_ssh"
1040
ensure.nonZero 'site' "$site"
1141
ensure.nonZero 'package' "$package"

pkg/lib/util/util.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,70 @@ util.parse_package_full() {
3636
REPLY="$site:$user:$repository:$ref"
3737
}
3838

39+
# @description Generate the final URL to clone from
40+
# @arg $1 repoSpec
41+
# @arg $2 with_ssh Whether to clone with SSH (yes/no)
42+
util.construct_clone_url() {
43+
REPLY1=
44+
REPLY2=
45+
REPLY3=
46+
47+
local repoSpec="$1"
48+
local with_ssh="$2"
49+
50+
if [ -z "$repoSpec" ]; then
51+
die "Must supply a repository"
52+
fi
53+
54+
local site= package= ref=
55+
56+
local regex="^https?://"
57+
local regex2="^git@"
58+
if [[ "$repoSpec" =~ $regex ]]; then
59+
local http="${repoSpec%%://*}"
60+
repoSpec="${repoSpec#http?(s)://}"
61+
repoSpec="${repoSpec%.git}"
62+
63+
IFS='/' read -r site package <<< "$repoSpec"
64+
65+
REPLY1="$http://$repoSpec.git"
66+
REPLY2="$package"
67+
REPLY3=
68+
elif [[ "$repoSpec" =~ $regex2 ]]; then
69+
repoSpec="${repoSpec#git@}"
70+
repoSpec="${repoSpec%.git}"
71+
72+
IFS=':' read -r site package <<< "$repoSpec"
73+
74+
REPLY1="git@$repoSpec.git"
75+
REPLY2="$package"
76+
REPLY3=
77+
else
78+
repoSpec="${repoSpec%.git}"
79+
80+
if [[ "$repoSpec" = */*/* ]]; then
81+
IFS='/' read -r site package <<< "$repoSpec"
82+
elif [[ "$repoSpec" = */* ]]; then
83+
site="github.com"
84+
package="$repoSpec"
85+
else
86+
die "Invalid repository"
87+
fi
88+
89+
if [[ "$package" = *@* ]]; then
90+
IFS='@' read -r package ref <<< "$package"
91+
fi
92+
93+
if [ "$with_ssh" = yes ]; then
94+
REPLY1="git@$site:$package.git"
95+
else
96+
REPLY1="https://$site/$package.git"
97+
fi
98+
REPLY2="$package"
99+
REPLY3="$ref"
100+
fi
101+
}
102+
39103
util.readlink() {
40104
if command -v realpath &>/dev/null; then
41105
realpath "$1"

tests/do-install.bats

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ load 'util/init.sh'
2626

2727
assert_success
2828
assert_line -n 0 -p "Installing 'username/package'"
29-
assert_line -n 1 'do-plumbing-clone false github.com username/package'
29+
assert_line -n 1 'do-plumbing-clone raw https://github.com/username/package.git'
3030
assert_line -n 2 'do-plumbing-deps username/package'
3131
assert_line -n 3 'do-plumbing-link-bins username/package'
3232
assert_line -n 4 'do-plumbing-link-completions username/package'
@@ -44,13 +44,13 @@ load 'util/init.sh'
4444

4545
assert_success
4646
assert_line -n 0 -p "Installing 'username/package'"
47-
assert_line -n 1 'do-plumbing-clone false github.com username/package'
47+
assert_line -n 1 'do-plumbing-clone raw https://github.com/username/package.git'
4848
assert_line -n 2 'do-plumbing-deps username/package'
4949
assert_line -n 3 'do-plumbing-link-bins username/package'
5050
assert_line -n 4 'do-plumbing-link-completions username/package'
5151
assert_line -n 5 'do-plumbing-link-man username/package'
5252
assert_line -n 6 -p "Installing 'username2/package2'"
53-
assert_line -n 7 'do-plumbing-clone false github.com username2/package2'
53+
assert_line -n 7 'do-plumbing-clone raw https://github.com/username2/package2.git'
5454
assert_line -n 8 'do-plumbing-deps username2/package2'
5555
assert_line -n 9 'do-plumbing-link-bins username2/package2'
5656
assert_line -n 10 'do-plumbing-link-completions username2/package2'
@@ -68,7 +68,7 @@ load 'util/init.sh'
6868
run do-install https://gitlab.com/username/package
6969

7070
assert_success
71-
assert_line "do-plumbing-clone false gitlab.com username/package"
71+
assert_line "do-plumbing-clone raw https://gitlab.com/username/package.git"
7272
}
7373

7474
@test "uses longhand (http) site to clone from, if specified" {
@@ -81,7 +81,7 @@ load 'util/init.sh'
8181
run do-install http://gitlab.com/username/package
8282

8383
assert_success
84-
assert_line "do-plumbing-clone false gitlab.com username/package"
84+
assert_line "do-plumbing-clone raw http://gitlab.com/username/package.git"
8585
}
8686

8787
@test "uses shorthand site to clone from, if specified" {
@@ -94,7 +94,7 @@ load 'util/init.sh'
9494
run do-install site/username/package
9595

9696
assert_success
97-
assert_line "do-plumbing-clone false site username/package"
97+
assert_line "do-plumbing-clone raw https://site/username/package.git"
9898
}
9999

100100
@test "uses GitHub as default site, if not specified" {
@@ -107,7 +107,7 @@ load 'util/init.sh'
107107
run do-install username/package
108108

109109
assert_success
110-
assert_line "do-plumbing-clone false github.com username/package"
110+
assert_line "do-plumbing-clone raw https://github.com/username/package.git"
111111
}
112112

113113
@test "uses ssh protocol, when specified" {
@@ -120,7 +120,20 @@ load 'util/init.sh'
120120
run do-install --ssh username/package
121121

122122
assert_success
123-
assert_line "do-plumbing-clone true github.com username/package"
123+
assert_line "do-plumbing-clone raw git@github.com:username/package.git"
124+
}
125+
126+
@test "uses ssh protocol raw, when specified" {
127+
test_util.mock_command do-plumbing-clone
128+
test_util.mock_command do-plumbing-deps
129+
test_util.mock_command do-plumbing-link-bins
130+
test_util.mock_command do-plumbing-link-completions
131+
test_util.mock_command do-plumbing-link-man
132+
133+
run do-install git@github.com:username/package
134+
135+
assert_success
136+
assert_line "do-plumbing-clone raw git@github.com:username/package.git"
124137
}
125138

126139
@test "uses custom version, when specified" {
@@ -133,7 +146,7 @@ load 'util/init.sh'
133146
run do-install username/package@v1.2.3
134147

135148
assert_success
136-
assert_line "do-plumbing-clone false github.com username/package v1.2.3"
149+
assert_line "do-plumbing-clone raw https://github.com/username/package.git v1.2.3"
137150
}
138151

139152
@test "does not use custom version, when not specified" {
@@ -146,5 +159,5 @@ load 'util/init.sh'
146159
run do-install username/package@
147160

148161
assert_success
149-
assert_line "do-plumbing-clone false github.com username/package"
162+
assert_line "do-plumbing-clone raw https://github.com/username/package.git"
150163
}

0 commit comments

Comments
 (0)