Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 1 | # git-gui branch delete support |
| 2 | # Copyright (C) 2007 Shawn Pearce |
| 3 | |
| 4 | class branch_delete { |
| 5 | |
| 6 | field w ; # widget path |
| 7 | field w_heads ; # listbox of local head names |
| 8 | field w_check ; # revision picker for merge test |
| 9 | field w_delete ; # delete button |
| 10 | |
| 11 | constructor dialog {} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 12 | global current_branch use_ttk NS |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 13 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 14 | make_dialog top w |
| 15 | wm withdraw $w |
Vasco Almeida | a3d97af | 2016-05-08 10:52:57 +0000 | [diff] [blame] | 16 | wm title $top [mc "%s (%s): Delete Branch" [appname] [reponame]] |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 17 | if {$top ne {.}} { |
| 18 | wm geometry $top "+[winfo rootx .]+[winfo rooty .]" |
| 19 | } |
| 20 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 21 | ${NS}::label $w.header -text [mc "Delete Local Branch"] \ |
| 22 | -font font_uibold -anchor center |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 23 | pack $w.header -side top -fill x |
| 24 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 25 | ${NS}::frame $w.buttons |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 26 | set w_delete $w.buttons.delete |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 27 | ${NS}::button $w_delete \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 28 | -text [mc Delete] \ |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 29 | -default active \ |
| 30 | -state disabled \ |
| 31 | -command [cb _delete] |
| 32 | pack $w_delete -side right |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 33 | ${NS}::button $w.buttons.cancel \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 34 | -text [mc Cancel] \ |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 35 | -command [list destroy $w] |
| 36 | pack $w.buttons.cancel -side right -padx 5 |
| 37 | pack $w.buttons -side bottom -fill x -pady 10 -padx 10 |
| 38 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 39 | ${NS}::labelframe $w.list -text [mc "Local Branches"] |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 40 | set w_heads $w.list.l |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 41 | slistbox $w_heads \ |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 42 | -height 10 \ |
| 43 | -width 70 \ |
| 44 | -selectmode extended \ |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 45 | -exportselection false |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 46 | pack $w.list.l -side left -fill both -expand 1 |
| 47 | pack $w.list -fill both -expand 1 -pady 5 -padx 5 |
| 48 | |
| 49 | set w_check [choose_rev::new \ |
| 50 | $w.check \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 51 | [mc "Delete Only If Merged Into"] \ |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 52 | ] |
Sam Hocevar | 966d077 | 2009-03-24 00:42:24 +0100 | [diff] [blame] | 53 | $w_check none [mc "Always (Do not perform merge checks)"] |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 54 | pack $w.check -anchor nw -fill x -pady 5 -padx 5 |
| 55 | |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 56 | foreach h [load_all_heads] { |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 57 | if {$h ne $current_branch} { |
| 58 | $w_heads insert end $h |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bind $w_heads <<ListboxSelect>> [cb _select] |
| 63 | bind $w <Visibility> " |
| 64 | grab $w |
| 65 | focus $w |
| 66 | " |
| 67 | bind $w <Key-Escape> [list destroy $w] |
| 68 | bind $w <Key-Return> [cb _delete]\;break |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 69 | wm deiconify $w |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 70 | tkwait window $w |
| 71 | } |
| 72 | |
| 73 | method _select {} { |
| 74 | if {[$w_heads curselection] eq {}} { |
| 75 | $w_delete configure -state disabled |
| 76 | } else { |
| 77 | $w_delete configure -state normal |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | method _delete {} { |
Shawn O. Pearce | 7618e6b | 2007-07-04 16:38:13 -0400 | [diff] [blame] | 82 | if {[catch {set check_cmt [$w_check commit_or_die]}]} { |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 83 | return |
| 84 | } |
| 85 | |
| 86 | set to_delete [list] |
| 87 | set not_merged [list] |
| 88 | foreach i [$w_heads curselection] { |
| 89 | set b [$w_heads get $i] |
| 90 | if {[catch { |
| 91 | set o [git rev-parse --verify "refs/heads/$b"] |
| 92 | }]} continue |
| 93 | if {$check_cmt ne {}} { |
| 94 | if {[catch {set m [git merge-base $o $check_cmt]}]} continue |
| 95 | if {$o ne $m} { |
| 96 | lappend not_merged $b |
| 97 | continue |
| 98 | } |
| 99 | } |
| 100 | lappend to_delete [list $b $o] |
| 101 | } |
| 102 | if {$not_merged ne {}} { |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 103 | set msg "[mc "The following branches are not completely merged into %s:" [$w_check get]] |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 104 | |
| 105 | - [join $not_merged "\n - "]" |
| 106 | tk_messageBox \ |
| 107 | -icon info \ |
| 108 | -type ok \ |
| 109 | -title [wm title $w] \ |
| 110 | -parent $w \ |
| 111 | -message $msg |
| 112 | } |
| 113 | if {$to_delete eq {}} return |
| 114 | if {$check_cmt eq {}} { |
Sam Hocevar | 966d077 | 2009-03-24 00:42:24 +0100 | [diff] [blame] | 115 | set msg [mc "Recovering deleted branches is difficult.\n\nDelete the selected branches?"] |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 116 | if {[tk_messageBox \ |
| 117 | -icon warning \ |
| 118 | -type yesno \ |
| 119 | -title [wm title $w] \ |
| 120 | -parent $w \ |
| 121 | -message $msg] ne yes} { |
| 122 | return |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | set failed {} |
| 127 | foreach i $to_delete { |
| 128 | set b [lindex $i 0] |
| 129 | set o [lindex $i 1] |
Shawn O. Pearce | 76bb40c | 2008-05-08 20:29:42 -0400 | [diff] [blame] | 130 | if {[catch {git branch -D $b} err]} { |
Vasco Almeida | eca9636 | 2016-05-08 10:52:55 +0000 | [diff] [blame] | 131 | append failed [mc " - %s:" $b] " $err\n" |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | if {$failed ne {}} { |
| 136 | tk_messageBox \ |
| 137 | -icon error \ |
| 138 | -type ok \ |
| 139 | -title [wm title $w] \ |
| 140 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 141 | -message [mc "Failed to delete branches:\n%s" $failed] |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Shawn O. Pearce | 3206c63 | 2007-07-03 23:33:59 -0400 | [diff] [blame] | 144 | destroy $w |
| 145 | } |
| 146 | |
| 147 | } |