1# git-gui branch merge support
2# Copyright (C) 2006, 2007 Shawn Pearce
3
4class merge {
5
6field w ; # top level window
7field w_rev ; # mega-widget to pick the revision to merge
8
9method _can_merge {} {
10 global HEAD commit_type file_states
11
12 if {[string match amend* $commit_type]} {
13 info_popup [mc "Cannot merge while amending.
14
15You must finish amending this commit before starting any type of merge.
16"]
17 return 0
18 }
19
20 if {[committer_ident] eq {}} {return 0}
21 if {![lock_index merge]} {return 0}
22
23 # -- Our in memory state should match the repository.
24 #
25 repository_state curType curHEAD curMERGE_HEAD
26 if {$commit_type ne $curType || $HEAD ne $curHEAD} {
27 info_popup [mc "Last scanned state does not match repository state.
28
29Another Git program has modified this repository since the last scan. A rescan must be performed before a merge can be performed.
30
31The rescan will be automatically started now.
32"]
33 unlock_index
34 rescan ui_ready
35 return 0
36 }
37
38 foreach path [array names file_states] {
39 switch -glob -- [lindex $file_states($path) 0] {
40 _O {
41 continue; # and pray it works!
42 }
43 U? {
44 error_popup [mc "You are in the middle of a conflicted merge.
45
46File %s has merge conflicts.
47
48You must resolve them, stage the file, and commit to complete the current merge. Only then can you begin another merge.
49" [short_path $path]]
50 unlock_index
51 return 0
52 }
53 ?? {
54 error_popup [mc "You are in the middle of a change.
55
56File %s is modified.
57
58You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
59" [short_path $path]]
60 unlock_index
61 return 0
62 }
63 }
64 }
65
66 return 1
67}
68
69method _rev {} {
70 if {[catch {$w_rev commit_or_die}]} {
71 return {}
72 }
73 return [$w_rev get]
74}
75
76method _visualize {} {
77 set rev [_rev $this]
78 if {$rev ne {}} {
79 do_gitk [list $rev --not HEAD]
80 }
81}
82
83method _start {} {
84 global HEAD current_branch remote_url
85
86 set name [_rev $this]
87 if {$name eq {}} {
88 return
89 }
90
91 set spec [$w_rev get_tracking_branch]
92 set cmit [$w_rev get_commit]
93
94 set fh [open [gitdir FETCH_HEAD] w]
95 fconfigure $fh -translation lf
96 if {$spec eq {}} {
97 set remote .
98 set branch $name
99 set stitle $branch
100 } else {
101 set remote $remote_url([lindex $spec 1])
102 if {[regexp {^[^:@]*@[^:]*:/} $remote]} {
103 regsub {^[^:@]*@} $remote {} remote
104 }
105 set branch [lindex $spec 2]
106 set stitle [mc "%s of %s" $branch $remote]
107 }
108 regsub ^refs/heads/ $branch {} branch
109 puts $fh "$cmit\t\tbranch '$branch' of $remote"
110 close $fh
111
112 set cmd [list git]
113 lappend cmd merge
114 lappend cmd --strategy=recursive
115 lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
116 lappend cmd HEAD
117 lappend cmd $name
118
119 ui_status [mc "Merging %s and %s..." $current_branch $stitle]
120 set cons [console::new [mc "Merge"] "merge $stitle"]
121 console::exec $cons $cmd [cb _finish $cons]
122
123 wm protocol $w WM_DELETE_WINDOW {}
124 destroy $w
125}
126
127method _finish {cons ok} {
128 console::done $cons $ok
129 if {$ok} {
130 set msg [mc "Merge completed successfully."]
131 } else {
132 set msg [mc "Merge failed. Conflict resolution is required."]
133 }
134 unlock_index
135 rescan [list ui_status $msg]
136 delete_this
137}
138
139constructor dialog {} {
140 global current_branch
141 global M1B
142
143 if {![_can_merge $this]} {
144 delete_this
145 return
146 }
147
148 make_toplevel top w
149 wm title $top [append "[appname] ([reponame]): " [mc "Merge"]]
150 if {$top ne {.}} {
151 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
152 }
153
154 set _start [cb _start]
155
156 label $w.header \
157 -text [mc "Merge Into %s" $current_branch] \
158 -font font_uibold
159 pack $w.header -side top -fill x
160
161 frame $w.buttons
162 button $w.buttons.visualize \
163 -text [mc Visualize] \
164 -command [cb _visualize]
165 pack $w.buttons.visualize -side left
166 button $w.buttons.merge \
167 -text [mc Merge] \
168 -command $_start
169 pack $w.buttons.merge -side right
170 button $w.buttons.cancel \
171 -text [mc "Cancel"] \
172 -command [cb _cancel]
173 pack $w.buttons.cancel -side right -padx 5
174 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
175
176 set w_rev [::choose_rev::new_unmerged $w.rev [mc "Revision To Merge"]]
177 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
178
179 bind $w <$M1B-Key-Return> $_start
180 bind $w <Key-Return> $_start
181 bind $w <Key-Escape> [cb _cancel]
182 wm protocol $w WM_DELETE_WINDOW [cb _cancel]
183
184 bind $w.buttons.merge <Visibility> [cb _visible]
185 tkwait window $w
186}
187
188method _visible {} {
189 grab $w
190 if {[is_config_true gui.matchtrackingbranch]} {
191 $w_rev pick_tracking_branch
192 }
193 $w_rev focus_filter
194}
195
196method _cancel {} {
197 wm protocol $w WM_DELETE_WINDOW {}
198 unlock_index
199 destroy $w
200 delete_this
201}
202
203}
204
205namespace eval merge {
206
207proc reset_hard {} {
208 global HEAD commit_type file_states
209
210 if {[string match amend* $commit_type]} {
211 info_popup [mc "Cannot abort while amending.
212
213You must finish amending this commit.
214"]
215 return
216 }
217
218 if {![lock_index abort]} return
219
220 if {[string match *merge* $commit_type]} {
221 set op_question [mc "Abort merge?
222
223Aborting the current merge will cause *ALL* uncommitted changes to be lost.
224
225Continue with aborting the current merge?"]
226 } else {
227 set op_question [mc "Reset changes?
228
229Resetting the changes will cause *ALL* uncommitted changes to be lost.
230
231Continue with resetting the current changes?"]
232 }
233
234 if {[ask_popup $op_question] eq {yes}} {
235 set fd [git_read --stderr read-tree --reset -u -v HEAD]
236 fconfigure $fd -blocking 0 -translation binary
237 fileevent $fd readable [namespace code [list _reset_wait $fd]]
238 $::main_status start [mc "Aborting"] [mc "files reset"]
239 } else {
240 unlock_index
241 }
242}
243
244proc _reset_wait {fd} {
245 global ui_comm
246
247 $::main_status update_meter [read $fd]
248
249 fconfigure $fd -blocking 1
250 if {[eof $fd]} {
251 set fail [catch {close $fd} err]
252 $::main_status stop
253 unlock_index
254
255 $ui_comm delete 0.0 end
256 $ui_comm edit modified false
257
258 catch {file delete [gitdir MERGE_HEAD]}
259 catch {file delete [gitdir rr-cache MERGE_RR]}
260 catch {file delete [gitdir SQUASH_MSG]}
261 catch {file delete [gitdir MERGE_MSG]}
262 catch {file delete [gitdir GITGUI_MSG]}
263
264 if {$fail} {
265 warn_popup "[mc "Abort failed."]\n\n$err"
266 }
267 rescan {ui_status [mc "Abort completed. Ready."]}
268 } else {
269 fconfigure $fd -blocking 0
270 }
271}
272
273}