Skip to content

Commit 303af7f

Browse files
committed
fix(cli): preserve single-root archive layout
1 parent 1b27cd5 commit 303af7f

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

.local/bin/unarchive

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ case "$name" in
2323
esac
2424

2525
extract_tar() {
26+
local tar_command=tar
2627
if command -v gtar >/dev/null 2>&1; then
27-
gtar xf "$1" --one-top-level="$name"
28-
return
28+
tar_command=gtar
2929
fi
3030

31-
# BSD tar has no --one-top-level. Count distinct top-level entries so that
32-
# archives with one root are extracted in place and multi-root archives are
33-
# kept together under the archive name.
31+
# Count distinct top-level entries so that archives with one root are
32+
# extracted in place and multi-root archives are kept together under the
33+
# archive name.
3434
local top_level_count
3535
top_level_count=$(
36-
tar tf "$1" |
36+
"$tar_command" tf "$1" |
3737
awk '
3838
{
3939
entry = $0
@@ -49,10 +49,14 @@ extract_tar() {
4949
)
5050

5151
if (( top_level_count > 1 )); then
52-
mkdir -p "$name"
53-
tar xf "$1" -C "$name"
52+
if [[ $tar_command == gtar ]]; then
53+
"$tar_command" xf "$1" --one-top-level="$name"
54+
else
55+
mkdir -p "$name"
56+
"$tar_command" xf "$1" -C "$name"
57+
fi
5458
else
55-
tar xf "$1"
59+
"$tar_command" xf "$1"
5660
fi
5761
}
5862

0 commit comments

Comments
 (0)