Skip to content

Commit

Permalink
gb -d without arguments cleans branches (nushell#777)
Browse files Browse the repository at this point in the history
- `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 <[email protected]>
  • Loading branch information
fj0r and nashvi committed Mar 7, 2024
1 parent 0fd7668 commit 43880c9
Showing 1 changed file with 60 additions and 39 deletions.
99 changes: 60 additions & 39 deletions modules/git/git-v2.nu
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,56 @@ export def gl [

# git branch
export def gb [
branch?: string@"nu-complete git branches"
remote?: string@"nu-complete git remote branches"
branch?: string@"nu-complete git branches"
--remote (-r)='origin': string@"nu-complete git remotes"
--delete (-d)
--no-merged (-n)
] {
let bs = git branch | lines | each {|x| $x | str substring 2..}
if ($branch | is-empty) {
let no_merged = git branch --no-merged | lines | str trim
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 | 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)
remote: (git branch --remote)
Expand All @@ -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 c = if ($n.c | is-empty) { null } else { true }
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 }
{ current: $c, remote: $rm, branch: $n.b, ref: $r, no-merged: $nm }
{ current: $c, remote: $rm, branch: $n.b, ref: $r, merged: $m }
}
}
| 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 {
git checkout $branch
} else {
Expand Down Expand Up @@ -157,15 +190,15 @@ export def gig [--empty-dir] {

# git pull, push and switch
export def gp [
branch?: string@"nu-complete git remote branches"
remote?: string@"nu-complete git remote branches"
--force (-f) # git push -f
branch?: string@"nu-complete git branches"
--remote (-r)='origin': string@"nu-complete git remotes"
--force (-f) # git push -f
--override
--submodule (-s) # git submodule
--init (-i) # git init
--merge (-m) # git pull (no)--rebase
--autostash (-a) # git pull --autostash
--back-to-prev (-b) # back to branch
--submodule (-s) # git submodule
--init (-i) # git init
--merge (-m) # git pull (no)--rebase
--autostash (-a) # git pull --autostash
--back-to-prev (-b) # back to branch
] {
if $submodule {
git submodule update
Expand All @@ -179,11 +212,10 @@ export def gp [
let a = if $autostash {[--autostash]} else {[]}
let branch = if ($branch | is-empty) { (_git_status).branch } else { $branch }
let branch_repr = $'(ansi yellow)($branch)(ansi light_gray)'
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
let lbs = git branch | lines | each {|x| $x | str substring 2..}
let rbs = remote_braches | each {|x| $x.1}
let lbs = git branch | lines | each { $in | str substring 2..}
let rbs = (remote_branches)
let prev = (_git_status).branch
if $branch in $rbs {
if $"($remote)/($branch)" in $rbs {
if $branch in $lbs {
let bmsg = $'both local and remote have ($branch_repr) branch'
if $force {
Expand Down Expand Up @@ -635,22 +667,11 @@ def "nu-complete git branches" [] {
| each {|x| $"($x|str trim)"}
}

export def remote_braches [] {
export def remote_branches [] {
git branch -r
| lines
| str trim
| 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" [] {
Expand Down

0 comments on commit 43880c9

Please sign in to comment.