From b71416f6100733810c0d4c7e12b6d4fbba16306c Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 17 May 2022 17:02:46 +0200 Subject: [PATCH] fish_add_path: Also deduplicate the new paths Previously, running `fish_add_path /foo /foo` would result in /foo being added to $PATH twice. Now we check that it hasn't already been given, so we skip the second (and any further) occurence. --- share/functions/fish_add_path.fish | 4 ++-- tests/checks/fish_add_path.fish | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/share/functions/fish_add_path.fish b/share/functions/fish_add_path.fish index 3b2ef96f5..21151c1e8 100644 --- a/share/functions/fish_add_path.fish +++ b/share/functions/fish_add_path.fish @@ -58,11 +58,11 @@ function fish_add_path --description "Add paths to the PATH" if set -l ind (contains -i -- $p $$var) # In move-mode, we remove it from its current position and add it back. - if set -q _flag_move + if set -q _flag_move; and not contains -- $p $newpaths set -a indexes $ind set -a newpaths $p end - else + else if not contains -- $p $newpaths # Without move, we only add it if it's not in. set -a newpaths $p end diff --git a/tests/checks/fish_add_path.fish b/tests/checks/fish_add_path.fish index 948498e3b..44e0bee6f 100644 --- a/tests/checks/fish_add_path.fish +++ b/tests/checks/fish_add_path.fish @@ -58,4 +58,7 @@ or echo "PATH CHANGED!!!" >&2 PATH=$tmpdir/{bin,etc,link,sbin} fish_add_path -nPpm $tmpdir/{link,sbin} | string replace -a $tmpdir '' # CHECK: set -g PATH /link /sbin /bin /etc +# See that trying to add a path twice doesn't duplicate it +PATH=$tmpdir/{bin,etc,link,sbin} fish_add_path -nPpm $tmpdir/sbin{,} | string replace -a $tmpdir '' +# CHECK: set -g PATH /sbin /link /bin /etc exit 0