@matheusbsbs / matheus-bagliano / commits / 61f82ce79a
Fork 0

git-gui: Allow users to rename branches through 'branch -m'

Git's native command line interface has had branch renaming support for quite a while, through the -m/-M options to the git-branch command line tool. This is an extremely useful feature as users may decide that the name of their current branch is not an adequate description, or was just entered incorrectly when it was created. Even though most people would consider git-branch to be a Porcelain tool I'm using it here in git-gui as it is the only code that implements the rather complex set of logic needed to successfully rename a branch in Git. Currently that is along the lines of: *) Backup the ref *) Backup the reflog *) Delete the old ref *) Create the new ref *) Move the backed up reflog to the new ref *) Record the rename event in the reflog *) If the current branch was renamed, update HEAD *) If HEAD changed, record the rename event in the HEAD reflog *) Rename the [branch "$name"] section in the config file Since that is some rather ugly set of functionality to implement and get right, and some of it isn't easily accessible through the raw plumbing layer I'm just cheating by relying on the Porcelain. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed May 28, 2007 at 12:52 UTC 61f82ce79abe965c466bb7d3d1d89ae631fbbd24
2 files changed +151
git-gui.sh
+14
@@ -200,6 +200,15 @@ proc is_config_true {name} {
200 }
201 }
202
203 +proc get_config {name} {
204 + global repo_config
205 + if {[catch {set v $repo_config($name)}]} {
206 + return {}
207 + } else {
208 + return $v
209 + }
210 +}
211 +
212 proc load_config {include_global} {
213 global repo_config global_config default_config
214
@@ -1420,6 +1429,11 @@ if {[is_enabled branch]} {
1429 lappend disable_on_lock [list .mbar.branch entryconf \
1430 [.mbar.branch index last] -state]
1431
1432 + .mbar.branch add command -label {Rename...} \
1433 + -command branch_rename::dialog
1434 + lappend disable_on_lock [list .mbar.branch entryconf \
1435 + [.mbar.branch index last] -state]
1436 +
1437 .mbar.branch add command -label {Delete...} \
1438 -command do_delete_branch
1439 lappend disable_on_lock [list .mbar.branch entryconf \
lib/branch_rename.tcl new
+137
@@ -0,0 +1,137 @@
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 all_heads current_branch
12 +
13 + make_toplevel top w
14 + wm title $top "[appname] ([reponame]): Rename Branch"
15 + if {$top ne {.}} {
16 + wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
17 + }
18 +
19 + set oldname $current_branch
20 + set newname [get_config gui.newbranchtemplate]
21 +
22 + label $w.header -text {Rename Branch} -font font_uibold
23 + pack $w.header -side top -fill x
24 +
25 + frame $w.buttons
26 + button $w.buttons.rename -text Rename \
27 + -default active \
28 + -command [cb _rename]
29 + pack $w.buttons.rename -side right
30 + button $w.buttons.cancel -text {Cancel} \
31 + -command [list destroy $w]
32 + pack $w.buttons.cancel -side right -padx 5
33 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
34 +
35 + frame $w.rename
36 + label $w.rename.oldname_l -text {Branch:}
37 + eval tk_optionMenu $w.rename.oldname_m @oldname $all_heads
38 +
39 + label $w.rename.newname_l -text {New Name:}
40 + entry $w.rename.newname_t \
41 + -borderwidth 1 \
42 + -relief sunken \
43 + -width 40 \
44 + -textvariable @newname \
45 + -validate key \
46 + -validatecommand {
47 + if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
48 + return 1
49 + }
50 +
51 + grid $w.rename.oldname_l $w.rename.oldname_m -sticky w -padx {0 5}
52 + grid $w.rename.newname_l $w.rename.newname_t -sticky we -padx {0 5}
53 + grid columnconfigure $w.rename 1 -weight 1
54 + pack $w.rename -anchor nw -fill x -pady 5 -padx 5
55 +
56 + bind $w <Key-Return> [cb _rename]
57 + bind $w <Key-Escape> [list destroy $w]
58 + bind $w <Visibility> "
59 + grab $w
60 + $w.rename.newname_t icursor end
61 + focus $w.rename.newname_t
62 + "
63 + bind $w.header <Destroy> [list delete_this $this]
64 + tkwait window $w
65 +}
66 +
67 +method _rename {} {
68 + global all_heads current_branch
69 +
70 + if {$oldname eq {}} {
71 + tk_messageBox \
72 + -icon error \
73 + -type ok \
74 + -title [wm title $w] \
75 + -parent $w \
76 + -message "Please select a branch to rename."
77 + focus $w.rename.oldname_m
78 + return
79 + }
80 + if {$newname eq {}
81 + || $newname eq [get_config gui.newbranchtemplate]} {
82 + tk_messageBox \
83 + -icon error \
84 + -type ok \
85 + -title [wm title $w] \
86 + -parent $w \
87 + -message "Please supply a branch name."
88 + focus $w.rename.newname_t
89 + return
90 + }
91 + if {![catch {git show-ref --verify -- "refs/heads/$newname"}]} {
92 + tk_messageBox \
93 + -icon error \
94 + -type ok \
95 + -title [wm title $w] \
96 + -parent $w \
97 + -message "Branch '$newname' already exists."
98 + focus $w.rename.newname_t
99 + return
100 + }
101 + if {[catch {git check-ref-format "heads/$newname"}]} {
102 + tk_messageBox \
103 + -icon error \
104 + -type ok \
105 + -title [wm title $w] \
106 + -parent $w \
107 + -message "We do not like '$newname' as a branch name."
108 + focus $w.rename.newname_t
109 + return
110 + }
111 +
112 + if {[catch {git branch -m $oldname $newname} err]} {
113 + tk_messageBox \
114 + -icon error \
115 + -type ok \
116 + -title [wm title $w] \
117 + -parent $w \
118 + -message "Failed to rename '$oldname'.\n\n$err"
119 + return
120 + }
121 +
122 + set oldidx [lsearch -exact -sorted $all_heads $oldname]
123 + if {$oldidx >= 0} {
124 + set all_heads [lreplace $all_heads $oldidx $oldidx]
125 + }
126 + lappend all_heads $newname
127 + set all_heads [lsort $all_heads]
128 + populate_branch_menu
129 +
130 + if {$current_branch eq $oldname} {
131 + set current_branch $newname
132 + }
133 +
134 + destroy $w
135 +}
136 +
137 +}