1 # git-gui branch merge support
2 # Copyright (C) 2006, 2007 Shawn Pearce
5 global HEAD commit_type file_states
7 if {[string match amend
* $commit_type]} {
8 info_popup
{Cannot merge
while amending.
10 You must finish amending this commit before starting any type of merge.
15 if {[committer_ident
] eq
{}} {return 0}
16 if {![lock_index merge
]} {return 0}
18 # -- Our in memory state should match the repository.
20 repository_state curType curHEAD curMERGE_HEAD
21 if {$commit_type ne
$curType ||
$HEAD ne
$curHEAD} {
22 info_popup
{Last scanned state does not match repository state.
24 Another Git program has modified this repository since the last
scan. A rescan must be performed before a merge can be performed.
26 The rescan will be automatically started now.
29 rescan
{set ui_status_value
{Ready.
}}
33 foreach path
[array names file_states
] {
34 switch -glob -- [lindex $file_states($path) 0] {
36 continue; # and pray it works!
39 error_popup
"You are in the middle of a conflicted merge.
41 File [short_path $path] has merge conflicts.
43 You must resolve them, add the file, and commit to complete the current merge. Only then can you begin another merge.
49 error_popup
"You are in the middle of a change.
51 File [short_path $path] is modified.
53 You should complete the current commit before starting a merge. Doing so will help you abort a failed merge, should the need arise.
64 proc visualize_local_merge
{w
} {
66 foreach i
[$w.
source.l curselection
] {
67 lappend revs
[$w.
source.l get
$i]
69 if {$revs eq
{}} return
70 lappend revs
--not HEAD
74 proc start_local_merge_action
{w
} {
75 global HEAD ui_status_value current_branch
77 set cmd
[list git merge
]
80 foreach i
[$w.
source.l curselection
] {
81 set b
[$w.
source.l get
$i]
89 } elseif
{$revcnt == 1} {
91 } elseif
{$revcnt <= 15} {
97 "Use octopus merge strategy?
99 You are merging $revcnt branches at once. This requires using the octopus merge driver, which may not succeed if there are file-level conflicts.
110 -title [wm title
$w] \
112 -message "Too many branches selected.
114 You have requested to merge $revcnt branches in an octopus merge. This exceeds Git's internal limit of 15 branches per merge.
116 Please select fewer branches. To merge more than 15 branches, merge the branches in batches.
121 set msg
"Merging $current_branch, [join $names {, }]"
122 set ui_status_value
"$msg..."
123 set cons
[console::new "Merge" $msg]
124 console::exec $cons $cmd [list finish_merge
$revcnt]
129 proc finish_merge
{revcnt w ok
} {
132 set msg
{Merge completed successfully.
}
135 info_popup
"Octopus merge failed.
137 Your merge of $revcnt branches has failed.
139 There are file-level conflicts between the branches which must be resolved manually.
141 The working directory will now be reset.
143 You can attempt this merge again by merging only one branch at a time." $w
145 set fd
[open "| git read-tree --reset -u HEAD" r
]
146 fconfigure $fd -blocking 0 -translation binary
147 fileevent $fd readable
[list reset_hard_wait
$fd]
148 set ui_status_value
{Aborting... please wait...
}
152 set msg
{Merge failed. Conflict resolution is required.
}
155 rescan
[list set ui_status_value
$msg]
158 proc do_local_merge
{} {
159 global current_branch
161 if {![can_merge
]} return
165 wm geometry
$w "+[winfo rootx .]+[winfo rooty .]"
168 -text "Merge Into $current_branch" \
170 pack $w.header
-side top
-fill x
173 button $w.buttons.visualize
-text Visualize
\
174 -command [list visualize_local_merge
$w]
175 pack $w.buttons.visualize
-side left
176 button $w.buttons.create
-text Merge
\
177 -command [list start_local_merge_action
$w]
178 pack $w.buttons.create
-side right
179 button $w.buttons.cancel
-text {Cancel
} \
180 -command [list destroy $w]
181 pack $w.buttons.cancel
-side right
-padx 5
182 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
184 labelframe $w.
source -text {Source Branches
}
185 listbox $w.
source.l
\
188 -selectmode extended
\
189 -yscrollcommand [list $w.
source.sby
set]
190 scrollbar $w.
source.sby
-command [list $w.
source.l yview
]
191 pack $w.
source.sby
-side right
-fill y
192 pack $w.
source.l
-side left
-fill both
-expand 1
193 pack $w.
source -fill both
-expand 1 -pady 5 -padx 5
195 set cmd
[list git for-each-ref
]
196 lappend cmd
{--format=%(objectname
) %(*objectname
) %(refname
)}
197 lappend cmd refs
/heads
198 lappend cmd refs
/remotes
199 lappend cmd refs
/tags
200 set fr_fd
[open "| $cmd" r
]
201 fconfigure $fr_fd -translation binary
202 while {[gets $fr_fd line
] > 0} {
203 set line
[split $line { }]
204 set sha1
([lindex $line 0]) [lindex $line 2]
205 set sha1
([lindex $line 1]) [lindex $line 2]
210 set fr_fd
[open "| git rev-list --all --not HEAD"]
211 while {[gets $fr_fd line
] > 0} {
212 if {[catch {set ref
$sha1($line)}]} continue
213 regsub ^refs
/(heads|remotes|tags
)/ $ref {} ref
218 foreach ref
[lsort -unique $to_show] {
219 $w.
source.l insert end
$ref
222 bind $w <Visibility
> "grab $w"
223 bind $w <Key-Escape
> "unlock_index;destroy $w"
224 bind $w <Destroy
> unlock_index
225 wm title
$w "[appname] ([reponame]): Merge"
229 proc do_reset_hard
{} {
230 global HEAD commit_type file_states
232 if {[string match amend
* $commit_type]} {
233 info_popup
{Cannot abort
while amending.
235 You must finish amending this commit.
240 if {![lock_index abort
]} return
242 if {[string match
*merge
* $commit_type]} {
248 if {[ask_popup
"Abort $op?
250 Aborting the current $op will cause *ALL* uncommitted changes to be lost.
252 Continue with aborting the current $op?"] eq
{yes
}} {
253 set fd
[open "| git read-tree --reset -u HEAD" r
]
254 fconfigure $fd -blocking 0 -translation binary
255 fileevent $fd readable
[list reset_hard_wait
$fd]
256 set ui_status_value
{Aborting... please wait...
}
262 proc reset_hard_wait
{fd
} {
270 $ui_comm delete
0.0 end
271 $ui_comm edit modified false
273 catch {file delete
[gitdir MERGE_HEAD
]}
274 catch {file delete
[gitdir rr-cache MERGE_RR
]}
275 catch {file delete
[gitdir SQUASH_MSG
]}
276 catch {file delete
[gitdir MERGE_MSG
]}
277 catch {file delete
[gitdir GITGUI_MSG
]}
279 rescan
{set ui_status_value
{Abort completed. Ready.
}}