From c0eb0397447c79634e6b99de9b179fe471dc0401 Mon Sep 17 00:00:00 2001 From: amtoine Date: Mon, 27 Nov 2023 18:55:38 +0100 Subject: [PATCH 1/8] add `gm for-each` --- src/nu-git-manager-sugar/extra.nu | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/nu-git-manager-sugar/extra.nu b/src/nu-git-manager-sugar/extra.nu index eb4e9290..8a9ddd9a 100644 --- a/src/nu-git-manager-sugar/extra.nu +++ b/src/nu-git-manager-sugar/extra.nu @@ -49,3 +49,10 @@ export def "gm report" []: nothing -> table any { + gm list --full-path | each {|repo| + cd $repo + do $code + } +} From 2c90cf4fdbf6415ace14b31f313ba404ade1fc4c Mon Sep 17 00:00:00 2001 From: amtoine Date: Wed, 29 Nov 2023 19:17:42 +0100 Subject: [PATCH 2/8] add some documentation --- src/nu-git-manager-sugar/extra.nu | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/nu-git-manager-sugar/extra.nu b/src/nu-git-manager-sugar/extra.nu index 8a9ddd9a..ab708c4b 100644 --- a/src/nu-git-manager-sugar/extra.nu +++ b/src/nu-git-manager-sugar/extra.nu @@ -50,6 +50,17 @@ export def "gm report" []: nothing -> table gm for-each { git status } +# +# get the number of files tracked by each repo: list +# > gm for-each { git lf | lines | length } export def "gm for-each" [code: closure]: nothing -> any { gm list --full-path | each {|repo| cd $repo From 5985a99c0ae5c7456e2d917f7efec1854bf6ec6e Mon Sep 17 00:00:00 2001 From: amtoine Date: Wed, 29 Nov 2023 19:22:23 +0100 Subject: [PATCH 3/8] give the name of the repo as parameter to closure --- src/nu-git-manager-sugar/extra.nu | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/nu-git-manager-sugar/extra.nu b/src/nu-git-manager-sugar/extra.nu index ab708c4b..323a5755 100644 --- a/src/nu-git-manager-sugar/extra.nu +++ b/src/nu-git-manager-sugar/extra.nu @@ -62,8 +62,10 @@ export def "gm report" []: nothing -> table # > gm for-each { git lf | lines | length } export def "gm for-each" [code: closure]: nothing -> any { - gm list --full-path | each {|repo| - cd $repo - do $code + let root = gm status | get root.path + + gm list | each {|repo| + cd ($root | path join $repo) + do $code $repo } } From 953902460142a290cdee3e8cf11430dcbb790376 Mon Sep 17 00:00:00 2001 From: amtoine Date: Wed, 29 Nov 2023 19:23:56 +0100 Subject: [PATCH 4/8] add documentation about closure parameter --- src/nu-git-manager-sugar/extra.nu | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/nu-git-manager-sugar/extra.nu b/src/nu-git-manager-sugar/extra.nu index 323a5755..57bc1596 100644 --- a/src/nu-git-manager-sugar/extra.nu +++ b/src/nu-git-manager-sugar/extra.nu @@ -55,12 +55,20 @@ export def "gm report" []: nothing -> table gm for-each { git status } # # get the number of files tracked by each repo: list # > gm for-each { git lf | lines | length } +# +# get the number of status lines of each repo: table +# > gm for-each { |r| { +# repo: $r, +# status: (git status --short | lines | length) +# } } export def "gm for-each" [code: closure]: nothing -> any { let root = gm status | get root.path From 97ea44b1f4df8cfeafadbc75d9554fd2bae5e66c Mon Sep 17 00:00:00 2001 From: amtoine Date: Wed, 29 Nov 2023 19:25:06 +0100 Subject: [PATCH 5/8] fix the import tests --- tests/sugar/mod.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sugar/mod.nu b/tests/sugar/mod.nu index 45e559c1..971e8535 100644 --- a/tests/sugar/mod.nu +++ b/tests/sugar/mod.nu @@ -10,7 +10,7 @@ module imports { } export def extra [] { - assert imports $MODULE "extra" [ "gm report" ] + assert imports $MODULE "extra" [ "gm for-each", "gm report" ] } export def git [] { From d6b76d9c6e8b0b28ad14733bf0b8e303abb528a5 Mon Sep 17 00:00:00 2001 From: amtoine Date: Wed, 29 Nov 2023 19:31:19 +0100 Subject: [PATCH 6/8] refactor `run-with-env` in `common/` --- tests/common/main.nu | 16 ++++++++++++++++ tests/gm.nu | 18 +----------------- 2 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 tests/common/main.nu diff --git a/tests/common/main.nu b/tests/common/main.nu new file mode 100644 index 00000000..37e28d32 --- /dev/null +++ b/tests/common/main.nu @@ -0,0 +1,16 @@ +export def run-with-env [code: closure, --prepare-cache] { + let TEST_ENV_BASE = get-random-test-dir + + let TEST_ENV = { + GIT_REPOS_HOME: ($TEST_ENV_BASE | path join "repos"), + GIT_REPOS_CACHE: ($TEST_ENV_BASE | path join "repos.cache"), + } + + if $prepare_cache { + with-env $TEST_ENV { gm update-cache } + } + + with-env $TEST_ENV $code + + rm --recursive --force --verbose $TEST_ENV_BASE +} diff --git a/tests/gm.nu b/tests/gm.nu index 0155c9a4..ee893729 100644 --- a/tests/gm.nu +++ b/tests/gm.nu @@ -6,23 +6,7 @@ use ../src/nu-git-manager/ * use common/setup.nu [get-random-test-dir] use common/import.nu ["assert imports"] - -def run-with-env [code: closure, --prepare-cache] { - let TEST_ENV_BASE = get-random-test-dir - - let TEST_ENV = { - GIT_REPOS_HOME: ($TEST_ENV_BASE | path join "repos"), - GIT_REPOS_CACHE: ($TEST_ENV_BASE | path join "repos.cache"), - } - - if $prepare_cache { - with-env $TEST_ENV { gm update-cache } - } - - with-env $TEST_ENV $code - - rm --recursive --force --verbose $TEST_ENV_BASE -} +use common/main.nu [run-with-env] export def error-with-empty-store [] { run-with-env { From 602e1e5f2573478ca6d4d2629e970dfeb1d1a974 Mon Sep 17 00:00:00 2001 From: amtoine Date: Wed, 29 Nov 2023 19:33:44 +0100 Subject: [PATCH 7/8] add test --- tests/sugar/mod.nu | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/sugar/mod.nu b/tests/sugar/mod.nu index 971e8535..d6e89a1f 100644 --- a/tests/sugar/mod.nu +++ b/tests/sugar/mod.nu @@ -1,6 +1,9 @@ use std assert +use ../../src/nu-git-manager-sugar extra ["gm for-each"] + use ../common/import.nu ["assert imports"] +use ../common/main.nu [run-with-env] const MODULE = "nu-git-manager-sugar" @@ -45,3 +48,13 @@ export use imports def report [] { exit 1 } + +export def for-each [] { + use ../../src/nu-git-manager ["gm clone", "gm list", "gm status"] + + run-with-env --prepare-cache { + gm clone https://github.com/amtoine/nu-git-manager --depth 1 + + assert equal (gm for-each { pwd }) (gm list) + } +} From 1b576a5c72d48a9f93048018cd467fa2b5d832ac Mon Sep 17 00:00:00 2001 From: amtoine Date: Thu, 30 Nov 2023 19:09:25 +0100 Subject: [PATCH 8/8] format --- tests/sugar/mod.nu | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/sugar/mod.nu b/tests/sugar/mod.nu index d6e89a1f..07ec6666 100644 --- a/tests/sugar/mod.nu +++ b/tests/sugar/mod.nu @@ -50,11 +50,11 @@ def report [] { } export def for-each [] { - use ../../src/nu-git-manager ["gm clone", "gm list", "gm status"] + use ../../src/nu-git-manager ["gm clone", "gm list", "gm status"] - run-with-env --prepare-cache { + run-with-env --prepare-cache { gm clone https://github.com/amtoine/nu-git-manager --depth 1 assert equal (gm for-each { pwd }) (gm list) - } + } }