Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gm for-each to sugar extra #116

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/nu-git-manager-sugar/extra.nu
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,31 @@ export def "gm report" []: nothing -> table<name: string, branch: string, remote
) == 0
}
}

# run a piece of code on all the repositories of the local store
#
# depending on the code given to `gm for-each`, the command might return any
# kind of data, e.g. nothing or a table.
#
# the name of each repo will be passed as the first and only argument.
#
# # Examples
# get the status of all the repos: empty list
# > gm for-each { git status }
#
# get the number of files tracked by each repo: list<int>
# > gm for-each { git lf | lines | length }
#
# get the number of status lines of each repo: table<repo: string, status: int>
# > 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

gm list | each {|repo|
cd ($root | path join $repo)
do $code $repo
}
}
16 changes: 16 additions & 0 deletions tests/common/main.nu
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 1 addition & 17 deletions tests/gm.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion tests/sugar/mod.nu
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -10,7 +13,7 @@ module imports {
}

export def extra [] {
assert imports $MODULE "extra" [ "gm report" ]
assert imports $MODULE "extra" [ "gm for-each", "gm report" ]
}

export def git [] {
Expand Down Expand Up @@ -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)
}
}
Loading