Code

Merge git://repo.or.cz/git-gui
[git.git] / git-gui / lib / merge.tcl
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 [mc "Cannot merge while amending.
15 You must finish amending this commit before starting any type of merge.
16 "]
17                 return 0
18         }
20         if {[committer_ident] eq {}} {return 0}
21         if {![lock_index merge]} {return 0}
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.
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.
32 "]
33                 unlock_index
34                 rescan ui_ready
35                 return 0
36         }
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.
47 File %s has merge conflicts.
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.
57 File %s is modified.
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         }
67         return 1
68 }
70 method _rev {} {
71         if {[catch {$w_rev commit_or_die}]} {
72                 return {}
73         }
74         return [$w_rev get]
75 }
77 method _visualize {} {
78         set rev [_rev $this]
79         if {$rev ne {}} {
80                 do_gitk [list $rev --not HEAD]
81         }
82 }
84 method _start {} {
85         global HEAD current_branch remote_url
87         set name [_rev $this]
88         if {$name eq {}} {
89                 return
90         }
92         set spec [$w_rev get_tracking_branch]
93         set cmit [$w_rev get_commit]
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])
103                 if {[regexp {^[^:@]*@[^:]*:/} $remote]} {
104                         regsub {^[^:@]*@} $remote {} remote
105                 }
106                 set branch [lindex $spec 2]
107                 set stitle [mc "%s of %s" $branch $remote]
108         }
109         regsub ^refs/heads/ $branch {} branch
110         puts $fh "$cmit\t\tbranch '$branch' of $remote"
111         close $fh
113         set cmd [list git]
114         lappend cmd merge
115         lappend cmd --strategy=recursive
116         lappend cmd [git fmt-merge-msg <[gitdir FETCH_HEAD]]
117         lappend cmd HEAD
118         lappend cmd $name
120         ui_status [mc "Merging %s and %s..." $current_branch $stitle]
121         set cons [console::new [mc "Merge"] "merge $stitle"]
122         console::exec $cons $cmd [cb _finish $cons]
124         wm protocol $w WM_DELETE_WINDOW {}
125         destroy $w
128 method _finish {cons ok} {
129         console::done $cons $ok
130         if {$ok} {
131                 set msg [mc "Merge completed successfully."]
132         } else {
133                 set msg [mc "Merge failed.  Conflict resolution is required."]
134         }
135         unlock_index
136         rescan [list ui_status $msg]
137         delete_this
140 constructor dialog {} {
141         global current_branch
142         global M1B use_ttk NS
144         if {![_can_merge $this]} {
145                 delete_this
146                 return
147         }
149         make_dialog top w
150         wm title $top [append "[appname] ([reponame]): " [mc "Merge"]]
151         if {$top ne {.}} {
152                 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
153         }
155         set _start [cb _start]
157         ${NS}::label $w.header \
158                 -text [mc "Merge Into %s" $current_branch] \
159                 -font font_uibold
160         pack $w.header -side top -fill x
162         ${NS}::frame $w.buttons
163         ${NS}::button $w.buttons.visualize \
164                 -text [mc Visualize] \
165                 -command [cb _visualize]
166         pack $w.buttons.visualize -side left
167         ${NS}::button $w.buttons.merge \
168                 -text [mc Merge] \
169                 -command $_start
170         pack $w.buttons.merge -side right
171         ${NS}::button $w.buttons.cancel \
172                 -text [mc "Cancel"] \
173                 -command [cb _cancel]
174         pack $w.buttons.cancel -side right -padx 5
175         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
177         set w_rev [::choose_rev::new_unmerged $w.rev [mc "Revision To Merge"]]
178         pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
180         bind $w <$M1B-Key-Return> $_start
181         bind $w <Key-Return> $_start
182         bind $w <Key-Escape> [cb _cancel]
183         wm protocol $w WM_DELETE_WINDOW [cb _cancel]
185         bind $w.buttons.merge <Visibility> [cb _visible]
186         tkwait window $w
189 method _visible {} {
190         grab $w
191         if {[is_config_true gui.matchtrackingbranch]} {
192                 $w_rev pick_tracking_branch
193         }
194         $w_rev focus_filter
197 method _cancel {} {
198         wm protocol $w WM_DELETE_WINDOW {}
199         unlock_index
200         destroy $w
201         delete_this
206 namespace eval merge {
208 proc reset_hard {} {
209         global HEAD commit_type file_states
211         if {[string match amend* $commit_type]} {
212                 info_popup [mc "Cannot abort while amending.
214 You must finish amending this commit.
215 "]
216                 return
217         }
219         if {![lock_index abort]} return
221         if {[string match *merge* $commit_type]} {
222                 set op_question [mc "Abort merge?
224 Aborting the current merge will cause *ALL* uncommitted changes to be lost.
226 Continue with aborting the current merge?"]
227         } else {
228                 set op_question [mc "Reset changes?
230 Resetting the changes will cause *ALL* uncommitted changes to be lost.
232 Continue with resetting the current changes?"]
233         }
235         if {[ask_popup $op_question] eq {yes}} {
236                 set fd [git_read --stderr read-tree --reset -u -v HEAD]
237                 fconfigure $fd -blocking 0 -translation binary
238                 fileevent $fd readable [namespace code [list _reset_wait $fd]]
239                 $::main_status start [mc "Aborting"] [mc "files reset"]
240         } else {
241                 unlock_index
242         }
245 proc _reset_wait {fd} {
246         global ui_comm
248         $::main_status update_meter [read $fd]
250         fconfigure $fd -blocking 1
251         if {[eof $fd]} {
252                 set fail [catch {close $fd} err]
253                 $::main_status stop
254                 unlock_index
256                 $ui_comm delete 0.0 end
257                 $ui_comm edit modified false
259                 catch {file delete [gitdir MERGE_HEAD]}
260                 catch {file delete [gitdir rr-cache MERGE_RR]}
261                 catch {file delete [gitdir MERGE_RR]}
262                 catch {file delete [gitdir SQUASH_MSG]}
263                 catch {file delete [gitdir MERGE_MSG]}
264                 catch {file delete [gitdir GITGUI_MSG]}
266                 if {$fail} {
267                         warn_popup "[mc "Abort failed."]\n\n$err"
268                 }
269                 rescan {ui_status [mc "Abort completed.  Ready."]}
270         } else {
271                 fconfigure $fd -blocking 0
272         }