From 8aec003f07ef7bd778a229fd9c4f96888a45ddb3 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Tue, 31 Oct 2023 17:03:52 +0000 Subject: [PATCH] add `--depth: int` to `gm clone` (#49) --- nu-git-manager/mod.nu | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/nu-git-manager/mod.nu b/nu-git-manager/mod.nu index 00e0de1e..08a57ad4 100644 --- a/nu-git-manager/mod.nu +++ b/nu-git-manager/mod.nu @@ -57,6 +57,9 @@ export def "gm" []: nothing -> nothing { # # setup a public repo in the local store and use HTTP to fetch without PAT and push with SSH # > gm clone https://github.com/amtoine/nu-git-manager --fetch https --push ssh +# +# clone a big repo as a single commit, avoiding all intermediate Git deltas +# > gm clone https://github.com/neovim/neovim --depth 1 export def "gm clone" [ url: string # the URL to the repository to clone, supports HTTPS and SSH links, as well as references ending in `.git` or starting with `git@` --remote: string = "origin" # the name of the remote to setup @@ -64,6 +67,7 @@ export def "gm clone" [ --fetch: string@"nu-complete git-protocols" # setup the FETCH protocol explicitely, will overwrite `--ssh` for FETCH --push: string@"nu-complete git-protocols" # setup the PUSH protocol explicitely, will overwrite `--ssh` for PUSH --bare # clone the repository as a "bare" project + --depth: int # the depth at which to clone the repository ]: nothing -> nothing { let repository = $url | parse-git-url @@ -86,12 +90,33 @@ export def "gm clone" [ let urls = get-fetch-push-urls $repository $fetch $push $ssh - if $bare { - ^git clone $urls.fetch $local_path --origin $remote --bare + mut args = [$urls.fetch $local_path --origin $remote] + if $depth != null { + if ($depth < 1) { + let span = metadata $depth | get span + error make { + msg: $"(ansi red_bold)invalid_clone_depth(ansi reset)" + label: { + text: $"clone depth should be strictly positive, found ($depth)" + start: $span.start + end: $span.end + } + } + } + + $args = ($args ++ --depth ++ $depth) + + if $bare { + $args = ($args ++ --bare) + } } else { - ^git clone $urls.fetch $local_path --origin $remote + if $bare { + $args = ($args ++ --bare) + } } + ^git clone $args + ^git -C $local_path remote set-url $remote $urls.fetch ^git -C $local_path remote set-url $remote --push $urls.push