git-gui: Refactor our ui_status_value update technique
[git/gitweb.git] / lib / merge.tcl
blobf0a02ea2288976e03dad1de9fd22d4b11e403382
1 # git-gui branch merge support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 namespace eval merge {
6 proc _can_merge {} {
7 global HEAD commit_type file_states
9 if {[string match amend* $commit_type]} {
10 info_popup {Cannot merge while amending.
12 You must finish amending this commit before starting any type of merge.
14 return 0
17 if {[committer_ident] eq {}} {return 0}
18 if {![lock_index merge]} {return 0}
20 # -- Our in memory state should match the repository.
22 repository_state curType curHEAD curMERGE_HEAD
23 if {$commit_type ne $curType || $HEAD ne $curHEAD} {
24 info_popup {Last scanned state does not match repository state.
26 Another Git program has modified this repository since the last scan. A rescan must be performed before a merge can be performed.
28 The rescan will be automatically started now.
30 unlock_index
31 rescan ui_ready
32 return 0
35 foreach path [array names file_states] {
36 switch -glob -- [lindex $file_states($path) 0] {
37 _O {
38 continue; # and pray it works!
40 U? {
41 error_popup "You are in the middle of a conflicted merge.
43 File [short_path $path] has merge conflicts.
45 You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
47 unlock_index
48 return 0
50 ?? {
51 error_popup "You are in the middle of a change.
53 File [short_path $path] is modified.
55 You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
57 unlock_index
58 return 0
63 return 1
66 proc _refs {w list} {
67 set r {}
68 foreach i [$w.source.l curselection] {
69 lappend r [lindex [lindex $list $i] 0]
71 return $r
74 proc _visualize {w list} {
75 set revs [_refs $w $list]
76 if {$revs eq {}} return
77 lappend revs --not HEAD
78 do_gitk $revs
81 proc _start {w list} {
82 global HEAD current_branch
84 set cmd [list git merge]
85 set names [_refs $w $list]
86 set revcnt [llength $names]
87 append cmd { } $names
89 if {$revcnt == 0} {
90 return
91 } elseif {$revcnt == 1} {
92 set unit branch
93 } elseif {$revcnt <= 15} {
94 set unit branches
96 if {[tk_dialog \
97 $w.confirm_octopus \
98 [wm title $w] \
99 "Use octopus merge strategy?
101 You are merging $revcnt branches at once. This requires using the octopus merge driver, which may not succeed if there are file-level conflicts.
103 question \
105 {Cancel} \
106 {Use octopus} \
107 ] != 1} return
108 } else {
109 tk_messageBox \
110 -icon error \
111 -type ok \
112 -title [wm title $w] \
113 -parent $w \
114 -message "Too many branches selected.
116 You have requested to merge $revcnt branches in an octopus merge. This exceeds Git's internal limit of 15 branches per merge.
118 Please select fewer branches. To merge more than 15 branches, merge the branches in batches.
120 return
123 set msg "Merging $current_branch, [join $names {, }]"
124 ui_status "$msg..."
125 set cons [console::new "Merge" $msg]
126 console::exec $cons $cmd \
127 [namespace code [list _finish $revcnt $cons]]
129 wm protocol $w WM_DELETE_WINDOW {}
130 destroy $w
133 proc _finish {revcnt w ok} {
134 console::done $w $ok
135 if {$ok} {
136 set msg {Merge completed successfully.}
137 } else {
138 if {$revcnt != 1} {
139 info_popup "Octopus merge failed.
141 Your merge of $revcnt branches has failed.
143 There are file-level conflicts between the branches which must be resolved manually.
145 The working directory will now be reset.
147 You can attempt this merge again by merging only one branch at a time." $w
149 set fd [open "| git read-tree --reset -u HEAD" r]
150 fconfigure $fd -blocking 0 -translation binary
151 fileevent $fd readable \
152 [namespace code [list _reset_wait $fd]]
153 ui_status {Aborting... please wait...}
154 return
157 set msg {Merge failed. Conflict resolution is required.}
159 unlock_index
160 rescan [list ui_status $msg]
163 proc dialog {} {
164 global current_branch
165 global M1B
167 if {![_can_merge]} return
169 set fmt {list %(objectname) %(*objectname) %(refname) %(subject)}
170 set cmd [list git for-each-ref --tcl --format=$fmt]
171 lappend cmd refs/heads
172 lappend cmd refs/remotes
173 lappend cmd refs/tags
174 set fr_fd [open "| $cmd" r]
175 fconfigure $fr_fd -translation binary
176 while {[gets $fr_fd line] > 0} {
177 set line [eval $line]
178 set ref [lindex $line 2]
179 regsub ^refs/(heads|remotes|tags)/ $ref {} ref
180 set subj($ref) [lindex $line 3]
181 lappend sha1([lindex $line 0]) $ref
182 if {[lindex $line 1] ne {}} {
183 lappend sha1([lindex $line 1]) $ref
186 close $fr_fd
188 set to_show {}
189 set fr_fd [open "| git rev-list --all --not HEAD"]
190 while {[gets $fr_fd line] > 0} {
191 if {[catch {set ref $sha1($line)}]} continue
192 foreach n $ref {
193 lappend to_show [list $n $line]
196 close $fr_fd
197 set to_show [lsort -unique $to_show]
199 set w .merge_setup
200 toplevel $w
201 wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
203 set _visualize [namespace code [list _visualize $w $to_show]]
204 set _start [namespace code [list _start $w $to_show]]
206 label $w.header \
207 -text "Merge Into $current_branch" \
208 -font font_uibold
209 pack $w.header -side top -fill x
211 frame $w.buttons
212 button $w.buttons.visualize -text Visualize -command $_visualize
213 pack $w.buttons.visualize -side left
214 button $w.buttons.create -text Merge -command $_start
215 pack $w.buttons.create -side right
216 button $w.buttons.cancel \
217 -text {Cancel} \
218 -command "unlock_index;destroy $w"
219 pack $w.buttons.cancel -side right -padx 5
220 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
222 labelframe $w.source -text {Source Branches}
223 listbox $w.source.l \
224 -height 10 \
225 -width 70 \
226 -font font_diff \
227 -selectmode extended \
228 -yscrollcommand [list $w.source.sby set]
229 scrollbar $w.source.sby -command [list $w.source.l yview]
230 pack $w.source.sby -side right -fill y
231 pack $w.source.l -side left -fill both -expand 1
232 pack $w.source -fill both -expand 1 -pady 5 -padx 5
234 foreach ref $to_show {
235 set n [lindex $ref 0]
236 if {[string length $n] > 20} {
237 set n "[string range $n 0 16]..."
239 $w.source.l insert end [format {%s %-20s %s} \
240 [string range [lindex $ref 1] 0 5] \
241 $n \
242 $subj([lindex $ref 0])]
245 bind $w.source.l <Key-K> [list event generate %W <Shift-Key-Up>]
246 bind $w.source.l <Key-J> [list event generate %W <Shift-Key-Down>]
247 bind $w.source.l <Key-k> [list event generate %W <Key-Up>]
248 bind $w.source.l <Key-j> [list event generate %W <Key-Down>]
249 bind $w.source.l <Key-h> [list event generate %W <Key-Left>]
250 bind $w.source.l <Key-l> [list event generate %W <Key-Right>]
251 bind $w.source.l <Key-v> $_visualize
253 bind $w <$M1B-Key-Return> $_start
254 bind $w <Visibility> "grab $w; focus $w.source.l"
255 bind $w <Key-Escape> "unlock_index;destroy $w"
256 wm protocol $w WM_DELETE_WINDOW "unlock_index;destroy $w"
257 wm title $w "[appname] ([reponame]): Merge"
258 tkwait window $w
261 proc reset_hard {} {
262 global HEAD commit_type file_states
264 if {[string match amend* $commit_type]} {
265 info_popup {Cannot abort while amending.
267 You must finish amending this commit.
269 return
272 if {![lock_index abort]} return
274 if {[string match *merge* $commit_type]} {
275 set op merge
276 } else {
277 set op commit
280 if {[ask_popup "Abort $op?
282 Aborting the current $op will cause *ALL* uncommitted changes to be lost.
284 Continue with aborting the current $op?"] eq {yes}} {
285 set fd [open "| git read-tree --reset -u HEAD" r]
286 fconfigure $fd -blocking 0 -translation binary
287 fileevent $fd readable [namespace code [list _reset_wait $fd]]
288 ui_status {Aborting... please wait...}
289 } else {
290 unlock_index
294 proc _reset_wait {fd} {
295 global ui_comm
297 read $fd
298 if {[eof $fd]} {
299 close $fd
300 unlock_index
302 $ui_comm delete 0.0 end
303 $ui_comm edit modified false
305 catch {file delete [gitdir MERGE_HEAD]}
306 catch {file delete [gitdir rr-cache MERGE_RR]}
307 catch {file delete [gitdir SQUASH_MSG]}
308 catch {file delete [gitdir MERGE_MSG]}
309 catch {file delete [gitdir GITGUI_MSG]}
311 rescan {ui_status {Abort completed. Ready.}}