lib / remote_branch_delete.tclon commit Merge branch 'maint' (71a9db5)
   1# git-gui remote branch deleting support
   2# Copyright (C) 2007 Shawn Pearce
   3
   4class remote_branch_delete {
   5
   6field w
   7field head_m
   8
   9field urltype   {url}
  10field remote    {}
  11field url       {}
  12
  13field checktype  {head}
  14field check_head {}
  15
  16field status    {}
  17field idle_id   {}
  18field full_list {}
  19field head_list {}
  20field active_ls {}
  21field head_cache
  22field full_cache
  23field cached
  24
  25constructor dialog {} {
  26        global all_remotes M1B
  27
  28        make_toplevel top w
  29        wm title $top "[appname] ([reponame]): Delete Remote Branch"
  30        if {$top ne {.}} {
  31                wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
  32        }
  33
  34        label $w.header -text {Delete Remote Branch} -font font_uibold
  35        pack $w.header -side top -fill x
  36
  37        frame $w.buttons
  38        button $w.buttons.delete -text Delete \
  39                -default active \
  40                -command [cb _delete]
  41        pack $w.buttons.delete -side right
  42        button $w.buttons.cancel -text {Cancel} \
  43                -command [list destroy $w]
  44        pack $w.buttons.cancel -side right -padx 5
  45        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
  46
  47        labelframe $w.dest -text {From Repository}
  48        if {$all_remotes ne {}} {
  49                radiobutton $w.dest.remote_r \
  50                        -text {Remote:} \
  51                        -value remote \
  52                        -variable @urltype
  53                eval tk_optionMenu $w.dest.remote_m @remote $all_remotes
  54                grid $w.dest.remote_r $w.dest.remote_m -sticky w
  55                if {[lsearch -sorted -exact $all_remotes origin] != -1} {
  56                        set remote origin
  57                } else {
  58                        set remote [lindex $all_remotes 0]
  59                }
  60                set urltype remote
  61                trace add variable @remote write [cb _write_remote]
  62        } else {
  63                set urltype url
  64        }
  65        radiobutton $w.dest.url_r \
  66                -text {Arbitrary URL:} \
  67                -value url \
  68                -variable @urltype
  69        entry $w.dest.url_t \
  70                -borderwidth 1 \
  71                -relief sunken \
  72                -width 50 \
  73                -textvariable @url \
  74                -validate key \
  75                -validatecommand {
  76                        if {%d == 1 && [regexp {\s} %S]} {return 0}
  77                        return 1
  78                }
  79        trace add variable @url write [cb _write_url]
  80        grid $w.dest.url_r $w.dest.url_t -sticky we -padx {0 5}
  81        grid columnconfigure $w.dest 1 -weight 1
  82        pack $w.dest -anchor nw -fill x -pady 5 -padx 5
  83
  84        labelframe $w.heads -text {Branches}
  85        listbox $w.heads.l \
  86                -height 10 \
  87                -width 70 \
  88                -listvariable @head_list \
  89                -selectmode extended \
  90                -yscrollcommand [list $w.heads.sby set]
  91        scrollbar $w.heads.sby -command [list $w.heads.l yview]
  92
  93        frame $w.heads.footer
  94        label $w.heads.footer.status \
  95                -textvariable @status \
  96                -anchor w \
  97                -justify left
  98        button $w.heads.footer.rescan \
  99                -text {Rescan} \
 100                -command [cb _rescan]
 101        pack $w.heads.footer.status -side left -fill x -expand 1
 102        pack $w.heads.footer.rescan -side right
 103
 104        pack $w.heads.footer -side bottom -fill x -expand 1
 105        pack $w.heads.sby -side right -fill y
 106        pack $w.heads.l -side left -fill both -expand 1
 107        pack $w.heads -fill both -expand 1 -pady 5 -padx 5
 108
 109        labelframe $w.validate -text {Delete Only If}
 110        radiobutton $w.validate.head_r \
 111                -text {Merged Into:} \
 112                -value head \
 113                -variable @checktype
 114        set head_m [tk_optionMenu $w.validate.head_m @check_head {}]
 115        trace add variable @head_list write [cb _write_head_list]
 116        trace add variable @check_head write [cb _write_check_head]
 117        grid $w.validate.head_r $w.validate.head_m -sticky w
 118        radiobutton $w.validate.always_r \
 119                -text {Always (Do not perform merge checks)} \
 120                -value always \
 121                -variable @checktype
 122        grid $w.validate.always_r -columnspan 2 -sticky w
 123        grid columnconfigure $w.validate 1 -weight 1
 124        pack $w.validate -anchor nw -fill x -pady 5 -padx 5
 125
 126        trace add variable @urltype write [cb _write_urltype]
 127        _rescan $this
 128
 129        bind $w <Key-F5>     [cb _rescan]
 130        bind $w <$M1B-Key-r> [cb _rescan]
 131        bind $w <$M1B-Key-R> [cb _rescan]
 132        bind $w <Key-Return> [cb _delete]
 133        bind $w <Key-Escape> [list destroy $w]
 134        bind $w.header <Destroy> [list delete_this $this]
 135        return $w
 136}
 137
 138method _delete {} {
 139        switch $urltype {
 140        remote {set uri $remote}
 141        url    {set uri $url}
 142        }
 143
 144        set cache $urltype:$uri
 145        set crev {}
 146        if {$checktype eq {head}} {
 147                if {$check_head eq {}} {
 148                        tk_messageBox \
 149                                -icon error \
 150                                -type ok \
 151                                -title [wm title $w] \
 152                                -parent $w \
 153                                -message "A branch is required for 'Merged Into'."
 154                        return
 155                }
 156                set crev $full_cache("$cache\nrefs/heads/$check_head")
 157        }
 158
 159        set not_merged [list]
 160        set need_fetch 0
 161        set have_selection 0
 162        set push_cmd [list git push]
 163        lappend push_cmd -v
 164        lappend push_cmd $uri
 165
 166        foreach i [$w.heads.l curselection] {
 167                set ref [lindex $full_list $i]
 168                if {$crev ne {}} {
 169                        set obj $full_cache("$cache\n$ref")
 170                        if {[catch {set m [git merge-base $obj $crev]}]} {
 171                                set need_fetch 1
 172                                set m {}
 173                        }
 174                        if {$obj ne $m} {
 175                                lappend not_merged [lindex $head_list $i]
 176                                continue
 177                        }
 178                }
 179
 180                lappend push_cmd :$ref
 181                set have_selection 1
 182        }
 183
 184        if {$not_merged ne {}} {
 185                set msg "The following branches are not completely merged into $check_head:
 186
 187 - [join $not_merged "\n - "]"
 188
 189                if {$need_fetch} {
 190                        append msg "
 191
 192One or more of the merge tests failed because you have not fetched the necessary commits.  Try fetching from $uri first."
 193                }
 194
 195                tk_messageBox \
 196                        -icon info \
 197                        -type ok \
 198                        -title [wm title $w] \
 199                        -parent $w \
 200                        -message $msg
 201                if {!$have_selection} return
 202        }
 203
 204        if {!$have_selection} {
 205                tk_messageBox \
 206                        -icon error \
 207                        -type ok \
 208                        -title [wm title $w] \
 209                        -parent $w \
 210                        -message "Please select one or more branches to delete."
 211                return
 212        }
 213
 214        if {[tk_messageBox \
 215                -icon warning \
 216                -type yesno \
 217                -title [wm title $w] \
 218                -parent $w \
 219                -message {Recovering deleted branches is difficult.
 220
 221Delete the selected branches?}] ne yes} {
 222                return
 223        }
 224
 225        destroy $w
 226
 227        set cons [console::new \
 228                "push $uri" \
 229                "Deleting branches from $uri"]
 230        console::exec $cons $push_cmd
 231}
 232
 233method _rescan {{force 1}} {
 234        switch $urltype {
 235        remote {set uri $remote}
 236        url    {set uri $url}
 237        }
 238
 239        if {$force} {
 240                unset -nocomplain cached($urltype:$uri)
 241        }
 242
 243        if {$idle_id ne {}} {
 244                after cancel $idle_id
 245                set idle_id {}
 246        }
 247
 248        _load $this $urltype:$uri $uri
 249}
 250
 251method _write_remote     {args} { set urltype remote }
 252method _write_url        {args} { set urltype url    }
 253method _write_check_head {args} { set checktype head }
 254
 255method _write_head_list {args} {
 256        $head_m delete 0 end
 257        foreach abr $head_list {
 258                $head_m insert end radiobutton \
 259                        -label $abr \
 260                        -value $abr \
 261                        -variable @check_head
 262        }
 263        if {[lsearch -exact -sorted $head_list $check_head] < 0} {
 264                set check_head {}
 265        }
 266}
 267
 268method _write_urltype {args} {
 269        if {$urltype eq {url}} {
 270                if {$idle_id ne {}} {
 271                        after cancel $idle_id
 272                }
 273                _load $this none: {}
 274                set idle_id [after 1000 [cb _rescan 0]]
 275        } else {
 276                _rescan $this 0
 277        }
 278}
 279
 280method _load {cache uri} {
 281        if {$active_ls ne {}} {
 282                catch {close $active_ls}
 283        }
 284
 285        if {$uri eq {}} {
 286                $w.heads.l conf -state disabled
 287                set head_list [list]
 288                set full_list [list]
 289                set status {No repository selected.}
 290                return
 291        }
 292
 293        if {[catch {set x $cached($cache)}]} {
 294                set status "Scanning $uri..."
 295                $w.heads.l conf -state disabled
 296                set head_list [list]
 297                set full_list [list]
 298                set head_cache($cache) [list]
 299                set full_cache($cache) [list]
 300                set active_ls [open "| [list git ls-remote $uri]" r]
 301                fconfigure $active_ls \
 302                        -blocking 0 \
 303                        -translation lf \
 304                        -encoding utf-8
 305                fileevent $active_ls readable [cb _read $cache $active_ls]
 306        } else {
 307                set status {}
 308                set full_list $full_cache($cache)
 309                set head_list $head_cache($cache)
 310                $w.heads.l conf -state normal
 311        }
 312}
 313
 314method _read {cache fd} {
 315        if {$fd ne $active_ls} {
 316                catch {close $fd}
 317                return
 318        }
 319
 320        while {[gets $fd line] >= 0} {
 321                if {[string match {*^{}} $line]} continue
 322                if {[regexp {^([0-9a-f]{40})    (.*)$} $line _junk obj ref]} {
 323                        if {[regsub ^refs/heads/ $ref {} abr]} {
 324                                lappend head_list $abr
 325                                lappend head_cache($cache) $abr
 326                                lappend full_list $ref
 327                                lappend full_cache($cache) $ref
 328                                set full_cache("$cache\n$ref") $obj
 329                        }
 330                }
 331        }
 332
 333        if {[eof $fd]} {
 334                if {[catch {close $fd} err]} {
 335                        set status $err
 336                        set head_list [list]
 337                        set full_list [list]
 338                } else {
 339                        set status {}
 340                        set cached($cache) 1
 341                        $w.heads.l conf -state normal
 342                }
 343        }
 344} ifdeleted {
 345        catch {close $fd}
 346}
 347
 348}