]>
Commit | Line | Data |
---|---|---|
d41b43eb SP |
1 | # git-gui commit checkout support |
2 | # Copyright (C) 2007 Shawn Pearce | |
3 | ||
4 | class checkout_op { | |
5 | ||
6 | field w {}; # our window (if we have one) | |
7 | field w_cons {}; # embedded console window object | |
8 | ||
9 | field new_expr ; # expression the user saw/thinks this is | |
10 | field new_hash ; # commit SHA-1 we are switching to | |
11 | field new_ref ; # ref we are updating/creating | |
12 | ||
13 | field parent_w .; # window that started us | |
14 | field merge_type none; # type of merge to apply to existing branch | |
60f7352f | 15 | field merge_base {}; # merge base if we have another ref involved |
d41b43eb SP |
16 | field fetch_spec {}; # refetch tracking branch if used? |
17 | field checkout 1; # actually checkout the branch? | |
18 | field create 0; # create the branch if it doesn't exist? | |
19 | ||
20 | field reset_ok 0; # did the user agree to reset? | |
21 | field fetch_ok 0; # did the fetch succeed? | |
22 | ||
b7922306 | 23 | field readtree_d {}; # buffered output from read-tree |
d41b43eb SP |
24 | field update_old {}; # was the update-ref call deferred? |
25 | field reflog_msg {}; # log message for the update-ref call | |
26 | ||
27 | constructor new {expr hash {ref {}}} { | |
28 | set new_expr $expr | |
29 | set new_hash $hash | |
30 | set new_ref $ref | |
31 | ||
32 | return $this | |
33 | } | |
34 | ||
35 | method parent {path} { | |
36 | set parent_w [winfo toplevel $path] | |
37 | } | |
38 | ||
39 | method enable_merge {type} { | |
40 | set merge_type $type | |
41 | } | |
42 | ||
43 | method enable_fetch {spec} { | |
44 | set fetch_spec $spec | |
45 | } | |
46 | ||
47 | method enable_checkout {co} { | |
48 | set checkout $co | |
49 | } | |
50 | ||
51 | method enable_create {co} { | |
52 | set create $co | |
53 | } | |
54 | ||
55 | method run {} { | |
56 | if {$fetch_spec ne {}} { | |
57 | global M1B | |
58 | ||
59 | # We were asked to refresh a single tracking branch | |
60 | # before we get to work. We should do that before we | |
61 | # consider any ref updating. | |
62 | # | |
63 | set fetch_ok 0 | |
64 | set l_trck [lindex $fetch_spec 0] | |
65 | set remote [lindex $fetch_spec 1] | |
66 | set r_head [lindex $fetch_spec 2] | |
67 | regsub ^refs/heads/ $r_head {} r_name | |
68 | ||
54febd4f SP |
69 | set cmd [list git fetch $remote] |
70 | if {$l_trck ne {}} { | |
71 | lappend cmd +$r_head:$l_trck | |
72 | } else { | |
73 | lappend cmd $r_head | |
74 | } | |
75 | ||
d41b43eb SP |
76 | _toplevel $this {Refreshing Tracking Branch} |
77 | set w_cons [::console::embed \ | |
78 | $w.console \ | |
79 | "Fetching $r_name from $remote"] | |
80 | pack $w.console -fill both -expand 1 | |
54febd4f | 81 | $w_cons exec $cmd [cb _finish_fetch] |
d41b43eb SP |
82 | |
83 | bind $w <$M1B-Key-w> break | |
84 | bind $w <$M1B-Key-W> break | |
85 | bind $w <Visibility> " | |
86 | [list grab $w] | |
87 | [list focus $w] | |
88 | " | |
89 | wm protocol $w WM_DELETE_WINDOW [cb _noop] | |
90 | tkwait window $w | |
91 | ||
92 | if {!$fetch_ok} { | |
93 | delete_this | |
94 | return 0 | |
95 | } | |
96 | } | |
97 | ||
98 | if {$new_ref ne {}} { | |
99 | # If we have a ref we need to update it before we can | |
100 | # proceed with a checkout (if one was enabled). | |
101 | # | |
102 | if {![_update_ref $this]} { | |
103 | delete_this | |
104 | return 0 | |
105 | } | |
106 | } | |
107 | ||
108 | if {$checkout} { | |
109 | _checkout $this | |
110 | return 1 | |
111 | } | |
112 | ||
113 | delete_this | |
114 | return 1 | |
115 | } | |
116 | ||
117 | method _noop {} {} | |
118 | ||
119 | method _finish_fetch {ok} { | |
120 | if {$ok} { | |
121 | set l_trck [lindex $fetch_spec 0] | |
54febd4f SP |
122 | if {$l_trck eq {}} { |
123 | set l_trck FETCH_HEAD | |
124 | } | |
d41b43eb SP |
125 | if {[catch {set new_hash [git rev-parse --verify "$l_trck^0"]} err]} { |
126 | set ok 0 | |
127 | $w_cons insert "fatal: Cannot resolve $l_trck" | |
128 | $w_cons insert $err | |
129 | } | |
130 | } | |
131 | ||
132 | $w_cons done $ok | |
133 | set w_cons {} | |
134 | wm protocol $w WM_DELETE_WINDOW {} | |
135 | ||
136 | if {$ok} { | |
137 | destroy $w | |
138 | set w {} | |
139 | } else { | |
140 | button $w.close -text Close -command [list destroy $w] | |
141 | pack $w.close -side bottom -anchor e -padx 10 -pady 10 | |
142 | } | |
143 | ||
144 | set fetch_ok $ok | |
145 | } | |
146 | ||
147 | method _update_ref {} { | |
148 | global null_sha1 current_branch | |
149 | ||
150 | set ref $new_ref | |
151 | set new $new_hash | |
152 | ||
153 | set is_current 0 | |
154 | set rh refs/heads/ | |
155 | set rn [string length $rh] | |
156 | if {[string equal -length $rn $rh $ref]} { | |
157 | set newbranch [string range $ref $rn end] | |
158 | if {$current_branch eq $newbranch} { | |
159 | set is_current 1 | |
160 | } | |
161 | } else { | |
162 | set newbranch $ref | |
163 | } | |
164 | ||
165 | if {[catch {set cur [git rev-parse --verify "$ref^0"]}]} { | |
166 | # Assume it does not exist, and that is what the error was. | |
167 | # | |
168 | if {!$create} { | |
169 | _error $this "Branch '$newbranch' does not exist." | |
170 | return 0 | |
171 | } | |
172 | ||
173 | set reflog_msg "branch: Created from $new_expr" | |
174 | set cur $null_sha1 | |
175 | } elseif {$create && $merge_type eq {none}} { | |
176 | # We were told to create it, but not do a merge. | |
177 | # Bad. Name shouldn't have existed. | |
178 | # | |
179 | _error $this "Branch '$newbranch' already exists." | |
180 | return 0 | |
181 | } elseif {!$create && $merge_type eq {none}} { | |
182 | # We aren't creating, it exists and we don't merge. | |
183 | # We are probably just a simple branch switch. | |
184 | # Use whatever value we just read. | |
185 | # | |
186 | set new $cur | |
187 | set new_hash $cur | |
188 | } elseif {$new eq $cur} { | |
189 | # No merge would be required, don't compute anything. | |
190 | # | |
191 | } else { | |
60f7352f | 192 | catch {set merge_base [git merge-base $new $cur]} |
f66b8a68 SP |
193 | if {$merge_base eq $cur} { |
194 | # The current branch is older. | |
195 | # | |
196 | set reflog_msg "merge $new_expr: Fast-forward" | |
197 | } else { | |
198 | switch -- $merge_type { | |
199 | ff { | |
200 | if {$merge_base eq $new} { | |
201 | # The current branch is actually newer. | |
202 | # | |
203 | set new $cur | |
204 | } else { | |
205 | _error $this "Branch '$newbranch' already exists.\n\nIt cannot fast-forward to $new_expr.\nA merge is required." | |
206 | return 0 | |
207 | } | |
d41b43eb | 208 | } |
f66b8a68 | 209 | reset { |
d41b43eb SP |
210 | # The current branch will lose things. |
211 | # | |
212 | if {[_confirm_reset $this $cur]} { | |
213 | set reflog_msg "reset $new_expr" | |
214 | } else { | |
215 | return 0 | |
216 | } | |
217 | } | |
f66b8a68 | 218 | default { |
eea1ab6e | 219 | _error $this "Merge strategy '$merge_type' not supported." |
f66b8a68 SP |
220 | return 0 |
221 | } | |
222 | } | |
d41b43eb SP |
223 | } |
224 | } | |
225 | ||
226 | if {$new ne $cur} { | |
227 | if {$is_current} { | |
228 | # No so fast. We should defer this in case | |
229 | # we cannot update the working directory. | |
230 | # | |
231 | set update_old $cur | |
232 | return 1 | |
233 | } | |
234 | ||
235 | if {[catch { | |
236 | git update-ref -m $reflog_msg $ref $new $cur | |
237 | } err]} { | |
238 | _error $this "Failed to update '$newbranch'.\n\n$err" | |
239 | return 0 | |
240 | } | |
241 | } | |
242 | ||
243 | return 1 | |
244 | } | |
245 | ||
246 | method _checkout {} { | |
247 | if {[lock_index checkout_op]} { | |
248 | after idle [cb _start_checkout] | |
249 | } else { | |
250 | _error $this "Index is already locked." | |
251 | delete_this | |
252 | } | |
253 | } | |
254 | ||
255 | method _start_checkout {} { | |
256 | global HEAD commit_type | |
257 | ||
258 | # -- Our in memory state should match the repository. | |
259 | # | |
260 | repository_state curType curHEAD curMERGE_HEAD | |
261 | if {[string match amend* $commit_type] | |
262 | && $curType eq {normal} | |
263 | && $curHEAD eq $HEAD} { | |
264 | } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} { | |
265 | info_popup {Last scanned state does not match repository state. | |
266 | ||
267 | Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed. | |
268 | ||
269 | The rescan will be automatically started now. | |
270 | } | |
271 | unlock_index | |
272 | rescan ui_ready | |
273 | delete_this | |
274 | return | |
275 | } | |
276 | ||
dba07411 SP |
277 | if {$curHEAD eq $new_hash} { |
278 | _after_readtree $this | |
279 | } elseif {[is_config_true gui.trustmtime]} { | |
d41b43eb SP |
280 | _readtree $this |
281 | } else { | |
282 | ui_status {Refreshing file status...} | |
0b812616 SP |
283 | set fd [git_read update-index \ |
284 | -q \ | |
285 | --unmerged \ | |
286 | --ignore-missing \ | |
287 | --refresh \ | |
288 | ] | |
d41b43eb SP |
289 | fconfigure $fd -blocking 0 -translation binary |
290 | fileevent $fd readable [cb _refresh_wait $fd] | |
291 | } | |
292 | } | |
293 | ||
294 | method _refresh_wait {fd} { | |
295 | read $fd | |
296 | if {[eof $fd]} { | |
297 | close $fd | |
298 | _readtree $this | |
299 | } | |
300 | } | |
301 | ||
302 | method _name {} { | |
303 | if {$new_ref eq {}} { | |
304 | return [string range $new_hash 0 7] | |
305 | } | |
306 | ||
307 | set rh refs/heads/ | |
308 | set rn [string length $rh] | |
309 | if {[string equal -length $rn $rh $new_ref]} { | |
310 | return [string range $new_ref $rn end] | |
311 | } else { | |
312 | return $new_ref | |
313 | } | |
314 | } | |
315 | ||
316 | method _readtree {} { | |
317 | global HEAD | |
318 | ||
b7922306 SP |
319 | set readtree_d {} |
320 | $::main_status start \ | |
321 | "Updating working directory to '[_name $this]'..." \ | |
322 | {files checked out} | |
323 | ||
0b812616 SP |
324 | set fd [git_read --stderr read-tree \ |
325 | -m \ | |
326 | -u \ | |
327 | -v \ | |
328 | --exclude-per-directory=.gitignore \ | |
329 | $HEAD \ | |
330 | $new_hash \ | |
331 | ] | |
d41b43eb SP |
332 | fconfigure $fd -blocking 0 -translation binary |
333 | fileevent $fd readable [cb _readtree_wait $fd] | |
334 | } | |
335 | ||
336 | method _readtree_wait {fd} { | |
b7922306 SP |
337 | global current_branch |
338 | ||
339 | set buf [read $fd] | |
340 | $::main_status update_meter $buf | |
341 | append readtree_d $buf | |
d41b43eb | 342 | |
d41b43eb SP |
343 | fconfigure $fd -blocking 1 |
344 | if {![eof $fd]} { | |
345 | fconfigure $fd -blocking 0 | |
346 | return | |
347 | } | |
348 | ||
b7922306 SP |
349 | if {[catch {close $fd}]} { |
350 | set err $readtree_d | |
d41b43eb | 351 | regsub {^fatal: } $err {} err |
b7922306 | 352 | $::main_status stop "Aborted checkout of '[_name $this]' (file level merging is required)." |
d41b43eb SP |
353 | warn_popup "File level merge required. |
354 | ||
355 | $err | |
356 | ||
357 | Staying on branch '$current_branch'." | |
d41b43eb SP |
358 | unlock_index |
359 | delete_this | |
360 | return | |
361 | } | |
362 | ||
b7922306 SP |
363 | $::main_status stop |
364 | _after_readtree $this | |
365 | } | |
366 | ||
367 | method _after_readtree {} { | |
368 | global selected_commit_type commit_type HEAD MERGE_HEAD PARENT | |
369 | global current_branch is_detached | |
370 | global ui_comm | |
371 | ||
372 | set name [_name $this] | |
d41b43eb SP |
373 | set log "checkout: moving" |
374 | if {!$is_detached} { | |
375 | append log " from $current_branch" | |
376 | } | |
377 | ||
378 | # -- Move/create HEAD as a symbolic ref. Core git does not | |
379 | # even check for failure here, it Just Works(tm). If it | |
380 | # doesn't we are in some really ugly state that is difficult | |
381 | # to recover from within git-gui. | |
382 | # | |
383 | set rh refs/heads/ | |
384 | set rn [string length $rh] | |
385 | if {[string equal -length $rn $rh $new_ref]} { | |
386 | set new_branch [string range $new_ref $rn end] | |
387 | append log " to $new_branch" | |
388 | ||
389 | if {[catch { | |
390 | git symbolic-ref -m $log HEAD $new_ref | |
391 | } err]} { | |
392 | _fatal $this $err | |
393 | } | |
394 | set current_branch $new_branch | |
395 | set is_detached 0 | |
396 | } else { | |
397 | append log " to $new_expr" | |
398 | ||
399 | if {[catch { | |
400 | _detach_HEAD $log $new_hash | |
401 | } err]} { | |
402 | _fatal $this $err | |
403 | } | |
404 | set current_branch HEAD | |
405 | set is_detached 1 | |
406 | } | |
407 | ||
408 | # -- We had to defer updating the branch itself until we | |
409 | # knew the working directory would update. So now we | |
410 | # need to finish that work. If it fails we're in big | |
411 | # trouble. | |
412 | # | |
413 | if {$update_old ne {}} { | |
414 | if {[catch { | |
415 | git update-ref \ | |
416 | -m $reflog_msg \ | |
417 | $new_ref \ | |
418 | $new_hash \ | |
419 | $update_old | |
420 | } err]} { | |
421 | _fatal $this $err | |
422 | } | |
423 | } | |
424 | ||
425 | if {$is_detached} { | |
426 | info_popup "You are no longer on a local branch. | |
427 | ||
428 | If you wanted to be on a branch, create one now starting from 'This Detached Checkout'." | |
429 | } | |
430 | ||
431 | # -- Update our repository state. If we were previously in | |
432 | # amend mode we need to toss the current buffer and do a | |
433 | # full rescan to update our file lists. If we weren't in | |
434 | # amend mode our file lists are accurate and we can avoid | |
435 | # the rescan. | |
436 | # | |
437 | unlock_index | |
438 | set selected_commit_type new | |
439 | if {[string match amend* $commit_type]} { | |
440 | $ui_comm delete 0.0 end | |
441 | $ui_comm edit reset | |
442 | $ui_comm edit modified false | |
443 | rescan [list ui_status "Checked out '$name'."] | |
444 | } else { | |
445 | repository_state commit_type HEAD MERGE_HEAD | |
446 | set PARENT $HEAD | |
447 | ui_status "Checked out '$name'." | |
448 | } | |
449 | delete_this | |
450 | } | |
451 | ||
452 | git-version proc _detach_HEAD {log new} { | |
453 | >= 1.5.3 { | |
454 | git update-ref --no-deref -m $log HEAD $new | |
455 | } | |
456 | default { | |
457 | set p [gitdir HEAD] | |
458 | file delete $p | |
459 | set fd [open $p w] | |
460 | fconfigure $fd -translation lf -encoding utf-8 | |
461 | puts $fd $new | |
462 | close $fd | |
463 | } | |
464 | } | |
465 | ||
466 | method _confirm_reset {cur} { | |
467 | set reset_ok 0 | |
468 | set name [_name $this] | |
469 | set gitk [list do_gitk [list $cur ^$new_hash]] | |
470 | ||
471 | _toplevel $this {Confirm Branch Reset} | |
472 | pack [label $w.msg1 \ | |
473 | -anchor w \ | |
474 | -justify left \ | |
475 | -text "Resetting '$name' to $new_expr will lose the following commits:" \ | |
476 | ] -anchor w | |
477 | ||
478 | set list $w.list.l | |
479 | frame $w.list | |
480 | text $list \ | |
481 | -font font_diff \ | |
482 | -width 80 \ | |
483 | -height 10 \ | |
484 | -wrap none \ | |
485 | -xscrollcommand [list $w.list.sbx set] \ | |
486 | -yscrollcommand [list $w.list.sby set] | |
487 | scrollbar $w.list.sbx -orient h -command [list $list xview] | |
488 | scrollbar $w.list.sby -orient v -command [list $list yview] | |
489 | pack $w.list.sbx -fill x -side bottom | |
490 | pack $w.list.sby -fill y -side right | |
491 | pack $list -fill both -expand 1 | |
492 | pack $w.list -fill both -expand 1 -padx 5 -pady 5 | |
493 | ||
494 | pack [label $w.msg2 \ | |
495 | -anchor w \ | |
496 | -justify left \ | |
497 | -text {Recovering lost commits may not be easy.} \ | |
498 | ] | |
499 | pack [label $w.msg3 \ | |
500 | -anchor w \ | |
501 | -justify left \ | |
502 | -text "Reset '$name'?" \ | |
503 | ] | |
504 | ||
505 | frame $w.buttons | |
506 | button $w.buttons.visualize \ | |
507 | -text Visualize \ | |
508 | -command $gitk | |
509 | pack $w.buttons.visualize -side left | |
510 | button $w.buttons.reset \ | |
511 | -text Reset \ | |
512 | -command " | |
513 | set @reset_ok 1 | |
514 | destroy $w | |
515 | " | |
516 | pack $w.buttons.reset -side right | |
517 | button $w.buttons.cancel \ | |
518 | -default active \ | |
519 | -text Cancel \ | |
520 | -command [list destroy $w] | |
521 | pack $w.buttons.cancel -side right -padx 5 | |
522 | pack $w.buttons -side bottom -fill x -pady 10 -padx 10 | |
523 | ||
0b812616 | 524 | set fd [git_read rev-list --pretty=oneline $cur ^$new_hash] |
d41b43eb SP |
525 | while {[gets $fd line] > 0} { |
526 | set abbr [string range $line 0 7] | |
527 | set subj [string range $line 41 end] | |
528 | $list insert end "$abbr $subj\n" | |
529 | } | |
530 | close $fd | |
531 | $list configure -state disabled | |
532 | ||
533 | bind $w <Key-v> $gitk | |
534 | bind $w <Visibility> " | |
535 | grab $w | |
536 | focus $w.buttons.cancel | |
537 | " | |
538 | bind $w <Key-Return> [list destroy $w] | |
539 | bind $w <Key-Escape> [list destroy $w] | |
540 | tkwait window $w | |
541 | return $reset_ok | |
542 | } | |
543 | ||
544 | method _error {msg} { | |
545 | if {[winfo ismapped $parent_w]} { | |
546 | set p $parent_w | |
547 | } else { | |
548 | set p . | |
549 | } | |
550 | ||
551 | tk_messageBox \ | |
552 | -icon error \ | |
553 | -type ok \ | |
554 | -title [wm title $p] \ | |
555 | -parent $p \ | |
556 | -message $msg | |
557 | } | |
558 | ||
559 | method _toplevel {title} { | |
560 | regsub -all {::} $this {__} w | |
561 | set w .$w | |
562 | ||
563 | if {[winfo ismapped $parent_w]} { | |
564 | set p $parent_w | |
565 | } else { | |
566 | set p . | |
567 | } | |
568 | ||
569 | toplevel $w | |
570 | wm title $w $title | |
571 | wm geometry $w "+[winfo rootx $p]+[winfo rooty $p]" | |
572 | } | |
573 | ||
574 | method _fatal {err} { | |
575 | error_popup "Failed to set current branch. | |
576 | ||
577 | This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file. | |
578 | ||
579 | This should not have occurred. [appname] will now close and give up. | |
580 | ||
581 | $err" | |
582 | exit 1 | |
583 | } | |
584 | ||
585 | } |