blob: 148d859c5c11022f68c4c4217953ac1cd3f1f8f1 [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]} {
13 info_popup {Cannot merge while amending.
14
15You 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 {Last scanned state does not match repository state.
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.
32}
33 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 }
43 U? {
44 error_popup "You are in the middle of a conflicted merge.
45
46File [short_path $path] has merge conflicts.
47
48You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
49"
50 unlock_index
51 return 0
52 }
53 ?? {
54 error_popup "You are in the middle of a change.
55
56File [short_path $path] is modified.
57
58You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
59"
60 unlock_index
61 return 0
62 }
63 }
64 }
65
66 return 1
67}
68
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -040069method _rev {} {
Shawn O. Pearce350a35f2007-07-25 03:44:50 -040070 if {[catch {$w_rev commit_or_die}]} {
71 return {}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040072 }
Shawn O. Pearce350a35f2007-07-25 03:44:50 -040073 return [$w_rev get]
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -040074}
75
Shawn O. Pearceff749c12007-07-18 02:56:44 -040076method _visualize {} {
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -040077 set rev [_rev $this]
78 if {$rev ne {}} {
79 do_gitk [list $rev --not HEAD]
80 }
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040081}
82
Shawn O. Pearceff749c12007-07-18 02:56:44 -040083method _start {} {
Shawn O. Pearceead49f52007-07-25 04:54:53 -040084 global HEAD current_branch remote_url
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040085
Shawn O. Pearce5dc2cae2007-07-19 02:24:25 -040086 set name [_rev $this]
87 if {$name eq {}} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040088 return
89 }
90
Shawn O. Pearceead49f52007-07-25 04:54:53 -040091 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
96
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])
Shawn O. Pearcebc318ea2007-07-25 05:02:38 -0400105 if {[regexp {^[^:@]*@[^:]*:/} $remote]} {
106 regsub {^[^:@]*@} $remote {} remote
107 }
Shawn O. Pearceead49f52007-07-25 04:54:53 -0400108 set branch [lindex $spec 2]
109 set stitle "$branch of $remote"
110 }
111 regsub ^refs/heads/ $branch {} branch
112 puts $fh "$cmit\t\tbranch '$branch' of $remote"
113 close $fh
114
115 lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
116 lappend cmd HEAD
117 lappend cmd $cmit
118
119 set msg "Merging $current_branch and $stitle"
Shawn O. Pearce699d5602007-07-05 23:16:13 -0400120 ui_status "$msg..."
Shawn O. Pearceead49f52007-07-25 04:54:53 -0400121 set cons [console::new "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} {
131 set msg {Merge completed successfully.}
132 } else {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400133 set msg {Merge failed. Conflict resolution is required.}
134 }
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
Shawn O. Pearceebcaada2007-05-05 02:28:41 -0400142 global M1B
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
Shawn O. Pearceff749c12007-07-18 02:56:44 -0400149 make_toplevel top w
150 wm title $top "[appname] ([reponame]): Merge"
151 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
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -0400157 label $w.header \
158 -text "Merge Into $current_branch" \
159 -font font_uibold
160 pack $w.header -side top -fill x
161
162 frame $w.buttons
Shawn O. Pearce9feefbd2007-07-25 04:32:18 -0400163 button $w.buttons.visualize \
164 -text Visualize \
165 -command [cb _visualize]
Shawn O. Pearce1fc4ba82007-05-03 18:21:39 -0400166 pack $w.buttons.visualize -side left
Shawn O. Pearce9feefbd2007-07-25 04:32:18 -0400167 button $w.buttons.merge \
168 -text Merge \
169 -command $_start
170 pack $w.buttons.merge -side right
Shawn O. Pearcec8e23aa2007-07-04 02:29:32 -0400171 button $w.buttons.cancel \
172 -text {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
Shawn O. Pearce350a35f2007-07-25 03:44:50 -0400177 set w_rev [::choose_rev::new_unmerged $w.rev {Revision To Merge}]
178 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]} {
212 info_popup {Cannot abort while amending.
213
214You must finish amending this commit.
215}
216 return
217 }
218
219 if {![lock_index abort]} return
220
221 if {[string match *merge* $commit_type]} {
222 set op merge
223 } else {
224 set op commit
225 }
226
227 if {[ask_popup "Abort $op?
228
229Aborting the current $op will cause *ALL* uncommitted changes to be lost.
230
231Continue with aborting the current $op?"] eq {yes}} {
Shawn O. Pearce0b812612007-07-09 01:17:09 -0400232 set fd [git_read read-tree --reset -u HEAD]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400233 fconfigure $fd -blocking 0 -translation binary
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400234 fileevent $fd readable [namespace code [list _reset_wait $fd]]
Shawn O. Pearce699d5602007-07-05 23:16:13 -0400235 ui_status {Aborting... please wait...}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400236 } else {
237 unlock_index
238 }
239}
240
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400241proc _reset_wait {fd} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400242 global ui_comm
243
244 read $fd
245 if {[eof $fd]} {
246 close $fd
247 unlock_index
248
249 $ui_comm delete 0.0 end
250 $ui_comm edit modified false
251
252 catch {file delete [gitdir MERGE_HEAD]}
253 catch {file delete [gitdir rr-cache MERGE_RR]}
254 catch {file delete [gitdir SQUASH_MSG]}
255 catch {file delete [gitdir MERGE_MSG]}
256 catch {file delete [gitdir GITGUI_MSG]}
257
Shawn O. Pearce699d5602007-07-05 23:16:13 -0400258 rescan {ui_status {Abort completed. Ready.}}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400259 }
260}
Shawn O. Pearcea6c9b082007-05-02 13:56:27 -0400261
262}