blob: 5cded2341c541b5688adbac35d3d2e1216bc2d0b [file] [log] [blame]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -04001# git-gui branch merge support
2# Copyright (C) 2006, 2007 Shawn Pearce
3
Shawn O. Pearceff749c12007-07-18 02:56:44 -04004class merge {
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -04005
Shawn O. Pearceff749c12007-07-18 02:56:44 -04006field w ; # top level window
Shawn O. Pearce350a35f2007-07-25 03:44:50 -04007field w_rev ; # mega-widget to pick the revision to merge
Shawn O. Pearceff749c12007-07-18 02:56:44 -04008
9method _can_merge {} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040010 global HEAD commit_type file_states
11
12 if {[string match amend* $commit_type]} {
Christian Stimming1ac17952007-07-21 14:21:34 +020013 info_popup [mc "Cannot merge while amending.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040014
15You must finish amending this commit before starting any type of merge.
Christian Stimming1ac17952007-07-21 14:21:34 +020016"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040017 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} {
Christian Stimming1ac17952007-07-21 14:21:34 +020027 info_popup [mc "Last scanned state does not match repository state.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040028
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.
Christian Stimming1ac17952007-07-21 14:21:34 +020032"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040033 unlock_index
Shawn O. Pearce699d5602007-07-05 23:16:13 -040034 rescan ui_ready
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040035 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 }
Johannes Sixta9108982008-10-08 10:03:54 +020043 _U -
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040044 U? {
Christian Stimming1ac17952007-07-21 14:21:34 +020045 error_popup [mc "You are in the middle of a conflicted merge.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040046
Christian Stimming1ac17952007-07-21 14:21:34 +020047File %s has merge conflicts.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040048
Christian Stimming360cc102007-07-28 22:17:10 +020049You must resolve them, stage the file, and commit to complete the current merge. Only then can you begin another merge.
Christian Stimming1ac17952007-07-21 14:21:34 +020050" [short_path $path]]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040051 unlock_index
52 return 0
53 }
54 ?? {
Christian Stimming1ac17952007-07-21 14:21:34 +020055 error_popup [mc "You are in the middle of a change.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040056
Christian Stimming1ac17952007-07-21 14:21:34 +020057File %s is modified.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040058
59You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
Christian Stimming1ac17952007-07-21 14:21:34 +020060" [short_path $path]]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040061 unlock_index
62 return 0
63 }
64 }
65 }
66
67 return 1
68}
69
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -040070method _rev {} {
Shawn O. Pearce350a35f2007-07-25 03:44:50 -040071 if {[catch {$w_rev commit_or_die}]} {
72 return {}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040073 }
Shawn O. Pearce350a35f2007-07-25 03:44:50 -040074 return [$w_rev get]
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -040075}
76
Shawn O. Pearceff749c12007-07-18 02:56:44 -040077method _visualize {} {
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -040078 set rev [_rev $this]
79 if {$rev ne {}} {
80 do_gitk [list $rev --not HEAD]
81 }
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040082}
83
Shawn O. Pearceff749c12007-07-18 02:56:44 -040084method _start {} {
Shawn O. Pearceead49f52007-07-25 04:54:53 -040085 global HEAD current_branch remote_url
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040086
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -040087 set name [_rev $this]
88 if {$name eq {}} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040089 return
90 }
91
Shawn O. Pearceead49f52007-07-25 04:54:53 -040092 set spec [$w_rev get_tracking_branch]
93 set cmit [$w_rev get_commit]
Shawn O. Pearceead49f52007-07-25 04:54:53 -040094
95 set fh [open [gitdir FETCH_HEAD] w]
96 fconfigure $fh -translation lf
97 if {$spec eq {}} {
98 set remote .
99 set branch $name
100 set stitle $branch
101 } else {
102 set remote $remote_url([lindex $spec 1])
Shawn O. Pearcebc318ea2007-07-25 05:02:38 -0400103 if {[regexp {^[^:@]*@[^:]*:/} $remote]} {
104 regsub {^[^:@]*@} $remote {} remote
105 }
Shawn O. Pearceead49f52007-07-25 04:54:53 -0400106 set branch [lindex $spec 2]
Christian Stimming1ac17952007-07-21 14:21:34 +0200107 set stitle [mc "%s of %s" $branch $remote]
Shawn O. Pearceead49f52007-07-25 04:54:53 -0400108 }
109 regsub ^refs/heads/ $branch {} branch
110 puts $fh "$cmit\t\tbranch '$branch' of $remote"
111 close $fh
112
Shawn O. Pearce84f67532007-07-29 20:21:54 -0400113 set cmd [list git]
114 lappend cmd merge
115 lappend cmd --strategy=recursive
Shawn O. Pearceead49f52007-07-25 04:54:53 -0400116 lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
117 lappend cmd HEAD
Shawn O. Pearce18a01a02007-08-20 02:17:05 -0400118 lappend cmd $name
Shawn O. Pearceead49f52007-07-25 04:54:53 -0400119
Christian Stimming5e6d7762008-02-02 10:20:17 +0100120 ui_status [mc "Merging %s and %s..." $current_branch $stitle]
Christian Stimming1ac17952007-07-21 14:21:34 +0200121 set cons [console::new [mc "Merge"] "merge $stitle"]
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -0400122 console::exec $cons $cmd [cb _finish $cons]
Shawn O. Pearce39fa2a92007-06-11 23:52:43 -0400123
124 wm protocol $w WM_DELETE_WINDOW {}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400125 destroy $w
126}
127
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -0400128method _finish {cons ok} {
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400129 console::done $cons $ok
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400130 if {$ok} {
Christian Stimming1ac17952007-07-21 14:21:34 +0200131 set msg [mc "Merge completed successfully."]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400132 } else {
Christian Stimming1ac17952007-07-21 14:21:34 +0200133 set msg [mc "Merge failed. Conflict resolution is required."]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400134 }
135 unlock_index
Shawn O. Pearce699d5602007-07-05 23:16:13 -0400136 rescan [list ui_status $msg]
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400137 delete_this
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400138}
139
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400140constructor dialog {} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400141 global current_branch
Pat Thoytsc80d7be2010-01-26 00:05:31 +0000142 global M1B use_ttk NS
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400143
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400144 if {![_can_merge $this]} {
145 delete_this
146 return
147 }
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400148
Pat Thoytsc80d7be2010-01-26 00:05:31 +0000149 make_dialog top w
Christian Stimming1ac17952007-07-21 14:21:34 +0200150 wm title $top [append "[appname] ([reponame]): " [mc "Merge"]]
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400151 if {$top ne {.}} {
152 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
153 }
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -0400154
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400155 set _start [cb _start]
Shawn O. Pearceebcaada2007-05-05 02:28:41 -0400156
Pat Thoytsc80d7be2010-01-26 00:05:31 +0000157 ${NS}::label $w.header \
Christian Stimming1ac17952007-07-21 14:21:34 +0200158 -text [mc "Merge Into %s" $current_branch] \
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -0400159 -font font_uibold
160 pack $w.header -side top -fill x
161
Pat Thoytsc80d7be2010-01-26 00:05:31 +0000162 ${NS}::frame $w.buttons
163 ${NS}::button $w.buttons.visualize \
Christian Stimming1ac17952007-07-21 14:21:34 +0200164 -text [mc Visualize] \
Shawn O. Pearce9feefbd2007-07-25 04:32:18 -0400165 -command [cb _visualize]
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -0400166 pack $w.buttons.visualize -side left
Pat Thoytsc80d7be2010-01-26 00:05:31 +0000167 ${NS}::button $w.buttons.merge \
Christian Stimming1ac17952007-07-21 14:21:34 +0200168 -text [mc Merge] \
Shawn O. Pearce9feefbd2007-07-25 04:32:18 -0400169 -command $_start
170 pack $w.buttons.merge -side right
Pat Thoytsc80d7be2010-01-26 00:05:31 +0000171 ${NS}::button $w.buttons.cancel \
Christian Stimming1ac17952007-07-21 14:21:34 +0200172 -text [mc "Cancel"] \
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400173 -command [cb _cancel]
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -0400174 pack $w.buttons.cancel -side right -padx 5
175 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
176
Christian Stimming1ac17952007-07-21 14:21:34 +0200177 set w_rev [::choose_rev::new_unmerged $w.rev [mc "Revision To Merge"]]
Shawn O. Pearce350a35f2007-07-25 03:44:50 -0400178 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
Shawn O. Pearceebcaada2007-05-05 02:28:41 -0400179
180 bind $w <$M1B-Key-Return> $_start
Shawn O. Pearce350a35f2007-07-25 03:44:50 -0400181 bind $w <Key-Return> $_start
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400182 bind $w <Key-Escape> [cb _cancel]
183 wm protocol $w WM_DELETE_WINDOW [cb _cancel]
Shawn O. Pearce9feefbd2007-07-25 04:32:18 -0400184
185 bind $w.buttons.merge <Visibility> [cb _visible]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400186 tkwait window $w
187}
188
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400189method _visible {} {
190 grab $w
Shawn O. Pearce350a35f2007-07-25 03:44:50 -0400191 if {[is_config_true gui.matchtrackingbranch]} {
192 $w_rev pick_tracking_branch
193 }
194 $w_rev focus_filter
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400195}
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
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400208proc reset_hard {} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400209 global HEAD commit_type file_states
210
211 if {[string match amend* $commit_type]} {
Christian Stimming1ac17952007-07-21 14:21:34 +0200212 info_popup [mc "Cannot abort while amending.
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400213
214You must finish amending this commit.
Christian Stimming1ac17952007-07-21 14:21:34 +0200215"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400216 return
217 }
218
219 if {![lock_index abort]} return
220
221 if {[string match *merge* $commit_type]} {
Christian Stimming1ac17952007-07-21 14:21:34 +0200222 set op_question [mc "Abort merge?
Christian Stimming360cc102007-07-28 22:17:10 +0200223
224Aborting the current merge will cause *ALL* uncommitted changes to be lost.
225
Christian Stimming1ac17952007-07-21 14:21:34 +0200226Continue with aborting the current merge?"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400227 } else {
Christian Stimming1ac17952007-07-21 14:21:34 +0200228 set op_question [mc "Reset changes?
Christian Stimming360cc102007-07-28 22:17:10 +0200229
230Resetting the changes will cause *ALL* uncommitted changes to be lost.
231
Christian Stimming1ac17952007-07-21 14:21:34 +0200232Continue with resetting the current changes?"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400233 }
234
Christian Stimming360cc102007-07-28 22:17:10 +0200235 if {[ask_popup $op_question] eq {yes}} {
Shawn O. Pearce0fe055c2007-07-29 04:06:51 -0400236 set fd [git_read --stderr read-tree --reset -u -v HEAD]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400237 fconfigure $fd -blocking 0 -translation binary
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400238 fileevent $fd readable [namespace code [list _reset_wait $fd]]
Christian Stimming5e6d7762008-02-02 10:20:17 +0100239 $::main_status start [mc "Aborting"] [mc "files reset"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400240 } else {
241 unlock_index
242 }
243}
244
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400245proc _reset_wait {fd} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400246 global ui_comm
247
Shawn O. Pearce0fe055c2007-07-29 04:06:51 -0400248 $::main_status update_meter [read $fd]
249
250 fconfigure $fd -blocking 1
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400251 if {[eof $fd]} {
Shawn O. Pearce0fe055c2007-07-29 04:06:51 -0400252 set fail [catch {close $fd} err]
253 $::main_status stop
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400254 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]}
Johannes Schindelinf049e092008-07-12 15:56:59 +0100261 catch {file delete [gitdir MERGE_RR]}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400262 catch {file delete [gitdir SQUASH_MSG]}
263 catch {file delete [gitdir MERGE_MSG]}
264 catch {file delete [gitdir GITGUI_MSG]}
265
Shawn O. Pearce0fe055c2007-07-29 04:06:51 -0400266 if {$fail} {
Christian Stimming1ac17952007-07-21 14:21:34 +0200267 warn_popup "[mc "Abort failed."]\n\n$err"
Shawn O. Pearce0fe055c2007-07-29 04:06:51 -0400268 }
Christian Stimming1ac17952007-07-21 14:21:34 +0200269 rescan {ui_status [mc "Abort completed. Ready."]}
Shawn O. Pearce0fe055c2007-07-29 04:06:51 -0400270 } else {
271 fconfigure $fd -blocking 0
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400272 }
273}
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400274
275}