1 # git-gui branch merge support
2 # Copyright (C) 2006, 2007 Shawn Pearce
7 global HEAD commit_type file_states
9 if {[string match amend
* $commit_type]} {
10 info_popup
{Cannot merge
while amending.
12 You must finish amending this commit before starting any type of merge.
17 if {[committer_ident
] eq
{}} {return 0}
18 if {![lock_index merge
]} {return 0}
20 # -- Our in memory state should match the repository.
22 repository_state curType curHEAD curMERGE_HEAD
23 if {$commit_type ne
$curType ||
$HEAD ne
$curHEAD} {
24 info_popup
{Last scanned state does not match repository state.
26 Another Git program has modified this repository since the last
scan. A rescan must be performed before a merge can be performed.
28 The rescan will be automatically started now.
35 foreach path
[array names file_states
] {
36 switch -glob -- [lindex $file_states($path) 0] {
38 continue; # and pray it works!
41 error_popup
"You are in the middle of a conflicted merge.
43 File [short_path $path] has merge conflicts.
45 You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
51 error_popup
"You are in the middle of a change.
53 File [short_path $path] is modified.
55 You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
68 foreach i
[$w.
source.l curselection
] {
69 lappend r
[lindex [lindex $list $i] 0]
74 proc _visualize
{w
list} {
75 set revs
[_refs
$w $list]
76 if {$revs eq
{}} return
77 lappend revs
--not HEAD
81 proc _start
{w
list} {
82 global HEAD current_branch
84 set cmd
[list git merge
]
85 set names
[_refs
$w $list]
86 set revcnt
[llength $names]
91 } elseif
{$revcnt == 1} {
93 } elseif
{$revcnt <= 15} {
99 "Use octopus merge strategy?
101 You are merging $revcnt branches at once. This requires using the octopus merge driver, which may not succeed if there are file-level conflicts.
112 -title [wm title
$w] \
114 -message "Too many branches selected.
116 You have requested to merge $revcnt branches in an octopus merge. This exceeds Git's internal limit of 15 branches per merge.
118 Please select fewer branches. To merge more than 15 branches, merge the branches in batches.
123 set msg
"Merging $current_branch, [join $names {, }]"
125 set cons
[console::new "Merge" $msg]
126 console::exec $cons $cmd \
127 [namespace code
[list _finish
$revcnt $cons]]
129 wm protocol
$w WM_DELETE_WINDOW
{}
133 proc _finish
{revcnt w ok
} {
136 set msg
{Merge completed successfully.
}
139 info_popup
"Octopus merge failed.
141 Your merge of $revcnt branches has failed.
143 There are file-level conflicts between the branches which must be resolved manually.
145 The working directory will now be reset.
147 You can attempt this merge again by merging only one branch at a time." $w
149 set fd
[git_read read-tree
--reset -u HEAD
]
150 fconfigure $fd -blocking 0 -translation binary
151 fileevent $fd readable
\
152 [namespace code
[list _reset_wait
$fd]]
153 ui_status
{Aborting... please wait...
}
157 set msg
{Merge failed. Conflict resolution is required.
}
160 rescan
[list ui_status
$msg]
164 global current_branch
167 if {![_can_merge
]} return
169 set fmt
{list %(objectname
) %(*objectname
) %(refname
) %(subject
)}
170 set fr_fd
[git_read for-each-ref
\
177 fconfigure $fr_fd -translation binary
178 while {[gets $fr_fd line
] > 0} {
179 set line
[eval $line]
180 set ref
[lindex $line 2]
181 regsub ^refs
/(heads|remotes|tags
)/ $ref {} ref
182 set subj
($ref) [lindex $line 3]
183 lappend sha1
([lindex $line 0]) $ref
184 if {[lindex $line 1] ne
{}} {
185 lappend sha1
([lindex $line 1]) $ref
191 set fr_fd
[git_read rev-list
--all --not HEAD
]
192 while {[gets $fr_fd line
] > 0} {
193 if {[catch {set ref
$sha1($line)}]} continue
195 lappend to_show
[list $n $line]
199 set to_show
[lsort -unique $to_show]
203 wm geometry
$w "+[winfo rootx .]+[winfo rooty .]"
205 set _visualize
[namespace code
[list _visualize
$w $to_show]]
206 set _start
[namespace code
[list _start
$w $to_show]]
209 -text "Merge Into $current_branch" \
211 pack $w.header
-side top
-fill x
214 button $w.buttons.visualize
-text Visualize
-command $_visualize
215 pack $w.buttons.visualize
-side left
216 button $w.buttons.create
-text Merge
-command $_start
217 pack $w.buttons.create
-side right
218 button $w.buttons.cancel
\
220 -command "unlock_index;destroy $w"
221 pack $w.buttons.cancel
-side right
-padx 5
222 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
224 labelframe $w.
source -text {Source Branches
}
225 listbox $w.
source.l
\
229 -selectmode extended
\
230 -yscrollcommand [list $w.
source.sby
set]
231 scrollbar $w.
source.sby
-command [list $w.
source.l yview
]
232 pack $w.
source.sby
-side right
-fill y
233 pack $w.
source.l
-side left
-fill both
-expand 1
234 pack $w.
source -fill both
-expand 1 -pady 5 -padx 5
236 foreach ref
$to_show {
237 set n
[lindex $ref 0]
238 if {[string length
$n] > 20} {
239 set n
"[string range $n 0 16]..."
241 $w.
source.l insert end
[format {%s
%-20s %s
} \
242 [string range
[lindex $ref 1] 0 5] \
244 $subj([lindex $ref 0])]
247 bind $w.
source.l
<Key-K
> [list event generate
%W
<Shift-Key-Up
>]
248 bind $w.
source.l
<Key-J
> [list event generate
%W
<Shift-Key-Down
>]
249 bind $w.
source.l
<Key-k
> [list event generate
%W
<Key-Up
>]
250 bind $w.
source.l
<Key-j
> [list event generate
%W
<Key-Down
>]
251 bind $w.
source.l
<Key-h
> [list event generate
%W
<Key-Left
>]
252 bind $w.
source.l
<Key-l
> [list event generate
%W
<Key-Right
>]
253 bind $w.
source.l
<Key-v
> $_visualize
255 bind $w <$M1B-Key
-Return
> $_start
256 bind $w <Visibility
> "grab $w; focus $w.source.l"
257 bind $w <Key-Escape
> "unlock_index;destroy $w"
258 wm protocol
$w WM_DELETE_WINDOW
"unlock_index;destroy $w"
259 wm title
$w "[appname] ([reponame]): Merge"
264 global HEAD commit_type file_states
266 if {[string match amend
* $commit_type]} {
267 info_popup
{Cannot abort
while amending.
269 You must finish amending this commit.
274 if {![lock_index abort
]} return
276 if {[string match
*merge
* $commit_type]} {
282 if {[ask_popup
"Abort $op?
284 Aborting the current $op will cause *ALL* uncommitted changes to be lost.
286 Continue with aborting the current $op?"] eq
{yes
}} {
287 set fd
[git_read read-tree
--reset -u HEAD
]
288 fconfigure $fd -blocking 0 -translation binary
289 fileevent $fd readable
[namespace code
[list _reset_wait
$fd]]
290 ui_status
{Aborting... please wait...
}
296 proc _reset_wait
{fd
} {
304 $ui_comm delete
0.0 end
305 $ui_comm edit modified false
307 catch {file delete
[gitdir MERGE_HEAD
]}
308 catch {file delete
[gitdir rr-cache MERGE_RR
]}
309 catch {file delete
[gitdir SQUASH_MSG
]}
310 catch {file delete
[gitdir MERGE_MSG
]}
311 catch {file delete
[gitdir GITGUI_MSG
]}
313 rescan
{ui_status
{Abort completed. Ready.
}}