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