git-gui / lib / branch_rename.tclon commit Merge branch 'rs/fsck-obj-leakfix' (2d68161)
   1# git-gui branch rename support
   2# Copyright (C) 2007 Shawn Pearce
   3
   4class branch_rename {
   5
   6field w
   7field oldname
   8field newname
   9
  10constructor dialog {} {
  11        global current_branch use_ttk NS
  12
  13        make_dialog top w
  14        wm withdraw $w
  15        wm title $top [mc "%s (%s): Rename Branch" [appname] [reponame]]
  16        if {$top ne {.}} {
  17                wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
  18        }
  19
  20        set oldname $current_branch
  21        set newname [get_config gui.newbranchtemplate]
  22
  23        ${NS}::label $w.header -text [mc "Rename Branch"]\
  24                -font font_uibold -anchor center
  25        pack $w.header -side top -fill x
  26
  27        ${NS}::frame $w.buttons
  28        ${NS}::button $w.buttons.rename -text [mc Rename] \
  29                -default active \
  30                -command [cb _rename]
  31        pack $w.buttons.rename -side right
  32        ${NS}::button $w.buttons.cancel -text [mc Cancel] \
  33                -command [list destroy $w]
  34        pack $w.buttons.cancel -side right -padx 5
  35        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
  36
  37        ${NS}::frame $w.rename
  38        ${NS}::label $w.rename.oldname_l -text [mc "Branch:"]
  39        if {$use_ttk} {
  40                ttk::combobox $w.rename.oldname_m -textvariable @oldname \
  41                        -values [load_all_heads] -state readonly
  42        } else {
  43                eval tk_optionMenu $w.rename.oldname_m @oldname [load_all_heads]
  44        }
  45
  46        ${NS}::label $w.rename.newname_l -text [mc "New Name:"]
  47        ${NS}::entry $w.rename.newname_t \
  48                -width 40 \
  49                -textvariable @newname \
  50                -validate key \
  51                -validatecommand {
  52                        if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
  53                        return 1
  54                }
  55
  56        grid $w.rename.oldname_l $w.rename.oldname_m -sticky we -padx {0 5}
  57        grid $w.rename.newname_l $w.rename.newname_t -sticky we -padx {0 5}
  58        grid columnconfigure $w.rename 1 -weight 1
  59        pack $w.rename -anchor nw -fill x -pady 5 -padx 5
  60
  61        bind $w <Key-Return> [cb _rename]
  62        bind $w <Key-Escape> [list destroy $w]
  63        bind $w <Visibility> "
  64                grab $w
  65                $w.rename.newname_t icursor end
  66                focus $w.rename.newname_t
  67        "
  68        wm deiconify $w
  69        tkwait window $w
  70}
  71
  72method _rename {} {
  73        global current_branch
  74
  75        if {$oldname eq {}} {
  76                tk_messageBox \
  77                        -icon error \
  78                        -type ok \
  79                        -title [wm title $w] \
  80                        -parent $w \
  81                        -message [mc "Please select a branch to rename."]
  82                focus $w.rename.oldname_m
  83                return
  84        }
  85        if {$newname eq {}
  86                || $newname eq [get_config gui.newbranchtemplate]} {
  87                tk_messageBox \
  88                        -icon error \
  89                        -type ok \
  90                        -title [wm title $w] \
  91                        -parent $w \
  92                        -message [mc "Please supply a branch name."]
  93                focus $w.rename.newname_t
  94                return
  95        }
  96        if {![catch {git show-ref --verify -- "refs/heads/$newname"}]} {
  97                tk_messageBox \
  98                        -icon error \
  99                        -type ok \
 100                        -title [wm title $w] \
 101                        -parent $w \
 102                        -message [mc "Branch '%s' already exists." $newname]
 103                focus $w.rename.newname_t
 104                return
 105        }
 106        if {[catch {git check-ref-format "heads/$newname"}]} {
 107                tk_messageBox \
 108                        -icon error \
 109                        -type ok \
 110                        -title [wm title $w] \
 111                        -parent $w \
 112                        -message [mc "'%s' is not an acceptable branch name." $newname]
 113                focus $w.rename.newname_t
 114                return
 115        }
 116
 117        if {[catch {git branch -m $oldname $newname} err]} {
 118                tk_messageBox \
 119                        -icon error \
 120                        -type ok \
 121                        -title [wm title $w] \
 122                        -parent $w \
 123                        -message [strcat [mc "Failed to rename '%s'." $oldname] "\n\n$err"]
 124                return
 125        }
 126
 127        if {$current_branch eq $oldname} {
 128                set current_branch $newname
 129        }
 130
 131        destroy $w
 132}
 133
 134}