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