Prepare for 2.15.2
[git.git] / git-gui / lib / merge.tcl
blob9f253db5b37e5633c19914248a58a3f2feb3c512
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 [mc "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 [mc "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 U? {
45 error_popup [mc "You are in the middle of a conflicted merge.
47 File %s has merge conflicts.
49 You must resolve them, stage the file, and commit to complete the current merge. Only then can you begin another merge.
50 " [short_path $path]]
51 unlock_index
52 return 0
54 ?? {
55 error_popup [mc "You are in the middle of a change.
57 File %s is modified.
59 You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
60 " [short_path $path]]
61 unlock_index
62 return 0
67 return 1
70 method _rev {} {
71 if {[catch {$w_rev commit_or_die}]} {
72 return {}
74 return [$w_rev get]
77 method _visualize {} {
78 set rev [_rev $this]
79 if {$rev ne {}} {
80 do_gitk [list $rev --not HEAD]
84 method _start {} {
85 global HEAD current_branch remote_url
86 global _last_merged_branch
88 set name [_rev $this]
89 if {$name eq {}} {
90 return
93 set spec [$w_rev get_tracking_branch]
94 set cmit [$w_rev get_commit]
96 set fh [open [gitdir FETCH_HEAD] w]
97 fconfigure $fh -translation lf
98 if {$spec eq {}} {
99 set remote .
100 set branch $name
101 set stitle $branch
102 } else {
103 set remote $remote_url([lindex $spec 1])
104 if {[regexp {^[^:@]*@[^:]*:/} $remote]} {
105 regsub {^[^:@]*@} $remote {} remote
107 set branch [lindex $spec 2]
108 set stitle [mc "%s of %s" $branch $remote]
110 regsub ^refs/heads/ $branch {} branch
111 puts $fh "$cmit\t\tbranch '$branch' of $remote"
112 close $fh
113 set _last_merged_branch $branch
115 if {[git-version >= "2.5.0"]} {
116 set cmd [list git merge --strategy=recursive FETCH_HEAD]
117 } else {
118 set cmd [list git]
119 lappend cmd merge
120 lappend cmd --strategy=recursive
121 lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
122 lappend cmd HEAD
123 lappend cmd $name
126 ui_status [mc "Merging %s and %s..." $current_branch $stitle]
127 set cons [console::new [mc "Merge"] "merge $stitle"]
128 console::exec $cons $cmd [cb _finish $cons]
130 wm protocol $w WM_DELETE_WINDOW {}
131 destroy $w
134 method _finish {cons ok} {
135 console::done $cons $ok
136 if {$ok} {
137 set msg [mc "Merge completed successfully."]
138 } else {
139 set msg [mc "Merge failed. Conflict resolution is required."]
141 unlock_index
142 rescan [list ui_status $msg]
143 delete_this
146 constructor dialog {} {
147 global current_branch
148 global M1B use_ttk NS
150 if {![_can_merge $this]} {
151 delete_this
152 return
155 make_dialog top w
156 wm title $top [mc "%s (%s): Merge" [appname] [reponame]]
157 if {$top ne {.}} {
158 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
161 set _start [cb _start]
163 ${NS}::label $w.header \
164 -text [mc "Merge Into %s" $current_branch] \
165 -font font_uibold
166 pack $w.header -side top -fill x
168 ${NS}::frame $w.buttons
169 ${NS}::button $w.buttons.visualize \
170 -text [mc Visualize] \
171 -command [cb _visualize]
172 pack $w.buttons.visualize -side left
173 ${NS}::button $w.buttons.merge \
174 -text [mc Merge] \
175 -command $_start
176 pack $w.buttons.merge -side right
177 ${NS}::button $w.buttons.cancel \
178 -text [mc "Cancel"] \
179 -command [cb _cancel]
180 pack $w.buttons.cancel -side right -padx 5
181 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
183 set w_rev [::choose_rev::new_unmerged $w.rev [mc "Revision To Merge"]]
184 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
186 bind $w <$M1B-Key-Return> $_start
187 bind $w <Key-Return> $_start
188 bind $w <Key-Escape> [cb _cancel]
189 wm protocol $w WM_DELETE_WINDOW [cb _cancel]
191 bind $w.buttons.merge <Visibility> [cb _visible]
192 tkwait window $w
195 method _visible {} {
196 grab $w
197 if {[is_config_true gui.matchtrackingbranch]} {
198 $w_rev pick_tracking_branch
200 $w_rev focus_filter
203 method _cancel {} {
204 wm protocol $w WM_DELETE_WINDOW {}
205 unlock_index
206 destroy $w
207 delete_this
212 namespace eval merge {
214 proc reset_hard {} {
215 global HEAD commit_type file_states
217 if {[string match amend* $commit_type]} {
218 info_popup [mc "Cannot abort while amending.
220 You must finish amending this commit.
222 return
225 if {![lock_index abort]} return
227 if {[string match *merge* $commit_type]} {
228 set op_question [mc "Abort merge?
230 Aborting the current merge will cause *ALL* uncommitted changes to be lost.
232 Continue with aborting the current merge?"]
233 } else {
234 set op_question [mc "Reset changes?
236 Resetting the changes will cause *ALL* uncommitted changes to be lost.
238 Continue with resetting the current changes?"]
241 if {[ask_popup $op_question] eq {yes}} {
242 set fd [git_read --stderr read-tree --reset -u -v HEAD]
243 fconfigure $fd -blocking 0 -translation binary
244 fileevent $fd readable [namespace code [list _reset_wait $fd]]
245 $::main_status start [mc "Aborting"] [mc "files reset"]
246 } else {
247 unlock_index
251 proc _reset_wait {fd} {
252 global ui_comm
254 $::main_status update_meter [read $fd]
256 fconfigure $fd -blocking 1
257 if {[eof $fd]} {
258 set fail [catch {close $fd} err]
259 $::main_status stop
260 unlock_index
262 $ui_comm delete 0.0 end
263 $ui_comm edit modified false
265 catch {file delete [gitdir MERGE_HEAD]}
266 catch {file delete [gitdir rr-cache MERGE_RR]}
267 catch {file delete [gitdir MERGE_RR]}
268 catch {file delete [gitdir SQUASH_MSG]}
269 catch {file delete [gitdir MERGE_MSG]}
270 catch {file delete [gitdir GITGUI_MSG]}
272 if {$fail} {
273 warn_popup "[mc "Abort failed."]\n\n$err"
275 rescan {ui_status [mc "Abort completed. Ready."]}
276 } else {
277 fconfigure $fd -blocking 0