feat(functions): add -l/--local flag to mkrep #167

Merged
rootiest merged 2 commits from feat/mkrep-local-flag into main 2026-09-22 07:18:18 +00:00
4 changed files with 207 additions and 38 deletions
+18 -4
View File
@@ -1742,10 +1742,11 @@ hist
.nf
\f[C]
Synopsis: mkrep [--cd | --no-cd] [--mkdir | --no-mkdir] [--git | --no-git]
[-c | --clean | --no-clean] [--strict] [-v | --verbose]
[-s | --silent] [--template <path>] [--branch <name>]
[--remote <url>] [--new-remote [<cmd>]] [--server <type>]
[--check-existing] [-y | --yes] [--name <name>] [-h | --help] <dir>
[-c | --clean | --no-clean] [--strict] [-l | --local]
[-v | --verbose] [-s | --silent] [--template <path>]
[--branch <name>] [--remote <url>] [--new-remote [<cmd>]]
[--server <type>] [--check-existing] [-y | --yes]
[--name <name>] [-h | --help] <dir>
Creates a directory, cds into it, and git-inits it -- mkcd plus a git
repo in one step. All three actions are on by default and each has a
@@ -1758,6 +1759,15 @@ the directory already exists. The two compose: --clean runs first, so
--clean --strict together is not a contradiction -- strict sees an
empty slot because clean just emptied it.
-l/--local forces strictly local action: any flag or environment
variable that would link to, create, or check a remote (--remote,
--new-remote, --server, --check-existing, $GIT_SERVER, $GITEA_URL,
$GITEA_HOST, $GITLAB_URL, $GITLAB_HOST, $MKREP_REMOTE_CMD) is
overridden and ignored. Even when passed alongside conflicting remote
flags like --server or --remote, -l/--local takes precedence and
proceeds with local directory creation and git initialization without
erroring or contacting any forge.
--remote links an already-existing remote (git remote add origin
<url>) -- it does not create anything. Linking is idempotent: an origin
already pointing at that URL is reported and accepted, so rerunning
@@ -1820,6 +1830,9 @@ Arguments:
-c, --clean Remove <dir> first if it already exists
--no-clean Leave an existing <dir> alone (default)
--strict Fail if <dir> already exists (checked after --clean)
-l, --local Force local-only operation; overrides and ignores any
remote flags (--remote, --new-remote, --server,
--check-existing) and remote environment variables
-v, --verbose Print each step as it runs
-s, --silent Suppress all output; overrides --verbose
--template <path>
@@ -1846,6 +1859,7 @@ Exit Status:
Example:
mkrep \[ti]/projects/my-new-repo
mkrep --clean --strict \[ti]/projects/scratch
mkrep -l \[ti]/projects/my-local-repo
mkrep --remote git\[at]git.example.com:me/foo.git \[ti]/projects/foo
set -Ux MKREP_REMOTE_CMD \[aq]gh repo create {name} --private --source=. --remote=origin --push\[aq]
mkrep --new-remote \[ti]/projects/foo
+18 -4
View File
@@ -1330,10 +1330,11 @@ functions). They are active in all interactive sessions.
### mkrep
Synopsis: mkrep [--cd | --no-cd] [--mkdir | --no-mkdir] [--git | --no-git]
[-c | --clean | --no-clean] [--strict] [-v | --verbose]
[-s | --silent] [--template <path>] [--branch <name>]
[--remote <url>] [--new-remote [<cmd>]] [--server <type>]
[--check-existing] [-y | --yes] [--name <name>] [-h | --help] <dir>
[-c | --clean | --no-clean] [--strict] [-l | --local]
[-v | --verbose] [-s | --silent] [--template <path>]
[--branch <name>] [--remote <url>] [--new-remote [<cmd>]]
[--server <type>] [--check-existing] [-y | --yes]
[--name <name>] [-h | --help] <dir>
Creates a directory, cds into it, and git-inits it -- mkcd plus a git
repo in one step. All three actions are on by default and each has a
@@ -1346,6 +1347,15 @@ functions). They are active in all interactive sessions.
--clean --strict together is not a contradiction -- strict sees an
empty slot because clean just emptied it.
-l/--local forces strictly local action: any flag or environment
variable that would link to, create, or check a remote (--remote,
--new-remote, --server, --check-existing, $GIT_SERVER, $GITEA_URL,
$GITEA_HOST, $GITLAB_URL, $GITLAB_HOST, $MKREP_REMOTE_CMD) is
overridden and ignored. Even when passed alongside conflicting remote
flags like --server or --remote, -l/--local takes precedence and
proceeds with local directory creation and git initialization without
erroring or contacting any forge.
--remote links an already-existing remote (git remote add origin
<url>) -- it does not create anything. Linking is idempotent: an origin
already pointing at that URL is reported and accepted, so rerunning
@@ -1408,6 +1418,9 @@ functions). They are active in all interactive sessions.
-c, --clean Remove <dir> first if it already exists
--no-clean Leave an existing <dir> alone (default)
--strict Fail if <dir> already exists (checked after --clean)
-l, --local Force local-only operation; overrides and ignores any
remote flags (--remote, --new-remote, --server,
--check-existing) and remote environment variables
-v, --verbose Print each step as it runs
-s, --silent Suppress all output; overrides --verbose
--template <path>
@@ -1434,6 +1447,7 @@ functions). They are active in all interactive sessions.
Example:
mkrep ~/projects/my-new-repo
mkrep --clean --strict ~/projects/scratch
mkrep -l ~/projects/my-local-repo
mkrep --remote git@git.example.com:me/foo.git ~/projects/foo
set -Ux MKREP_REMOTE_CMD 'gh repo create {name} --private --source=. --remote=origin --push'
mkrep --new-remote ~/projects/foo
+55 -30
View File
@@ -14,10 +14,11 @@
#
# SYNOPSIS
# mkrep [--cd | --no-cd] [--mkdir | --no-mkdir] [--git | --no-git]
# [-c | --clean | --no-clean] [--strict] [-v | --verbose]
# [-s | --silent] [--template <path>] [--branch <name>]
# [--remote <url>] [--new-remote [<cmd>]] [--server <type>]
# [--check-existing] [-y | --yes] [--name <name>] [-h | --help] <dir>
# [-c | --clean | --no-clean] [--strict] [-l | --local]
# [-v | --verbose] [-s | --silent] [--template <path>]
# [--branch <name>] [--remote <url>] [--new-remote [<cmd>]]
# [--server <type>] [--check-existing] [-y | --yes]
# [--name <name>] [-h | --help] <dir>
#
# DESCRIPTION
# Creates a directory, cds into it, and git-inits it -- mkcd plus a git
@@ -31,6 +32,15 @@
# --clean --strict together is not a contradiction -- strict sees an
# empty slot because clean just emptied it.
#
# -l/--local forces strictly local action: any flag or environment
# variable that would link to, create, or check a remote (--remote,
# --new-remote, --server, --check-existing, $GIT_SERVER, $GITEA_URL,
# $GITEA_HOST, $GITLAB_URL, $GITLAB_HOST, $MKREP_REMOTE_CMD) is
# overridden and ignored. Even when passed alongside conflicting remote
# flags like --server or --remote, -l/--local takes precedence and
# proceeds with local directory creation and git initialization without
# erroring or contacting any forge.
#
# --remote links an already-existing remote (git remote add origin
# <url>) -- it does not create anything. Linking is idempotent: an origin
# already pointing at that URL is reported and accepted, so rerunning
@@ -93,6 +103,9 @@
# -c, --clean Remove <dir> first if it already exists
# --no-clean Leave an existing <dir> alone (default)
# --strict Fail if <dir> already exists (checked after --clean)
# -l, --local Force local-only operation; overrides and ignores any
# remote flags (--remote, --new-remote, --server,
# --check-existing) and remote environment variables
# -v, --verbose Print each step as it runs
# -s, --silent Suppress all output; overrides --verbose
# --template <path>
@@ -119,6 +132,7 @@
# EXAMPLE
# mkrep ~/projects/my-new-repo
# mkrep --clean --strict ~/projects/scratch
# mkrep -l ~/projects/my-local-repo
# mkrep --remote git@git.example.com:me/foo.git ~/projects/foo
# set -Ux MKREP_REMOTE_CMD 'gh repo create {name} --private --source=. --remote=origin --push'
# mkrep --new-remote ~/projects/foo
@@ -145,7 +159,7 @@ function mkrep --description 'Create a directory, cd into it, and git init it'
__fish_palette
argparse h/help cd no-cd mkdir no-mkdir git no-git c/clean no-clean strict \
v/verbose s/silent y/yes template= branch= remote= new-remote=? server= \
l/local v/verbose s/silent y/yes template= branch= remote= new-remote=? server= \
check-existing name= \
-- $argv
or return 1
@@ -162,6 +176,7 @@ function mkrep --description 'Create a directory, cd into it, and git init it'
echo " $c_flag-c$c_reset, $c_flag--clean$c_reset Remove <dir> first if it exists"
echo " $c_flag--no-clean$c_reset Leave an existing <dir> alone (default)"
echo " $c_flag--strict$c_reset Fail if <dir> already exists"
echo " $c_flag-l$c_reset, $c_flag--local$c_reset Force local only; ignore remote flags/env"
echo " $c_flag-v$c_reset, $c_flag--verbose$c_reset Print each step as it runs"
echo " $c_flag-s$c_reset, $c_flag--silent$c_reset Suppress all output (overrides -v)"
echo " $c_flag--template$c_reset $c_arg<path>$c_reset git init --template=<path>"
@@ -187,6 +202,14 @@ function mkrep --description 'Create a directory, cd into it, and git init it'
end
set -l dir $argv[1]
if set -q _flag_local
set -e _flag_remote
set -e _flag_new_remote
set -e _flag_server
set -e _flag_check_existing
set -e _flag_yes
end
if set -q _flag_remote; and set -q _flag_new_remote
echo "$c_err""✘$c_reset --remote and --new-remote are mutually exclusive" >&2
return 1
@@ -220,31 +243,33 @@ function mkrep --description 'Create a directory, cd into it, and git init it'
# explicit request to auto-create; an ambient $GIT_SERVER is not, and only
# the latter needs confirming before we create a repo on a live forge.
set -l srv_implicit 0
if set -q _flag_server
set srv_type $_flag_server
else if test -n "$GIT_SERVER"
set srv_type $GIT_SERVER
set srv_implicit 1
end
if test -n "$srv_type"
switch $srv_type
case gitea
if test -n "$GITEA_URL"
set srv_url $GITEA_URL
else if test -n "$GITEA_HOST"
set srv_url "https://$GITEA_HOST"
end
case gitlab
if test -n "$GITLAB_URL"
set srv_url $GITLAB_URL
else if test -n "$GITLAB_HOST"
set srv_url "https://$GITLAB_HOST"
end
case github
# gh defaults to github.com; no base url needed
case '*'
echo "$c_err""✘$c_reset Unrecognized server type $c_arg$srv_type$c_reset (expected gitea, gitlab, or github)" >&2
return 1
if not set -q _flag_local
if set -q _flag_server
set srv_type $_flag_server
else if test -n "$GIT_SERVER"
set srv_type $GIT_SERVER
set srv_implicit 1
end
if test -n "$srv_type"
switch $srv_type
case gitea
if test -n "$GITEA_URL"
set srv_url $GITEA_URL
else if test -n "$GITEA_HOST"
set srv_url "https://$GITEA_HOST"
end
case gitlab
if test -n "$GITLAB_URL"
set srv_url $GITLAB_URL
else if test -n "$GITLAB_HOST"
set srv_url "https://$GITLAB_HOST"
end
case github
# gh defaults to github.com; no base url needed
case '*'
echo "$c_err""✘$c_reset Unrecognized server type $c_arg$srv_type$c_reset (expected gitea, gitlab, or github)" >&2
return 1
end
end
end
+116
View File
@@ -491,6 +491,121 @@ begin
rm -rf $base
end
section "mkrep: -l/--local forces local-only operation"
begin
set -l base (_mkrep_sandbox)
set -l target $base/repo
mkrep -l $target >/dev/null
check "-l exits 0" 0 $status
check "-l creates directory" true (test -d $target; and echo true; or echo false)
check "-l inits git repo" true (test -d $target/.git; and echo true; or echo false)
check "-l creates no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
begin
set -l base (_mkrep_sandbox)
set -l target $base/repo
mkrep --local $target >/dev/null
check "--local exits 0" 0 $status
check "--local creates directory" true (test -d $target; and echo true; or echo false)
check "--local inits git repo" true (test -d $target/.git; and echo true; or echo false)
check "--local creates no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
section "mkrep: -l overrides --server"
begin
_mkrep_stub_tool tea 1
set -l base (_mkrep_sandbox)
set -l target $base/repo
set -lx PATH $stub_bin $PATH
set -lx GITEA_URL https://gitea.example.invalid
set -lx MKREP_REMOTE_CMD 'echo should-not-run >created.txt'
mkrep --server gitea -l $target >/dev/null
check "--server with -l exits 0" 0 $status
check "--server with -l did not run create command" false (test -f $target/created.txt; and echo true; or echo false)
check "--server with -l added no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
section "mkrep: -l overrides --remote"
begin
set -l base (_mkrep_sandbox)
set -l target $base/repo
mkrep --remote https://example.invalid/me/repo.git -l $target >/dev/null
check "--remote with -l exits 0" 0 $status
check "--remote with -l added no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
section "mkrep: -l overrides --new-remote"
begin
set -l base (_mkrep_sandbox)
set -l target $base/repo
mkrep --new-remote='echo should-not-run >created.txt' -l $target >/dev/null
check "--new-remote with -l exits 0" 0 $status
check "--new-remote with -l did not run template" false (test -f $target/created.txt; and echo true; or echo false)
check "--new-remote with -l added no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
section "mkrep: -l overrides --check-existing"
begin
set -l base (_mkrep_sandbox)
set -l target $base/repo
set -lx GIT_SERVER ''
set -lx GITEA_URL ''
mkrep --check-existing -l $target >/dev/null
check "--check-existing with -l exits 0" 0 $status
check "--check-existing with -l creates local repo" true (test -d $target/.git; and echo true; or echo false)
check "--check-existing with -l adds no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
section "mkrep: -l overrides ambient \$GIT_SERVER and \$GITEA_URL"
begin
_mkrep_stub_tool tea 1
set -l base (_mkrep_sandbox)
set -l target $base/repo
set -lx PATH $stub_bin $PATH
set -lx GIT_SERVER gitea
set -lx GITEA_URL https://gitea.example.invalid
set -lx MKREP_REMOTE_CMD 'echo should-not-run >created.txt'
mkrep -l $target >/dev/null 2>$base/err
check "\$GIT_SERVER with -l exits 0" 0 $status
check "\$GIT_SERVER with -l ran no command" false (test -e $target/created.txt; and echo true; or echo false)
check "\$GIT_SERVER with -l added no remote" 0 (count (git -C $target remote))
check "\$GIT_SERVER with -l printed no skip note" false (string match -q '*Skipped creating*' -- (cat $base/err); and echo true; or echo false)
cd $start
rm -rf $base
end
section "mkrep: -l overrides conflicting remote flags without error"
begin
set -l base (_mkrep_sandbox)
set -l target $base/repo
mkrep --server gitea --remote https://example.invalid/x.git -l $target >/dev/null
check "--server + --remote with -l exits 0" 0 $status
check "--server + --remote with -l created local repo" true (test -d $target/.git; and echo true; or echo false)
check "--server + --remote with -l added no remote" 0 (count (git -C $target remote))
cd $start
rm -rf $base
end
rm -rf $stub_bin
section "mkrep: --help"
@@ -499,6 +614,7 @@ set -l base (_mkrep_sandbox)
set -l out (mkrep --help)
check "--help exits 0" 0 $status
check "--help prints usage" true (string match -q -- '*Usage:*' $out; and echo true; or echo false)
check "--help mentions --local" true (string match -q -- '*--local*' $out; and echo true; or echo false)
check "--help creates nothing" false (test -d $base/repo; and echo true; or echo false)
rm -rf $base