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 {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 {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 "You are in the middle of a conflicted merge.
45
46File [short_path $path] 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"
50 unlock_index
51 return 0
52 }
53 ?? {
54 error_popup "You are in the middle of a change.
55
56File [short_path $path] 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"
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 "$branch of $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 set msg "Merging $current_branch and $stitle"
120 ui_status "$msg..."
121 set cons [console::new "Merge" "merge $stitle"]
122 console::exec $cons $cmd [cb _finish $cons]
123
124 wm protocol $w WM_DELETE_WINDOW {}
125 destroy $w
126}
127
128method _finish {cons ok} {
129 console::done $cons $ok
130 if {$ok} {
131 set msg {Merge completed successfully.}
132 } else {
133 set msg {Merge failed. Conflict resolution is required.}
134 }
135 unlock_index
136 rescan [list ui_status $msg]
137 delete_this
138}
139
140constructor dialog {} {
141 global current_branch
142 global M1B
143
144 if {![_can_merge $this]} {
145 delete_this
146 return
147 }
148
149 make_toplevel top w
150 wm title $top "[appname] ([reponame]): Merge"
151 if {$top ne {.}} {
152 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
153 }
154
155 set _start [cb _start]
156
157 label $w.header \
158 -text "Merge Into $current_branch" \
159 -font font_uibold
160 pack $w.header -side top -fill x
161
162 frame $w.buttons
163 button $w.buttons.visualize \
164 -text Visualize \
165 -command [cb _visualize]
166 pack $w.buttons.visualize -side left
167 button $w.buttons.merge \
168 -text Merge \
169 -command $_start
170 pack $w.buttons.merge -side right
171 button $w.buttons.cancel \
172 -text {Cancel} \
173 -command [cb _cancel]
174 pack $w.buttons.cancel -side right -padx 5
175 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
176
177 set w_rev [::choose_rev::new_unmerged $w.rev {Revision To Merge}]
178 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
179
180 bind $w <$M1B-Key-Return> $_start
181 bind $w <Key-Return> $_start
182 bind $w <Key-Escape> [cb _cancel]
183 wm protocol $w WM_DELETE_WINDOW [cb _cancel]
184
185 bind $w.buttons.merge <Visibility> [cb _visible]
186 tkwait window $w
187}
188
189method _visible {} {
190 grab $w
191 if {[is_config_true gui.matchtrackingbranch]} {
192 $w_rev pick_tracking_branch
193 }
194 $w_rev focus_filter
195}
196
197method _cancel {} {
198 wm protocol $w WM_DELETE_WINDOW {}
199 unlock_index
200 destroy $w
201 delete_this
202}
203
204}
205
206namespace eval merge {
207
208proc reset_hard {} {
209 global HEAD commit_type file_states
210
211 if {[string match amend* $commit_type]} {
212 info_popup {Cannot abort while amending.
213
214You must finish amending this commit.
215}
216 return
217 }
218
219 if {![lock_index abort]} return
220
221 if {[string match *merge* $commit_type]} {
222 set op_question "Abort merge?
223
224Aborting the current merge will cause *ALL* uncommitted changes to be lost.
225
226Continue with aborting the current merge?"
227 } else {
228 set op_question "Reset changes?
229
230Resetting the changes will cause *ALL* uncommitted changes to be lost.
231
232Continue with resetting the current changes?"
233 }
234
235 if {[ask_popup $op_question] eq {yes}} {
236 set fd [git_read --stderr read-tree --reset -u -v HEAD]
237 fconfigure $fd -blocking 0 -translation binary
238 fileevent $fd readable [namespace code [list _reset_wait $fd]]
239 $::main_status start {Aborting} {files reset}
240 } else {
241 unlock_index
242 }
243}
244
245proc _reset_wait {fd} {
246 global ui_comm
247
248 $::main_status update_meter [read $fd]
249
250 fconfigure $fd -blocking 1
251 if {[eof $fd]} {
252 set fail [catch {close $fd} err]
253 $::main_status stop
254 unlock_index
255
256 $ui_comm delete 0.0 end
257 $ui_comm edit modified false
258
259 catch {file delete [gitdir MERGE_HEAD]}
260 catch {file delete [gitdir rr-cache MERGE_RR]}
261 catch {file delete [gitdir SQUASH_MSG]}
262 catch {file delete [gitdir MERGE_MSG]}
263 catch {file delete [gitdir GITGUI_MSG]}
264
265 if {$fail} {
266 warn_popup "Abort failed.\n\n$err"
267 }
268 rescan {ui_status {Abort completed. Ready.}}
269 } else {
270 fconfigure $fd -blocking 0
271 }
272}
273
274}