3a01d7df82157a5f3aef01eab9a940e4cba2283a

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

fix(extract): fix extract dir naming conflicts

Fixes #11642

Diff

 1diff --git a/plugins/extract/extract.plugin.zsh b/plugins/extract/extract.plugin.zsh
 2index 34c0806537589d075f868f814b12f6d49e21f67d..ee1d38b3f04f99d1e8c981d9637c652ca1c5c8c3 100644
 3--- a/plugins/extract/extract.plugin.zsh
 4+++ b/plugins/extract/extract.plugin.zsh
 5@@ -27,12 +27,20 @@ EOF
 6     fi
 7 
 8     local success=0
 9-    local extract_dir="${1:t:r}"
10     local file="$1" full_path="${1:A}"
11+    local extract_dir="${1:t:r}"
12+
13+    # If there's a file or directory with the same name as the archive
14+    # add a random string to the end of the extract directory
15+    if [[ -e "$extract_dir" ]]; then
16+      local rnd="${(L)"${$(( [##36]$RANDOM*$RANDOM ))}":1:5}"
17+      extract_dir="${extract_dir}-${rnd}"
18+    fi
19 
20     # Create an extraction directory based on the file name
21     command mkdir -p "$extract_dir"
22     builtin cd -q "$extract_dir"
23+    echo "extract: extracting to $extract_dir" >&2
24 
25     case "${file:l}" in
26       (*.tar.gz|*.tgz)
27@@ -107,11 +115,13 @@ EOF
28       if [[ "${content[1]:t}" == "$extract_dir" ]]; then
29         # =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
30         local tmp_dir==(:); tmp_dir="${tmp_dir:t}"
31-        command mv -f "${content[1]}" "$tmp_dir" \
32+        command mv "${content[1]}" "$tmp_dir" \
33         && command rmdir "$extract_dir" \
34-        && command mv -f "$tmp_dir" "$extract_dir"
35-      else
36-        command mv -f "${content[1]}" . \
37+        && command mv "$tmp_dir" "$extract_dir"
38+      # Otherwise, if the extracted folder name already exists in the current
39+      # directory (because of a previous file / folder), keep the extract_dir
40+      elif [[ ! -e "${content[1]:t}" ]]; then
41+        command mv "${content[1]}" . \
42         && command rmdir "$extract_dir"
43       fi
44     elif [[ ${#content} -eq 0 ]]; then