git-gui: Cleanup bindings within merge dialog
[git-gui/harish2704-git-gui.git] / lib / merge.tcl
blob62434ff310f80c180a31ae3ba985c7db2c3182ba
1 # git-gui branch merge support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 class merge {
6 field w ; # top level window
7 field w_rev ; # mega-widget to pick the revision to merge
9 method _can_merge {} {
10 global HEAD commit_type file_states
12 if {[string match amend* $commit_type]} {
13 info_popup {Cannot merge while amending.
15 You must finish amending this commit before starting any type of merge.
17 return 0
20 if {[committer_ident] eq {}} {return 0}
21 if {![lock_index merge]} {return 0}
23 # -- Our in memory state should match the repository.
25 repository_state curType curHEAD curMERGE_HEAD
26 if {$commit_type ne $curType || $HEAD ne $curHEAD} {
27 info_popup {Last scanned state does not match repository state.
29 Another Git program has modified this repository since the last scan. A rescan must be performed before a merge can be performed.
31 The rescan will be automatically started now.
33 unlock_index
34 rescan ui_ready
35 return 0
38 foreach path [array names file_states] {
39 switch -glob -- [lindex $file_states($path) 0] {
40 _O {
41 continue; # and pray it works!
43 U? {
44 error_popup "You are in the middle of a conflicted merge.
46 File [short_path $path] has merge conflicts.
48 You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
50 unlock_index
51 return 0
53 ?? {
54 error_popup "You are in the middle of a change.
56 File [short_path $path] is modified.
58 You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
60 unlock_index
61 return 0
66 return 1
69 method _rev {} {
70 if {[catch {$w_rev commit_or_die}]} {
71 return {}
73 return [$w_rev get]
76 method _visualize {} {
77 set rev [_rev $this]
78 if {$rev ne {}} {
79 do_gitk [list $rev --not HEAD]
83 method _start {} {
84 global HEAD current_branch
86 set name [_rev $this]
87 if {$name eq {}} {
88 return
91 set cmd [list git merge $name]
92 set msg "Merging $current_branch and $name"
93 ui_status "$msg..."
94 set cons [console::new "Merge" $cmd]
95 console::exec $cons $cmd [cb _finish $cons]
97 wm protocol $w WM_DELETE_WINDOW {}
98 destroy $w
101 method _finish {cons ok} {
102 console::done $cons $ok
103 if {$ok} {
104 set msg {Merge completed successfully.}
105 } else {
106 set msg {Merge failed. Conflict resolution is required.}
108 unlock_index
109 rescan [list ui_status $msg]
110 delete_this
113 constructor dialog {} {
114 global current_branch
115 global M1B
117 if {![_can_merge $this]} {
118 delete_this
119 return
122 make_toplevel top w
123 wm title $top "[appname] ([reponame]): Merge"
124 if {$top ne {.}} {
125 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
128 set _start [cb _start]
130 label $w.header \
131 -text "Merge Into $current_branch" \
132 -font font_uibold
133 pack $w.header -side top -fill x
135 frame $w.buttons
136 button $w.buttons.visualize \
137 -text Visualize \
138 -command [cb _visualize]
139 pack $w.buttons.visualize -side left
140 button $w.buttons.merge \
141 -text Merge \
142 -command $_start
143 pack $w.buttons.merge -side right
144 button $w.buttons.cancel \
145 -text {Cancel} \
146 -command [cb _cancel]
147 pack $w.buttons.cancel -side right -padx 5
148 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
150 set w_rev [::choose_rev::new_unmerged $w.rev {Revision To Merge}]
151 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
153 bind $w <$M1B-Key-Return> $_start
154 bind $w <Key-Return> $_start
155 bind $w <Key-Escape> [cb _cancel]
156 wm protocol $w WM_DELETE_WINDOW [cb _cancel]
158 bind $w.buttons.merge <Visibility> [cb _visible]
159 tkwait window $w
162 method _visible {} {
163 grab $w
164 if {[is_config_true gui.matchtrackingbranch]} {
165 $w_rev pick_tracking_branch
167 $w_rev focus_filter
170 method _cancel {} {
171 wm protocol $w WM_DELETE_WINDOW {}
172 unlock_index
173 destroy $w
174 delete_this
179 namespace eval merge {
181 proc reset_hard {} {
182 global HEAD commit_type file_states
184 if {[string match amend* $commit_type]} {
185 info_popup {Cannot abort while amending.
187 You must finish amending this commit.
189 return
192 if {![lock_index abort]} return
194 if {[string match *merge* $commit_type]} {
195 set op merge
196 } else {
197 set op commit
200 if {[ask_popup "Abort $op?
202 Aborting the current $op will cause *ALL* uncommitted changes to be lost.
204 Continue with aborting the current $op?"] eq {yes}} {
205 set fd [git_read read-tree --reset -u HEAD]
206 fconfigure $fd -blocking 0 -translation binary
207 fileevent $fd readable [namespace code [list _reset_wait $fd]]
208 ui_status {Aborting... please wait...}
209 } else {
210 unlock_index
214 proc _reset_wait {fd} {
215 global ui_comm
217 read $fd
218 if {[eof $fd]} {
219 close $fd
220 unlock_index
222 $ui_comm delete 0.0 end
223 $ui_comm edit modified false
225 catch {file delete [gitdir MERGE_HEAD]}
226 catch {file delete [gitdir rr-cache MERGE_RR]}
227 catch {file delete [gitdir SQUASH_MSG]}
228 catch {file delete [gitdir MERGE_MSG]}
229 catch {file delete [gitdir GITGUI_MSG]}
231 rescan {ui_status {Abort completed. Ready.}}