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