git-gui: Format tracking branch merges as though they were pulls
[alt-git.git] / lib / merge.tcl
blobc8b486710aa66afce100219a1167618a0103cd87
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 {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 {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 error_popup "You are in the middle of a conflicted merge.
46 File [short_path $path] has merge conflicts.
48 You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
50 unlock_index
51 return 0
53 ?? {
54 error_popup "You are in the middle of a change.
56 File [short_path $path] is modified.
58 You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
60 unlock_index
61 return 0
66 return 1
69 method _rev {} {
70 if {[catch {$w_rev commit_or_die}]} {
71 return {}
73 return [$w_rev get]
76 method _visualize {} {
77 set rev [_rev $this]
78 if {$rev ne {}} {
79 do_gitk [list $rev --not HEAD]
83 method _start {} {
84 global HEAD current_branch remote_url
86 set name [_rev $this]
87 if {$name eq {}} {
88 return
91 set spec [$w_rev get_tracking_branch]
92 set cmit [$w_rev get_commit]
93 set cmd [list git]
94 lappend cmd merge
95 lappend cmd --strategy=recursive
97 set fh [open [gitdir FETCH_HEAD] w]
98 fconfigure $fh -translation lf
99 if {$spec eq {}} {
100 set remote .
101 set branch $name
102 set stitle $branch
103 } else {
104 set remote $remote_url([lindex $spec 1])
105 set branch [lindex $spec 2]
106 set stitle "$branch of $remote"
108 regsub ^refs/heads/ $branch {} branch
109 puts $fh "$cmit\t\tbranch '$branch' of $remote"
110 close $fh
112 lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
113 lappend cmd HEAD
114 lappend cmd $cmit
116 set msg "Merging $current_branch and $stitle"
117 ui_status "$msg..."
118 set cons [console::new "Merge" "merge $stitle"]
119 console::exec $cons $cmd [cb _finish $cons]
121 wm protocol $w WM_DELETE_WINDOW {}
122 destroy $w
125 method _finish {cons ok} {
126 console::done $cons $ok
127 if {$ok} {
128 set msg {Merge completed successfully.}
129 } else {
130 set msg {Merge failed. Conflict resolution is required.}
132 unlock_index
133 rescan [list ui_status $msg]
134 delete_this
137 constructor dialog {} {
138 global current_branch
139 global M1B
141 if {![_can_merge $this]} {
142 delete_this
143 return
146 make_toplevel top w
147 wm title $top "[appname] ([reponame]): Merge"
148 if {$top ne {.}} {
149 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
152 set _start [cb _start]
154 label $w.header \
155 -text "Merge Into $current_branch" \
156 -font font_uibold
157 pack $w.header -side top -fill x
159 frame $w.buttons
160 button $w.buttons.visualize \
161 -text Visualize \
162 -command [cb _visualize]
163 pack $w.buttons.visualize -side left
164 button $w.buttons.merge \
165 -text Merge \
166 -command $_start
167 pack $w.buttons.merge -side right
168 button $w.buttons.cancel \
169 -text {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
174 set w_rev [::choose_rev::new_unmerged $w.rev {Revision To Merge}]
175 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
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]
182 bind $w.buttons.merge <Visibility> [cb _visible]
183 tkwait window $w
186 method _visible {} {
187 grab $w
188 if {[is_config_true gui.matchtrackingbranch]} {
189 $w_rev pick_tracking_branch
191 $w_rev focus_filter
194 method _cancel {} {
195 wm protocol $w WM_DELETE_WINDOW {}
196 unlock_index
197 destroy $w
198 delete_this
203 namespace eval merge {
205 proc reset_hard {} {
206 global HEAD commit_type file_states
208 if {[string match amend* $commit_type]} {
209 info_popup {Cannot abort while amending.
211 You must finish amending this commit.
213 return
216 if {![lock_index abort]} return
218 if {[string match *merge* $commit_type]} {
219 set op merge
220 } else {
221 set op commit
224 if {[ask_popup "Abort $op?
226 Aborting the current $op will cause *ALL* uncommitted changes to be lost.
228 Continue with aborting the current $op?"] eq {yes}} {
229 set fd [git_read read-tree --reset -u HEAD]
230 fconfigure $fd -blocking 0 -translation binary
231 fileevent $fd readable [namespace code [list _reset_wait $fd]]
232 ui_status {Aborting... please wait...}
233 } else {
234 unlock_index
238 proc _reset_wait {fd} {
239 global ui_comm
241 read $fd
242 if {[eof $fd]} {
243 close $fd
244 unlock_index
246 $ui_comm delete 0.0 end
247 $ui_comm edit modified false
249 catch {file delete [gitdir MERGE_HEAD]}
250 catch {file delete [gitdir rr-cache MERGE_RR]}
251 catch {file delete [gitdir SQUASH_MSG]}
252 catch {file delete [gitdir MERGE_MSG]}
253 catch {file delete [gitdir GITGUI_MSG]}
255 rescan {ui_status {Abort completed. Ready.}}