From e73f1de77e15049f4319d7c79473a1ae71e6803e Mon Sep 17 00:00:00 2001 From: amtoine Date: Sat, 18 Nov 2023 19:44:13 +0100 Subject: [PATCH 1/9] refactor in `fs path "path remove-prefix` --- src/nu-git-manager/fs/path.nu | 7 +++++++ src/nu-git-manager/mod.nu | 21 ++++++--------------- tests/mod.nu | 18 +++++------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/nu-git-manager/fs/path.nu b/src/nu-git-manager/fs/path.nu index 051d67f2..92831818 100644 --- a/src/nu-git-manager/fs/path.nu +++ b/src/nu-git-manager/fs/path.nu @@ -2,3 +2,10 @@ export def "path sanitize" []: path -> path { str replace --regex '^.:' '' | str replace --all '\' '/' } + +# remove a prefix from a path or a list of paths +# +# /!\ paths need to be sanitized /!\ +export def "path remove-prefix" [prefix: path]: [path -> string, list -> list] { + str replace $prefix '' | str trim --left --char "/" +} diff --git a/src/nu-git-manager/mod.nu b/src/nu-git-manager/mod.nu index 663f16ed..793b58c3 100644 --- a/src/nu-git-manager/mod.nu +++ b/src/nu-git-manager/mod.nu @@ -6,7 +6,7 @@ use fs/cache.nu [ save-cache, clean-cache-dir ] use fs/dir.nu [clean-empty-directories-rec] -use fs/path.nu ["path sanitize"] +use fs/path.nu ["path sanitize", "path remove-prefix"] use git/url.nu [parse-git-url, get-fetch-push-urls] use git/repo.nu [is-grafted, get-root-commit, list-remotes] use error/error.nu [throw-error, throw-warning] @@ -152,9 +152,7 @@ export def "gm clone" [ $"this repo is a fork of (ansi cyan)($msg)(ansi reset) because they share the same root commit: (ansi magenta)($repo.root_hash)(ansi reset)\n" + ( $forks | get path | each { - let repo = $in - | str replace (get-repo-store-path) '' - | str trim --left --char "/" + let repo = $in | path remove-prefix (get-repo-store-path) $"- (ansi cyan)($repo)(ansi reset)" } | str join "\n" ) @@ -193,9 +191,7 @@ export def "gm list" [ if $full_path { open-cache $cache_file | get path } else { - open-cache $cache_file | get path | each { - str replace (get-repo-store-path) '' | str trim --left --char "/" - } + open-cache $cache_file | get path | each { path remove-prefix (get-repo-store-path) } } } @@ -302,11 +298,7 @@ export def "gm remove" [ --no-confirm # do not ask for confirmation: useful in scripts but requires a single match ]: nothing -> nothing { let root = get-repo-store-path - let choices = gm list - | each { - str replace $root '' | str trim --left --char "/" - } - | find $pattern + let choices = gm list | each { path remove-prefix $root } | find $pattern let repo_to_remove = match ($choices | length) { 0 => { @@ -438,8 +430,7 @@ export def "gm squash-forks" [ let default = $non_interactive_preselect | get --ignore-errors $forks.root_hash.0 let main = if $default == null { let choice = $forks.path - | str replace $status.root.path '' - | str trim --char '/' + | path remove-prefix $status.root.path | input list $"Please choose a main fork to squash ($forks.root_hash.0)" if ($choice | is-empty) { log warning $"skipping ($forks.root_hash.0)" @@ -458,7 +449,7 @@ export def "gm squash-forks" [ let fork_origin = list-remotes $fork | where remote == "origin" | into record let fork_name = $fork | path split | reverse | get 1 - let fork_full_name = $fork | str replace $status.root.path '' | str trim --char '/' + let fork_full_name = $fork | path remove-prefix $status.root.path log debug $" squashing ($fork_full_name)" diff --git a/tests/mod.nu b/tests/mod.nu index 1b265ee0..b17aa7c7 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -7,7 +7,7 @@ use ../src/nu-git-manager/fs/cache.nu [ get-repo-store-cache-path, check-cache-file, add-to-cache, remove-from-cache, open-cache, save-cache, clean-cache-dir ] -use ../src/nu-git-manager/fs/path.nu "path sanitize" +use ../src/nu-git-manager/fs/path.nu ["path sanitize", "path remove-prefix"] use ../src/nu-git-manager/fs/dir.nu [clean-empty-directories-rec] export def path-sanitization [] { @@ -160,9 +160,7 @@ export def list-all-repos-in-store [] { } # NOTE: remove the path to BASE so that the test output is easy to read - let actual = with-env {GIT_REPOS_HOME: $BASE} { list-repos-in-store } | each { - str replace $BASE '' | str trim --left --char "/" - } + let actual = with-env {GIT_REPOS_HOME: $BASE} { list-repos-in-store } | each { path remove-prefix $BASE } let expected = $store | where in_store | get path | each { # NOTE: `list-repos-in-store` does not add `/` at the end of the paths str trim --right --char "/" @@ -188,16 +186,11 @@ export def cache-manipulation [] { } def "assert cache" [cache: list]: nothing -> nothing { - let actual = open-cache $CACHE - | update path { str replace (pwd | path sanitize) '' | str trim --left --char '/' } + let actual = open-cache $CACHE | update path { path remove-prefix (pwd | path sanitize) } let expected = $cache | each {|it| $BASE_REPO | update path { - $it - | path expand - | path sanitize - | str replace (pwd | path sanitize) '' - | str trim --left --char '/' + $it | path expand | path sanitize | each { path remove-prefix (pwd | path sanitize) } } } assert equal $actual $expected @@ -335,8 +328,7 @@ export def store-cleaning [] { $path } | clean-empty-directories-rec - | str replace ($env.GIT_REPOS_HOME | path sanitize) '' - | str trim --char '/' + | path remove-prefix ($env.GIT_REPOS_HOME | path sanitize) let expected = [ "foo/bar", "bar", From a434a6d9b9332e76eb6bb3d6c9f26b382977246e Mon Sep 17 00:00:00 2001 From: amtoine Date: Sat, 18 Nov 2023 19:45:36 +0100 Subject: [PATCH 2/9] do not use `path remove-prefix` in `each` `str replace` and `str trim` already vectorize. --- src/nu-git-manager/mod.nu | 4 ++-- tests/mod.nu | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nu-git-manager/mod.nu b/src/nu-git-manager/mod.nu index 793b58c3..dbd5b74f 100644 --- a/src/nu-git-manager/mod.nu +++ b/src/nu-git-manager/mod.nu @@ -191,7 +191,7 @@ export def "gm list" [ if $full_path { open-cache $cache_file | get path } else { - open-cache $cache_file | get path | each { path remove-prefix (get-repo-store-path) } + open-cache $cache_file | get path | path remove-prefix (get-repo-store-path) } } @@ -298,7 +298,7 @@ export def "gm remove" [ --no-confirm # do not ask for confirmation: useful in scripts but requires a single match ]: nothing -> nothing { let root = get-repo-store-path - let choices = gm list | each { path remove-prefix $root } | find $pattern + let choices = gm list | path remove-prefix $root | find $pattern let repo_to_remove = match ($choices | length) { 0 => { diff --git a/tests/mod.nu b/tests/mod.nu index b17aa7c7..9727d93a 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -160,7 +160,7 @@ export def list-all-repos-in-store [] { } # NOTE: remove the path to BASE so that the test output is easy to read - let actual = with-env {GIT_REPOS_HOME: $BASE} { list-repos-in-store } | each { path remove-prefix $BASE } + let actual = with-env {GIT_REPOS_HOME: $BASE} { list-repos-in-store } | path remove-prefix $BASE let expected = $store | where in_store | get path | each { # NOTE: `list-repos-in-store` does not add `/` at the end of the paths str trim --right --char "/" @@ -190,7 +190,7 @@ export def cache-manipulation [] { let expected = $cache | each {|it| $BASE_REPO | update path { - $it | path expand | path sanitize | each { path remove-prefix (pwd | path sanitize) } + $it | path expand | path sanitize | path remove-prefix (pwd | path sanitize) } } assert equal $actual $expected From df3c3d2c9e59c8119696089d1b5599d9f1e60899 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sat, 18 Nov 2023 19:49:37 +0100 Subject: [PATCH 3/9] fix the "store cleaning" test --- tests/mod.nu | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/mod.nu b/tests/mod.nu index 9727d93a..a7cc8377 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -329,6 +329,7 @@ export def store-cleaning [] { } | clean-empty-directories-rec | path remove-prefix ($env.GIT_REPOS_HOME | path sanitize) + | str trim --right --char '/' let expected = [ "foo/bar", "bar", From f6f1b804a870f0bcf3a1e97e6303207c2b02d043 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sat, 18 Nov 2023 19:56:33 +0100 Subject: [PATCH 4/9] refactor the "trailing /" trimming in `fs path` --- src/nu-git-manager/fs/path.nu | 7 +++++++ src/nu-git-manager/fs/store.nu | 4 ++-- tests/mod.nu | 12 ++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/nu-git-manager/fs/path.nu b/src/nu-git-manager/fs/path.nu index 92831818..7b2187a4 100644 --- a/src/nu-git-manager/fs/path.nu +++ b/src/nu-git-manager/fs/path.nu @@ -9,3 +9,10 @@ export def "path sanitize" []: path -> path { export def "path remove-prefix" [prefix: path]: [path -> string, list -> list] { str replace $prefix '' | str trim --left --char "/" } + +# remove the trailing `/` from a path or a list of paths +# +# /!\ paths need to be sanitized /!\ +export def "path remove-trailing-path-sep" []: [path -> path , list -> list] { + str trim --right --char "/" +} diff --git a/src/nu-git-manager/fs/store.nu b/src/nu-git-manager/fs/store.nu index 51ccaeea..542d18c9 100644 --- a/src/nu-git-manager/fs/store.nu +++ b/src/nu-git-manager/fs/store.nu @@ -1,5 +1,5 @@ use std log -use path.nu "path sanitize" +use path.nu ["path sanitize", "path remove-trailing-path-sep"] # get the root of the local repo store # @@ -44,5 +44,5 @@ export def list-repos-in-store []: nothing -> list { | filter {|it| not ($it.0 | str starts-with $it.1)} | each { get 0 } | prepend $sorted.0 - | str trim --right --char "/" + | path remove-trailing-path-sep } diff --git a/tests/mod.nu b/tests/mod.nu index a7cc8377..b39d8fbd 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -7,7 +7,9 @@ use ../src/nu-git-manager/fs/cache.nu [ get-repo-store-cache-path, check-cache-file, add-to-cache, remove-from-cache, open-cache, save-cache, clean-cache-dir ] -use ../src/nu-git-manager/fs/path.nu ["path sanitize", "path remove-prefix"] +use ../src/nu-git-manager/fs/path.nu [ + "path sanitize", "path remove-prefix", "path remove-trailing-path-sep" +] use ../src/nu-git-manager/fs/dir.nu [clean-empty-directories-rec] export def path-sanitization [] { @@ -161,10 +163,8 @@ export def list-all-repos-in-store [] { # NOTE: remove the path to BASE so that the test output is easy to read let actual = with-env {GIT_REPOS_HOME: $BASE} { list-repos-in-store } | path remove-prefix $BASE - let expected = $store | where in_store | get path | each { - # NOTE: `list-repos-in-store` does not add `/` at the end of the paths - str trim --right --char "/" - } + # NOTE: `list-repos-in-store` does not add `/` at the end of the paths + let expected = $store | where in_store | get path | path remove-trailing-path-sep # NOTE: need to sort the result to make sure the order of the `git init` does not influence the # results of the test @@ -329,7 +329,7 @@ export def store-cleaning [] { } | clean-empty-directories-rec | path remove-prefix ($env.GIT_REPOS_HOME | path sanitize) - | str trim --right --char '/' + | path remove-trailing-path-sep let expected = [ "foo/bar", "bar", From 45ae25761e03748a597919a6e2f65fed8a9ea965 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sun, 19 Nov 2023 15:35:19 +0100 Subject: [PATCH 5/9] refactor "path" tests in module `path` --- tests/mod.nu | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/mod.nu b/tests/mod.nu index b39d8fbd..3d7df670 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -12,8 +12,10 @@ use ../src/nu-git-manager/fs/path.nu [ ] use ../src/nu-git-manager/fs/dir.nu [clean-empty-directories-rec] -export def path-sanitization [] { - assert equal ('\foo\bar' | path sanitize) "/foo/bar" +export module path { + export def sanitization [] { + assert equal ('\foo\bar' | path sanitize) "/foo/bar" + } } export def git-url-parsing [] { From b0bd44a7279410e8eec1cb20fec49915e9483024 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sun, 19 Nov 2023 15:40:31 +0100 Subject: [PATCH 6/9] add tests --- tests/mod.nu | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/mod.nu b/tests/mod.nu index 3d7df670..a9d5274a 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -16,6 +16,20 @@ export module path { export def sanitization [] { assert equal ('\foo\bar' | path sanitize) "/foo/bar" } + + export def remove-prefix [] { + assert equal ("/foo/bar" | path remove-prefix "/foo") "bar" + assert equal ("/foo/bar" | path remove-prefix "/bar") "/foo/bar" + assert equal ("/foo/bar/baz" | path remove-prefix "/foo") "bar/baz" + assert equal (["/foo/bar", "/foo/bar/baz"] | path remove-prefix "/foo") ["bar", "bar/baz"] + } + + export def remove-trailing-path-sep [] { + assert equal ("/foo/bar/" | path remove-trailing-path-sep) "/foo/bar" + assert equal ("/foo/bar" | path remove-trailing-path-sep) "/foo/bar" + assert equal (["/foo/bar", "/foo/bar/"] | path remove-trailing-path-sep) ["/foo/bar", "/foo/bar"] + } + } export def git-url-parsing [] { From 4f57df3e8aff2169208214d04903a847ac36f99a Mon Sep 17 00:00:00 2001 From: amtoine Date: Sun, 19 Nov 2023 15:50:31 +0100 Subject: [PATCH 7/9] fix `path remove-prefix` --- src/nu-git-manager/fs/path.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nu-git-manager/fs/path.nu b/src/nu-git-manager/fs/path.nu index 7b2187a4..e5e1e4a4 100644 --- a/src/nu-git-manager/fs/path.nu +++ b/src/nu-git-manager/fs/path.nu @@ -7,7 +7,7 @@ export def "path sanitize" []: path -> path { # # /!\ paths need to be sanitized /!\ export def "path remove-prefix" [prefix: path]: [path -> string, list -> list] { - str replace $prefix '' | str trim --left --char "/" + str replace --regex ('^' + $prefix + '/') '' } # remove the trailing `/` from a path or a list of paths From 2324f6b4443b31d5aebc4e82e2b78cd35e6346bf Mon Sep 17 00:00:00 2001 From: amtoine Date: Sun, 19 Nov 2023 15:54:27 +0100 Subject: [PATCH 8/9] fix the `store-cleaning` test --- tests/mod.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mod.nu b/tests/mod.nu index a9d5274a..bd4fff60 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -353,7 +353,7 @@ export def store-cleaning [] { "foo", "baz/foo", "baz", - "", + ($env.GIT_REPOS_HOME | path sanitize), ] assert equal $actual $expected From 870af2c3dbe5d776715d476062f4eaecc62a0c6f Mon Sep 17 00:00:00 2001 From: amtoine Date: Sun, 19 Nov 2023 16:01:50 +0100 Subject: [PATCH 9/9] do not expand `prefix` path in `path remove-prefix` this will cause issues on Windows... --- src/nu-git-manager/fs/path.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nu-git-manager/fs/path.nu b/src/nu-git-manager/fs/path.nu index e5e1e4a4..6613dcbe 100644 --- a/src/nu-git-manager/fs/path.nu +++ b/src/nu-git-manager/fs/path.nu @@ -6,7 +6,7 @@ export def "path sanitize" []: path -> path { # remove a prefix from a path or a list of paths # # /!\ paths need to be sanitized /!\ -export def "path remove-prefix" [prefix: path]: [path -> string, list -> list] { +export def "path remove-prefix" [prefix: string]: [path -> string, list -> list] { str replace --regex ('^' + $prefix + '/') '' }