mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 07:07:46 +00:00
gb -d
without arguments cleans branches (#777)
- `gb -d` cleans branches that meet the criteria, if no arguments - not current - merged - `gb -d --no-merged` includes no merged branches - `gb` (no arguments) output `merged` instead of `no-merged` - changed `$remote` of `gb` and `gp` to flag - `remote_branches` return 'remote/branch' Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
parent
0fd766871f
commit
43880c91ac
1 changed files with 60 additions and 39 deletions
|
@ -70,14 +70,56 @@ export def gl [
|
||||||
|
|
||||||
# git branch
|
# git branch
|
||||||
export def gb [
|
export def gb [
|
||||||
branch?: string@"nu-complete git branches"
|
branch?: string@"nu-complete git branches"
|
||||||
remote?: string@"nu-complete git remote branches"
|
--remote (-r)='origin': string@"nu-complete git remotes"
|
||||||
--delete (-d)
|
--delete (-d)
|
||||||
--no-merged (-n)
|
--no-merged (-n)
|
||||||
] {
|
] {
|
||||||
let bs = git branch | lines | each {|x| $x | str substring 2..}
|
let bs = git branch | lines | each {|x| $x | str substring 2..}
|
||||||
if ($branch | is-empty) {
|
if $delete {
|
||||||
let no_merged = git branch --no-merged | lines | str trim
|
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 | filter { $"($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 will be delete!') {
|
||||||
|
git branch -D $branch
|
||||||
|
}
|
||||||
|
if $"($remote)/($branch)" in $remote_branches and (agree 'delete remote branch?!') {
|
||||||
|
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*(?P<b>[^\s]+)' | get 0.b }
|
||||||
{
|
{
|
||||||
local: (git branch)
|
local: (git branch)
|
||||||
remote: (git branch --remote)
|
remote: (git branch --remote)
|
||||||
|
@ -89,21 +131,12 @@ export def gb [
|
||||||
let n = $n | parse -r '\s*(?P<c>\*)?\s*(?P<b>[^\s]+)( -> )?(?P<r>[^\s]+)?' | get 0
|
let n = $n | parse -r '\s*(?P<c>\*)?\s*(?P<b>[^\s]+)( -> )?(?P<r>[^\s]+)?' | get 0
|
||||||
let c = if ($n.c | is-empty) { null } else { true }
|
let c = if ($n.c | is-empty) { null } else { true }
|
||||||
let r = if ($n.r | is-empty) { null } else { $n.r }
|
let r = if ($n.r | is-empty) { null } else { $n.r }
|
||||||
let nm = if $n.b in $no_merged { true } else { null }
|
let m = if $n.b in $merged { true } else { null }
|
||||||
let rm = if $x.k == 'remote' { true } else { null }
|
let rm = if $x.k == 'remote' { true } else { null }
|
||||||
{ current: $c, remote: $rm, branch: $n.b, ref: $r, no-merged: $nm }
|
{ current: $c, remote: $rm, branch: $n.b, ref: $r, merged: $m }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
| flatten
|
| flatten
|
||||||
} else if $delete {
|
|
||||||
if $branch in $bs and (agree 'branch will be delete!') {
|
|
||||||
git branch -D $branch
|
|
||||||
}
|
|
||||||
if $branch in (remote_braches | each {|x| $x.1}) and (agree 'delete remote branch?!') {
|
|
||||||
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
|
||||||
git branch -D -r $'($remote)/($branch)'
|
|
||||||
git push $remote -d $branch
|
|
||||||
}
|
|
||||||
} else if $branch in $bs {
|
} else if $branch in $bs {
|
||||||
git checkout $branch
|
git checkout $branch
|
||||||
} else {
|
} else {
|
||||||
|
@ -157,15 +190,15 @@ export def gig [--empty-dir] {
|
||||||
|
|
||||||
# git pull, push and switch
|
# git pull, push and switch
|
||||||
export def gp [
|
export def gp [
|
||||||
branch?: string@"nu-complete git remote branches"
|
branch?: string@"nu-complete git branches"
|
||||||
remote?: string@"nu-complete git remote branches"
|
--remote (-r)='origin': string@"nu-complete git remotes"
|
||||||
--force (-f) # git push -f
|
--force (-f) # git push -f
|
||||||
--override
|
--override
|
||||||
--submodule (-s) # git submodule
|
--submodule (-s) # git submodule
|
||||||
--init (-i) # git init
|
--init (-i) # git init
|
||||||
--merge (-m) # git pull (no)--rebase
|
--merge (-m) # git pull (no)--rebase
|
||||||
--autostash (-a) # git pull --autostash
|
--autostash (-a) # git pull --autostash
|
||||||
--back-to-prev (-b) # back to branch
|
--back-to-prev (-b) # back to branch
|
||||||
] {
|
] {
|
||||||
if $submodule {
|
if $submodule {
|
||||||
git submodule update
|
git submodule update
|
||||||
|
@ -179,11 +212,10 @@ export def gp [
|
||||||
let a = if $autostash {[--autostash]} else {[]}
|
let a = if $autostash {[--autostash]} else {[]}
|
||||||
let branch = if ($branch | is-empty) { (_git_status).branch } else { $branch }
|
let branch = if ($branch | is-empty) { (_git_status).branch } else { $branch }
|
||||||
let branch_repr = $'(ansi yellow)($branch)(ansi light_gray)'
|
let branch_repr = $'(ansi yellow)($branch)(ansi light_gray)'
|
||||||
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
let lbs = git branch | lines | each { $in | str substring 2..}
|
||||||
let lbs = git branch | lines | each {|x| $x | str substring 2..}
|
let rbs = (remote_branches)
|
||||||
let rbs = remote_braches | each {|x| $x.1}
|
|
||||||
let prev = (_git_status).branch
|
let prev = (_git_status).branch
|
||||||
if $branch in $rbs {
|
if $"($remote)/($branch)" in $rbs {
|
||||||
if $branch in $lbs {
|
if $branch in $lbs {
|
||||||
let bmsg = $'both local and remote have ($branch_repr) branch'
|
let bmsg = $'both local and remote have ($branch_repr) branch'
|
||||||
if $force {
|
if $force {
|
||||||
|
@ -635,22 +667,11 @@ def "nu-complete git branches" [] {
|
||||||
| each {|x| $"($x|str trim)"}
|
| each {|x| $"($x|str trim)"}
|
||||||
}
|
}
|
||||||
|
|
||||||
export def remote_braches [] {
|
export def remote_branches [] {
|
||||||
git branch -r
|
git branch -r
|
||||||
| lines
|
| lines
|
||||||
| str trim
|
| str trim
|
||||||
| filter {|x| not ($x | str starts-with 'origin/HEAD') }
|
| filter {|x| not ($x | str starts-with 'origin/HEAD') }
|
||||||
| each {|x| $x | split row '/'}
|
|
||||||
}
|
|
||||||
|
|
||||||
def "nu-complete git remote branches" [context: string, offset: int] {
|
|
||||||
let ctx = $context | argx parse
|
|
||||||
let rb = (remote_braches)
|
|
||||||
if ($ctx._args | length) < 3 {
|
|
||||||
$rb | each {|x| {value: $x.1, description: $x.0} }
|
|
||||||
} else {
|
|
||||||
$rb | filter {|x| $x.1 == $ctx.1 } | each {|x| $x.0}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def "nu-complete git remotes" [] {
|
def "nu-complete git remotes" [] {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue