feat: initial commit

This commit is contained in:
johannes committed 2026-07-26 11:55:16 +02:00
commit 94a6a1d744
454 files changed
+52433

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
export def agree [
prompt
--default-not (-n)
] {
let prompt = if ($prompt | str ends-with '!') {
$'(ansi red)($prompt)(ansi reset)'
} else {
$'($prompt)'
}
(if $default_not { [no yes] } else { [yes no] } | input list $prompt) == 'yes'
}
export def tips [msg] {
print -e $"(ansi light_gray)($msg)(ansi reset)"
}
+35
View File
@@ -0,0 +1,35 @@
export def cmpl-git-log [] {
git log -n 32 --pretty=%h»¦«%s
| lines
| split column "»¦«" value description
#| each { $"($in.value) # ($in.description)"}
| { completions: $in, options: { sort: false } }
}
export def cmpl-git-log-all [] {
git log --all -n 32 --pretty=%h»¦«%d»¦«%s
| lines
| split column "»¦«" value branch description
| each {|x| $x | update description $"($x.branch) ($x.description)" }
| { completions: $in, options: { sort: false } }
}
export def cmpl-git-branch-files [context: string, offset:int] {
let token = $context | split row ' '
let branch = $token | get 1
let files = $token | skip 2
git ls-tree -r --name-only $branch
| lines
| where {|x| not ($x in $files)}
}
export def cmpl-git-branches [] {
git branch
| lines
| where {|x| not ($x | str starts-with '*')}
| each {|x| $"($x|str trim)"}
}
export def cmpl-git-remotes [] {
^git remote | lines | each { |line| $line | str trim }
}
+544
View File
@@ -0,0 +1,544 @@
use common.nu *
use complete.nu *
use stat.nu *
def sum_prefix [p] {
$in | items {|k,v| if ($k | str starts-with $p) { $v } else { 0 } } | math sum
}
# git stash
export def git-stash [
--apply (-a)
--clear (-c)
--drop (-d)
--list (-l)
--pop (-p)
--show (-s)
--all (-A)
--include-untracked (-i)
] {
if $apply {
git stash apply
} else if $clear {
git stash clear
} else if $drop {
git stash drop
} else if $list {
git stash list
} else if $pop {
git stash pop
} else if $show {
git stash show --text
} else if $all {
git stash --all ...(if $include_untracked {[--include-untracked]} else {[]})
} else {
let s = _git_status
if ($s | sum_prefix 'wt_') > 0 {
git stash
} else {
git stash pop
}
}
}
# git branch
export def git-branch [
branch?: string@cmpl-git-branches
--remote (-r)='origin': string@cmpl-git-remotes
--delete (-d)
--no-merged (-n)
] {
let bs = git branch | lines | each {|x| $x | str substring 2..}
if $delete {
let remote_branches = remote_branches
if ($branch | is-empty) {
let dels = if $no_merged { gb } else {
gb
| where { $in.merged | is-not-empty }
}
| where { ($in.remote | is-empty) and ($in.current | is-empty) }
| each {|x|
let pf = if ($x.current | is-empty) { " " } else { $"(ansi cyan)* " }
let nm = if ($x.merged | is-not-empty ) { $"(ansi green)☑ " } else { " " }
$x | insert display $"($nm)($pf)(ansi reset)($x.branch)"
}
if ($dels | is-empty) {
tips "no branches to delete"
return
}
let $dels = $dels
| input list -d display --multi
| get branch
for b in $dels {
tips $"delete (ansi yellow)($b)"
git branch -D $b
}
if ($dels | is-not-empty) and (agree 'delete remote branch?!') {
for b in ($dels | where { $"($remote)/($in)" in $remote_branches }) {
tips $"delete (ansi yellow)($remote)/($b)"
git branch -D -r $'($remote)/($b)'
git push $remote -d $b
}
}
} else {
if $branch in $bs and (agree $'branch `($branch)` will be delete!') {
git branch -D $branch
}
if $"($remote)/($branch)" in $remote_branches and (agree $'remote branch `($branch)` will be delete!') {
git branch -D -r $'($remote)/($branch)'
git push $remote -d $branch
}
}
} else if ($branch | is-empty) {
let merged = git branch --merged
| lines
| each { $in | parse -r '\s*\*?\s*(?<b>[^\s]+)' | get 0.b }
{
local: (git branch)
remote: (git branch --remote)
}
| transpose k v
| each {|x|
$x.v | lines
| each {|n|
let n = $n | parse -r '\s*(?<c>\*)?\s*(?<b>[^\s]+)( -> )?(?<r>[^\s]+)?' | get 0
let c = if ($n.c | is-empty) { null } else { true }
let r = if ($n.r | is-empty) { null } else { $n.r }
let m = if $n.b in $merged { true } else { null }
let rm = if $x.k == 'remote' { true } else { null }
{ current: $c, remote: $rm, branch: $n.b, ref: $r, merged: $m }
}
}
| flatten
} else if $branch in $bs {
git checkout $branch
} else {
if (agree 'create new branch?') {
git checkout -b $branch
}
}
}
# git clone, init
export def --env git-new [
repo?: string@cmpl-git-branches
local?: path
--submodule (-s) # git submodule
--init (-i) # git init
] {
if $init {
if ($repo | is-empty) {
git init --initial-branch main
} else {
git init $repo --initial-branch main
cd $repo
}
if $submodule {
git submodule init
}
} else {
let local = if ($local | is-empty) {
$repo | path basename | split row '.' | get 0
} else {
$local
}
git clone ...(if $submodule {[--recurse-submodules]} else {[]}) $repo $local
cd $local
}
}
# edit .gitignore
export def git-ignore [--empty-dir] {
if $empty_dir {
[
'# Ignore everything in this directory'
'*'
'# Except this file'
'!.gitignore'
] | str join (char newline) | save .gitignore
} else {
^$env.EDITOR ([(git rev-parse --show-toplevel) .gitignore] | path join)
}
}
# git pull, push and switch
export def git-pull-push [
branch?: string@cmpl-git-branches # branch
--remote (-r)='origin': string@cmpl-git-remotes # remote
--rebase
--force (-f) # git push -f
--empty: string
--submodule (-s) # git submodule
--init (-i) # git init
--autostash (-a) # git pull --autostash
--back-to-prev (-b) # back to branch
] {
if $submodule {
git submodule update
} else if $rebase {
git pull --rebase
} else if ($empty | is-not-empty) {
git pull --rebase
git add --all
git commit --allow-empty -m $"🫙($empty)"
git push
} else {
# git fetch --prune
let m = if $rebase { [--rebase] } else { [] }
let a = if $autostash {[--autostash]} else {[]}
let prev = (_git_status).branch
let branch = if ($branch | is-empty) { $prev } else { $branch }
let branch_repr = $'(ansi yellow)($branch)(ansi light_gray)'
let lbs = git branch | lines | each { $in | str substring 2..}
let rbs = remote_branches
if $"($remote)/($branch)" in $rbs {
if $branch in $lbs {
let bmsg = $'both local and remote have ($branch_repr) branch'
if $force {
tips $'($bmsg), with `--force`, push'
git branch -u $'($remote)/($branch)' $branch
git push --force
} else {
tips $'($bmsg), pull'
if $prev != $branch {
tips $'switch to ($branch_repr)'
git checkout $branch
}
git pull ...$m ...$a
}
} else {
tips $"local doesn't have ($branch_repr) branch, fetch"
git fetch $remote $"($branch):($branch)"
git checkout $branch
git branch -u $'($remote)/($branch)' $branch
git pull ...$m ...$a -v
}
} else {
let bmsg = $"remote doesn't have ($branch_repr) branch"
let force = if $force {[--force]} else {[]}
if $branch in $lbs {
tips $'($bmsg), set upstream and push'
git checkout $branch
} else {
tips $'($bmsg), create and push'
git checkout -b $branch
}
git push ...$force --set-upstream $remote $branch
}
if $back_to_prev {
git checkout $prev
}
let s = _git_status
if $s.ahead > 0 {
tips 'remote is behind, push'
git push
}
}
}
# git add and restore
export def git-add [
...file: path
--all (-A)
--patch (-p)
--update (-u)
--verbose (-v)
--delete (-d) # git rm
--cached (-c)
--force (-f)
--restore (-r) # git restore
--staged (-s)
--source (-o): string
] {
mut args = []
if $restore {
if ($source | is-not-empty) { $args ++= [--source $source] }
if $staged { $args ++= [--staged]}
$args ++= if ($file | is-empty) { [.] } else { $file }
git restore ...$args
} else {
if $all { $args ++= [--all] }
if $patch { $args ++= [--patch] }
if $update { $args ++= [--update] }
if $verbose { $args ++= [--verbose] }
if $force { $args ++= [--force] }
$args ++= if ($file | is-empty) { [.] } else { $file }
git add ...$args
}
}
# git delete
export def git-delete [
...file: path
--force (-f)
--cached (-c)
--history (-h)
] {
mut args = []
if $history {
let f = $file | str join " "
(git filter-branch --force --index-filter
$'git rm --cached --ignore-unmatch ($f)'
--prune-empty --tag-name-filter cat
-- --all)
rm -rf ([(git rev-parse --show-toplevel) .git/refs/original/] | path join)
ggc
} else {
if $cached { $args ++= [--cached] }
if $force { $args ++= [--force] }
git rm ...$args -r ...$file
}
}
def cmpl-commit-type [] {
$env.GIT_COMMIT_TYPE | columns
}
# git commit
export def git-commit [
...message: string
--type (-t): string@cmpl-commit-type
--all (-A)
--amend (-a)
--keep (-k)
] {
mut args = []
if ($message | is-not-empty) {
let message = $message | str join ' '
let message = if ($type | is-empty) {
$message
} else {
$env.GIT_COMMIT_TYPE | get $type | str replace '{}' $message
}
$args ++= [-m $message]
} else {
if ($type | is-not-empty) {
let message = $env.GIT_COMMIT_TYPE | get $type | str replace '{}' ''
$args ++= [-m $message -e]
}
}
if $all { $args ++= [--all] }
if $amend { $args ++= [--amend] }
if $keep { $args ++= [--no-edit] }
git commit -v ...$args
}
# git diff
export def git-diff [
commit?: string@cmpl-git-log-all
commit2?: string@cmpl-git-log-all
--cached (-c) # cached
--unstashed (-u) # unstashed
--word-diff (-w) # word-diff
--staged (-s) # staged
] {
mut args = []
if $word_diff { $args ++= [--word-diff] }
if $cached { $args ++= [--cached] }
if $staged { $args ++= [--staged] }
let s = _git_status
if ($commit | is-empty) {
if ($s | sum_prefix 'wt_') > 0 {
git diff ...$args
} else if ($s | sum_prefix 'idx_') > 0 {
git diff ...$args --staged
}
} else {
if ($commit2 | is-empty) {
git diff ...$args $s.branch $commit
} else {
git diff ...$args $commit $commit2
}
}
}
# git merge
export def git-merge [
branch?: string@cmpl-git-branches
--abort (-a)
--continue (-c)
--quit (-q)
--squash (-s)
--fast-farward (-f)
--remote (-r)='origin': string@cmpl-git-remotes
] {
mut args = []
if $squash { $args ++= [--squash] }
if $fast_farward { $args ++= [--ff] } else { $args ++= [--no-ff] }
if ($branch | is-empty) {
git merge ...$args $"($remote)/(git_main_branch)"
} else {
git merge ...$args $branch
}
if $squash {
git commit -v
}
}
# git rebase
# TODO: --onto: (commit_id)
export def git-rebase [
branch?: string@cmpl-git-branches
--interactive (-i)
--onto (-o): string
--abort (-a)
--continue (-c)
--skip (-s)
--quit (-q)
] {
if $abort {
git rebase --abort
} else if $continue {
git rebase --continue
} else if $skip {
git rebase --skip
} else if $quit {
git rebase --quit
} else if ($onto | is-not-empty) {
git rebase --onto $branch
} else {
let i = if $interactive {[--interactive]} else {[]}
if ($branch | is-empty) {
git rebase ...$i (git_main_branch)
} else {
git rebase ...$i $branch
}
}
}
# git cherry-pick
export def git-cherry-pick [
commit?: string@cmpl-git-log-all
--abort (-a)
--continue (-c)
--skip (-s)
--quit (-q)
--no-commit (-n)
] {
mut args = []
if $abort {
$args ++= [--abort]
} else if $continue {
$args ++= [--continue]
} else if $skip {
$args ++= [--skip]
} else if $quit {
$args ++= [--quit]
} else {
if $no_commit {
$args ++= [--no-commit]
}
$args ++= [$commit]
}
git cherry-pick ...$args
}
# copy file from other branch
export def git-copy-file [
branch: string@cmpl-git-branches
...file: string@cmpl-git-branch-files
] {
^git checkout $branch $file
}
# git reset
export def git-reset [
commit?: string@cmpl-git-log
--hard (-h)
--soft (-s)
--clean (-c)
] {
mut args = []
if $hard { $args ++= [--hard] }
if $soft { $args ++= [--soft] }
if ($commit | is-not-empty) { $args ++= [$commit] }
git reset ...$args
if $clean {
git clean -fd
}
}
# git remote
export def git-remote [
remote?: string@cmpl-git-remotes
uri?: string
--add (-a)
--rename (-r)
--delete (-d)
--update (-u)
--set (-s)
] {
if ($remote | is-empty) {
git remote -v
} else if $add {
git remote add $remote $uri
} else if $set {
git remote set-url $remote $uri
} else if $rename {
let old = $remote
let new = $uri
git remote rename $old $new
} else if $delete {
git remote remove $remote
} else if $update {
git remote update $remote
} else {
git remote show $remote
}
}
# git bisect
export def git-bisect [
--bad (-b)
--good (-g)
--reset (-r)
--start (-s)
] {
if $good {
git bisect good
} else if $bad {
git bisect bad
} else if $reset {
git bisect reset
} else if $start {
git bisect start
} else {
git bisect
}
}
export def git-garbage-collect [] {
git reflog expire --all --expire=now
git gc --aggressive --prune=now
}
export def git-truncate-history [
retain:int=10
--message:string="Truncate history"
] {
let h = git log --pretty=%H --reverse -n $retain | lines | first
let s = _git_status
git checkout -f --orphan temp $h
git add .
git commit -m $message
git rebase --onto temp $h $s.branch
}
export def git-squash-last [
num:int
] {
let l = git log --pretty=»»¦««%s»¦«%b -n $num
| split row '»»¦««' | slice 1..
| split column '»¦«' message body
| each { $"($in.message)\n\n($in.body)" }
| str join "\n===\n"
git reset --soft $"HEAD~($num)"
git commit --edit -m $l
}
+20
View File
@@ -0,0 +1,20 @@
export def Q [...t --sep:string=''] {
let s = $t | str join $sep | str replace -a "'" "''"
$"'($s)'"
}
export def sqlx [s] {
open $env.GIT_FLOW_STATE | query db $s
}
export def --env init-db [env_name:string, file:string, hook: closure] {
let begin = date now
if $env_name not-in $env {
{$env_name: $file} | load-env
}
if ($file | path exists) { return }
{_: '.'} | into sqlite -t _ $file
open $file | query db "DROP TABLE _;"
do $hook {|s| open $file | query db $s } {|...t| Q ...$t }
print $"(ansi grey)created database: $env.($env_name), takes ((date now) - $begin)(ansi reset)"
}
+256
View File
@@ -0,0 +1,256 @@
use utils.nu *
use core.nu *
use complete.nu *
use stat.nu *
export-env {
$env.GIT_FLOW = {
branches: {
dev: dev
main: main
release: release
hotfix: hotfix
feature: feature
}
separator: '/'
}
}
def git-kind-select [kind] {
git branch
| lines
| where { $in | str starts-with '*' | not $in }
| each {|x| $"($x|str trim)"}
| where {|x|
let branches = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
$x | str starts-with $"($branches | get $kind)($sep)"
}
}
def cmpl-git-features [] {
git-kind-select feature
}
export def git-kind-branches [kind] {
let branches = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
let curr = (_git_status).branch
mut obj = $curr
if not ($obj | str starts-with $"($branches | get $kind)($sep)") {
let r = git-kind-select $kind
$obj = if ($r | is-empty) { $r } else { $r | input list $"There are no ($kind) currently, pick?" }
}
{
curr: $curr
$kind: $obj
dev: $branches.dev
main: $branches.main
}
}
export def gitflow-open-feature [
name: string
] {
let b = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
git checkout -b $"($b.feature)($sep)($name)" $b.dev
}
export def gitflow-close-feature [
--fast-farward (-f)
] {
let b = git-kind-branches feature
if ($b.feature | is-empty) {
print $"(ansi grey)There are no feature branches.(ansi reset)"
return
}
git checkout $b.dev
let f = if $fast_farward {[--ff]} else {[--no-ff]}
git merge ...$f $b.feature
let remote = git remote show
git push -u $remote $b.dev
git branch -d $b.feature
git checkout $b.dev
}
export def gitflow-resolve-feature [
] {
git checkout $env.GIT_FLOW.branches.dev
git add .
git commit
git push
}
export def gitflow-release [
tag?: string
--action(-a): closure
] {
let b = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
let remote = git remote show
git checkout $b.dev
git push -u $remote $b.dev
let rb = if ($tag | is-empty) {
$b.dev
} else {
let rb = $"($b.release)($sep)($tag)"
git checkout -b $rb $b.dev
if ($action | is-not-empty) {
do $action $tag
}
do -i { git commit -a -m $"Bumped version number to ($tag)" }
$rb
}
git checkout $b.main
let f = if ($tag | is-empty) {[--ff]} else {[--no-ff]}
git merge ...$f $rb
git push -u $remote $b.main
if ($tag | is-not-empty) {
git tag -a $tag
git push $remote tag $tag
do -i { git branch -d $rb }
}
git checkout $b.dev
}
export def gitflow-open-hotfix [
tag: string
] {
let b = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
let rb = $"($b.hotfix)($sep)($tag)"
git checkout -b $rb $b.main
# ... bump
git commit -a -m $"Bumped version number to ($tag)"
}
export def gitflow-close-hotfix [
message?: string
] {
let b = git-kind-branches hotfix
if ($b.hotfix | is-empty) {
print $"(ansi grey)There are no hotfix branches.(ansi reset)"
return
}
git checkout $b.hotfix
do -i { git commit -m $"Fixed: ($message)" }
let remote = git remote show
let f = if ($message | is-empty) {[--ff]} else {[--no-ff]}
git checkout $b.main
git merge ...$f $b.hotfix
git push -u $remote $b.main
if ($message | is-not-empty) {
let sep = $env.GIT_FLOW.separator
let t = $b.hotfix | split row $sep | slice 1.. | str join $sep
git tag -a $t
git push $remote tag $t
}
git checkout $b.dev
git merge ...$f $b.hotfix
git branch -d $b.hotfix
}
export def gitlab-open-feature [
name: string
--local(-l)
] {
let b = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
let remote = git remote show
let f = $"($b.feature)($sep)($name)"
git checkout -b $f $b.main
if not $local {
git push -u $remote $f
}
}
export def gitlab-close-feature [
--fast-farward (-f)
--local(-l)
] {
let b = git-kind-branches feature
if ($b.feature | is-empty) {
print $"(ansi grey)There are no feature branches.(ansi reset)"
return
}
let remote = git remote show
git checkout $b.main
if $local {
let f = if $fast_farward {[--ff]} else {[--no-ff]}
git merge ...$f $b.feature
git push
} else {
git pull
}
git branch -D $b.feature
let rb = $'($remote)/($b.feature)'
if $rb in (remote_branches) {
git branch -D -r $rb
git push $remote -d $b.feature
}
}
export def gitlab-resolve-feature [
] {
git checkout $env.GIT_FLOW.branches.main
git add .
git commit
git push
}
export def gitlab-release [
tag?: string
--from(-f): string
--to(-t): string = 'pre-production'
--action(-a): closure
] {
let b = $env.GIT_FLOW.branches
let sep = $env.GIT_FLOW.separator
let remote = git remote show
let $from = if ($from | is-empty) { $b.main } else { $from }
git checkout $from
git push -u $remote $from
let rb = if ($tag | is-empty) {
$from
} else {
let rb = $"($b.release)($sep)($tag)"
git checkout -b $rb $from
if ($action | is-not-empty) {
do $action $tag
}
do -i { git commit -a -m $"Bumped version number to ($tag)" }
$rb
}
git checkout $to
let f = if ($tag | is-empty) {[--ff]} else {[--no-ff]}
git merge ...$f $rb
git push -u $remote $rb
if ($tag | is-not-empty) {
git tag -a $tag
git push $remote tag $tag
do -i { git branch -d $rb }
}
git checkout $b.main
}
+76
View File
@@ -0,0 +1,76 @@
def 'precent' [] {
($in * 10000 | math round) / 100
}
def 'frequency' [-w: int] {
let o = $in
'' | fill -c '*' -w ($o * $w | math round)
}
def 'histogram-column' [column --len(-l):int = 50] {
let o = $in
let total = $o | get $column | math sum
let max = $o | get $column | math max | ($in / $total)
$o
| each {|x|
let c = $x | get $column | $in / $total
$x
| insert precent $"($c | precent)%"
| insert frequency ($c / $max | frequency -w $len)
}
}
def git_log [] {
git log --pretty=%h»¦«%aN»¦«%s»¦«%aD
| lines
| split column "»¦«" sha1 committer desc merged_at
}
export def git-histogram-merger [] {
git_log
| group-by committer
| items {|k, v| {committer: $k, count: ($v | length)} }
| histogram-column count
}
def cmpl-author [] {
git_log | group-by committer | columns
}
def cmpl-interval [] {
[hour day month year]
}
export def git-histogram-activities [
author:string@cmpl-author
--interval(-i): string@cmpl-interval
--builtin-histogram
] {
let dfs = match $interval {
'hour' => '%Y-%m-%d %H'
'day' => '%Y-%m-%d'
'month' => '%Y-%m'
'year' => '%Y'
_ => '%Y-%m-%d'
}
let d = git_log
| group-by committer
| get $author
| get merged_at
| into datetime
| format date $dfs
if $builtin_histogram {
$d
| histogram
| rename date
| sort-by date
} else {
$d
| wrap date
| sort-by date
| group-by date
| items {|k, v| {date: $k, count: ($v | length)} }
| histogram-column count
}
}
+20
View File
@@ -0,0 +1,20 @@
export use stat.nu *
export use utils.nu *
export use core.nu *
export use histogram.nu *
export use git-flow.nu *
export-env {
$env.GIT_COMMIT_TYPE = {
feat: 'feat: {}'
fix: 'fix: {}'
docs: 'docs: {}'
style: 'style: {}'
refactor: 'refactor: {}'
perf: 'perf: {}'
test: 'test: {}'
chore: 'chore: {}'
}
export use git-flow.nu
}
+40
View File
@@ -0,0 +1,40 @@
export alias gl = git-log
export alias gst = git-stash
export alias gb = git-branch
export alias gn = git-new
export alias gig = git-ignore
export alias gp = git-pull-push
export alias ga = git-add
export alias gdel = git-delete
export alias gc = git-commit
export alias gd = git-diff
export alias gm = git-merge
export alias gr = git-rebase
export alias gcp = git-cherry-pick
export alias gcpf = git-copy-file
export alias grs = git-reset
export alias grm = git-remote
export alias gbs = git-bisect
export alias ggc = git-garbage-collect
export alias ghm = git-histogram-merger
export alias gha = git-histogram-activities
export alias gcl = git config --list
export alias gsw = git switch
export alias gswc = git switch -c
export alias gts = git tag -s
export alias gfof = gitflow-open-feature
export alias gfcf = gitflow-close-feature
export alias gfrf = gitflow-resolve-feature
export alias gfrl = gitflow-release
export alias gfoh = gitflow-open-hotfix
export alias gfch = gitflow-close-hotfix
export alias gof = gitlab-open-feature
export alias gcf = gitlab-close-feature
export alias grf = gitlab-resolve-feature
export alias grl = gitlab-release
export alias goh = gitflow-open-hotfix
export alias gch = gitflow-close-hotfix
+226
View File
@@ -0,0 +1,226 @@
use complete.nu *
# git status
export def gs [] {
git status
}
export def _git_status [] {
# TODO: show-stash
let raw_status = do -i { git --no-optional-locks status --porcelain=2 --branch | lines }
let stashes = do -i { git stash list | lines | length }
mut status = {
idx_added_staged : 0
idx_modified_staged : 0
idx_deleted_staged : 0
idx_renamed : 0
idx_type_changed : 0
wt_untracked : 0
wt_modified : 0
wt_deleted : 0
wt_type_changed : 0
wt_renamed : 0
ignored : 0
conflicts : 0
ahead : 0
behind : 0
stashes : $stashes
repo_name : no_repository
tag : no_tag
branch : no_branch
remote : ''
}
if ($raw_status | is-empty) { return $status }
for s in $raw_status {
let r = $s | split row ' '
match $r.0 {
'#' => {
match ($r.1 | str substring 7..) {
'oid' => {
$status.commit_hash = ($r.2 | str substring 0..<8)
}
'head' => {
$status.branch = $r.2
}
'upstream' => {
$status.remote = $r.2
}
'ab' => {
$status.ahead = ($r.2 | into int)
$status.behind = ($r.3 | into int | math abs)
}
}
}
'1'|'2' => {
match ($r.1 | str substring 0..<1) {
'A' => {
$status.idx_added_staged += 1
}
'M' => {
$status.idx_modified_staged += 1
}
'R' => {
$status.idx_renamed += 1
}
'D' => {
$status.idx_deleted_staged += 1
}
'T' => {
$status.idx_type_changed += 1
}
}
match ($r.1 | str substring 1..<2) {
'M' => {
$status.wt_modified += 1
}
'R' => {
$status.wt_renamed += 1
}
'D' => {
$status.wt_deleted += 1
}
'T' => {
$status.wt_type_changed += 1
}
}
}
'?' => {
$status.wt_untracked += 1
}
'u' => {
$status.conflicts += 1
}
}
}
$status
}
def git-parse-stat [o: list<string>] {
mut r = { file:0, ins:0, del:0, change:[] }
for i in $o {
if ($i | is-empty) { continue }
if ($i | find -r '[0-9]+ file.+change' | is-not-empty) {
for j in ($i
| split row ','
| each {|x| $x | str trim | parse -r "(?<num>[0-9]+) (?<col>.+)" | first }
) {
let col = if ($j.col | str starts-with 'file') {
'file'
} else {
$j.col | str substring ..<3
}
let num = $j.num | into int
$r = $r | upsert $col $num
}
} else {
$r.change ++= [ ($i | split row '|' | first | str trim) ]
}
}
$r
}
export def _git_log [
--num(-n):int = 9
--reverse(-r)
--verbose(-v)
] {
let s = $"»(random chars -l 4)«"
let S = $"»(random chars -l 6)«"
let p = $"($S)($s)%h($s)%s($s)%aN($s)%aE($s)%aD($s)%D($s)%b"
mut a = [-n $num $"--pretty=($p)"]
if $reverse { $a ++= [--reverse] }
if $verbose { $a ++= [--stat] }
git log ...$a
| split row $S | slice 1..
| append $s
| reduce -f {c: { payload: [] }, r: [] } {|it, acc|
if ($it | str starts-with $s) {
let c = if $verbose {
$acc.c | merge (git-parse-stat $acc.c.payload)
} else {
$acc.c
}
| reject payload
$acc
| upsert r ($acc.r | append $c)
| update c (
$it
| split column $s _ sha message author email date refs body
| first
| reject _
| insert payload []
)
} else {
$acc | update c.payload {|x| $x.c.payload | append $it }
}
}
| get r | slice 1..
| each {|x|
let refs = if ($x.refs | is-empty) {
$x.refs
} else {
$x.refs | split row ", "
}
$x
| update date { $x.date | into datetime }
| update refs $refs
}
}
# git log
export def git-log [
commit?: string@cmpl-git-log
--markdown(-m)
--verbose(-v)
--reverse(-r)
--num(-n):int=32
] {
if ($commit|is-empty) {
let r = _git_log --reverse=(not $reverse) --verbose=$verbose -n $num
if $markdown {
mut m = []
for i in $r {
if ($i.refs | is-not-empty) {
let t = $i.refs
| where {|x| $x | str starts-with 'tag: ' }
| each {|x| $x | str substring 5.. }
for j in $t {
$m ++= [$"## ($j)"]
}
}
$m ++= [$"###### ($i.message)\n"]
$m ++= [$"> ($i.date | format date '%y-%m-%d/%w/%H:%M:%S') ($i.sha)\n"]
if ($i.body | str trim | is-not-empty) {
$m ++= [$"($i.body)"]
}
}
$m | str join "\n"
} else {
$r | update body {|x| $x.body | str trim}
}
} else {
git log --stat -p -n 1 $commit
}
}
export def remote_branches [] {
git branch -r
| lines
| str trim
| where {|x| not ($x | str starts-with 'origin/HEAD') }
}
export def git_main_branch [] {
git remote show origin
| lines
| str trim
| find --regex 'HEAD .*?[:: ].+'
| first
| str replace --regex 'HEAD .*?[:: ](.+)' '$1'
}
+102
View File
@@ -0,0 +1,102 @@
use stat.nu *
export def git-init [remote] {
git init --initial-branch main
git add .
git commit -m 'init'
git remote add origin $remote
}
export def git-is-repo [] {
(git rev-parse --is-inside-work-tree | complete).exit_code == 0
}
export def git-commit-changes [commit:string] {
git diff-tree --no-commit-id --name-only -r $commit | lines
}
export def git-last-changes [] {
let d = git log -n 9 --pretty=%h»¦«%s | lines | split column '»¦«' hash message
for i in $d {
let r = git-commit-changes $i.hash
if ($r | is-not-empty) {
return $r
}
}
[]
}
export def git-changes [] {
do -i { git --no-optional-locks status --porcelain=1 | lines }
}
export def git-last-commit [] {
let d = git log -n 9 --pretty=»»¦««%h»¦«%s»¦«%b
| split row '»»¦««' | slice 1..
| split column '»¦«' hash message body
for i in $d {
if (git-commit-changes $i.hash | is-not-empty) {
return $i
}
}
}
export def git-cdup [] {
let r = git rev-parse --show-cdup | complete
if $r.exit_code == 0 {
$r.stdout | str trim
}
}
export def git-repo-path [] {
let c = git-cdup
if ($c | describe) == nothing { return }
[$env.PWD (git-cdup)] | path join | path expand
}
export def git-sync [
src dest
--no-file
--push
--init: string
--post-sync: closure
--init-post-sync: closure
--trans-msg: closure
] {
let src = $src | path expand
cd $src
let l = git-last-commit
let msg = if ($trans_msg | is-empty) {
$"($l.message)\n\n($l.body)"
} else {
do $trans_msg $l
}
let dest = $dest | path expand
if not ($dest | path exists) { mkdir $dest }
cd $dest
if not $no_file {
rsync -a --delete --exclude='.git' $'($src)/' $dest
if ($post_sync | is-not-empty) and (git-is-repo) {
do $post_sync $src $dest $l
}
if ($init_post_sync | is-not-empty) {
do $init_post_sync $src $dest $l
}
}
if ($init | is-not-empty) and not (git-is-repo) {
git init .
git remote add origin $init
git add .
git commit -m $msg
if $push {
git push --set-upstream origin main
}
}
if (git-changes | is-not-empty) {
git add .
git commit -m $msg
if $push {
git push
}
}
}