1 # git-gui index (add/remove) support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc _delete_indexlock
{} {
5 if {[catch {file delete
-- [gitdir index.lock
]} err
]} {
6 error_popup
[strcat
[mc
"Unable to unlock the index."] "\n\n$err"]
10 proc _close_updateindex
{fd
after} {
11 fconfigure $fd -blocking 1
12 if {[catch {close $fd} err
]} {
15 wm title
$w [strcat
"[appname] ([reponame]): " [mc
"Index Error"]]
16 wm geometry
$w "+[winfo rootx .]+[winfo rooty .]"
21 [mc
"Updating the Git index failed. A rescan will be automatically started to resynchronize git-gui."] \
26 button $w.buttons.
continue \
27 -text [mc
"Continue"] \
28 -command [list destroy $w]
29 pack $w.buttons.
continue -side right
-padx 5
30 button $w.buttons.unlock
\
31 -text [mc
"Unlock Index"] \
32 -command "destroy $w; _delete_indexlock"
33 pack $w.buttons.unlock
-side right
34 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
36 wm protocol
$w WM_DELETE_WINDOW
update
37 bind $w.buttons.
continue <Visibility
> "
39 focus $w.buttons.continue
53 proc update_indexinfo
{msg pathList
after} {
54 global update_index_cp
56 if {![lock_index
update]} return
59 set pathList
[lsort $pathList]
60 set totalCnt
[llength $pathList]
61 set batch
[expr {int
($totalCnt * .01) + 1}]
62 if {$batch > 25} {set batch
25}
65 "%s... %i/%i files (%.2f%%)" \
70 set fd
[git_write update-index
-z --index-info
]
77 fileevent $fd writable
[list \
78 write_update_indexinfo
\
88 proc write_update_indexinfo
{fd pathList totalCnt batch msg
after} {
89 global update_index_cp
90 global file_states current_diff_path
92 if {$update_index_cp >= $totalCnt} {
93 _close_updateindex
$fd $after
98 {$update_index_cp < $totalCnt && $i > 0} \
100 set path
[lindex $pathList $update_index_cp]
103 set s
$file_states($path)
104 switch -glob -- [lindex $s 0] {
111 set info [lindex $s 2]
112 if {$info eq
{}} continue
114 puts -nonewline $fd "$info\t[encoding convertto $path]\0"
115 display_file
$path $new
119 "%s... %i/%i files (%.2f%%)" \
123 [expr {100.0 * $update_index_cp / $totalCnt}]]
126 proc update_index
{msg pathList
after} {
127 global update_index_cp
129 if {![lock_index
update]} return
131 set update_index_cp
0
132 set pathList
[lsort $pathList]
133 set totalCnt
[llength $pathList]
134 set batch
[expr {int
($totalCnt * .01) + 1}]
135 if {$batch > 25} {set batch
25}
138 "%s... %i/%i files (%.2f%%)" \
143 set fd
[git_write update-index
--add --remove -z --stdin]
150 fileevent $fd writable
[list \
161 proc write_update_index
{fd pathList totalCnt batch msg
after} {
162 global update_index_cp
163 global file_states current_diff_path
165 if {$update_index_cp >= $totalCnt} {
166 _close_updateindex
$fd $after
171 {$update_index_cp < $totalCnt && $i > 0} \
173 set path
[lindex $pathList $update_index_cp]
176 switch -glob -- [lindex $file_states($path) 0] {
182 if {[file exists
$path]} {
191 puts -nonewline $fd "[encoding convertto $path]\0"
192 display_file
$path $new
196 "%s... %i/%i files (%.2f%%)" \
200 [expr {100.0 * $update_index_cp / $totalCnt}]]
203 proc checkout_index
{msg pathList
after} {
204 global update_index_cp
206 if {![lock_index
update]} return
208 set update_index_cp
0
209 set pathList
[lsort $pathList]
210 set totalCnt
[llength $pathList]
211 set batch
[expr {int
($totalCnt * .01) + 1}]
212 if {$batch > 25} {set batch
25}
215 "%s... %i/%i files (%.2f%%)" \
220 set fd
[git_write checkout-index
\
233 fileevent $fd writable
[list \
234 write_checkout_index
\
244 proc write_checkout_index
{fd pathList totalCnt batch msg
after} {
245 global update_index_cp
246 global file_states current_diff_path
248 if {$update_index_cp >= $totalCnt} {
249 _close_updateindex
$fd $after
254 {$update_index_cp < $totalCnt && $i > 0} \
256 set path
[lindex $pathList $update_index_cp]
258 switch -glob -- [lindex $file_states($path) 0] {
262 puts -nonewline $fd "[encoding convertto $path]\0"
263 display_file
$path ?_
269 "%s... %i/%i files (%.2f%%)" \
273 [expr {100.0 * $update_index_cp / $totalCnt}]]
276 proc unstage_helper
{txt paths
} {
277 global file_states current_diff_path
279 if {![lock_index begin-update
]} return
283 foreach path
$paths {
284 switch -glob -- [lindex $file_states($path) 0] {
288 lappend pathList
$path
289 if {$path eq
$current_diff_path} {
290 set after {reshow_diff
;}
295 if {$pathList eq
{}} {
301 [concat $after [list ui_ready
]]
305 proc do_unstage_selection
{} {
306 global current_diff_path selected_paths
308 if {[array size selected_paths
] > 0} {
310 {Unstaging selected files from commit
} \
311 [array names selected_paths
]
312 } elseif
{$current_diff_path ne
{}} {
314 "Unstaging [short_path $current_diff_path] from commit" \
315 [list $current_diff_path]
319 proc add_helper
{txt paths
} {
320 global file_states current_diff_path
322 if {![lock_index begin-update
]} return
326 foreach path
$paths {
327 switch -glob -- [lindex $file_states($path) 0] {
332 lappend pathList
$path
333 if {$path eq
$current_diff_path} {
334 set after {reshow_diff
;}
339 if {$pathList eq
{}} {
345 [concat $after {ui_status
{Ready to commit.
}}]
349 proc do_add_selection
{} {
350 global current_diff_path selected_paths
352 if {[array size selected_paths
] > 0} {
354 {Adding selected files
} \
355 [array names selected_paths
]
356 } elseif
{$current_diff_path ne
{}} {
358 "Adding [short_path $current_diff_path]" \
359 [list $current_diff_path]
367 foreach path
[array names file_states
] {
368 switch -glob -- [lindex $file_states($path) 0] {
371 ?D
{lappend paths
$path}
374 add_helper
{Adding all changed files
} $paths
377 proc revert_helper
{txt paths
} {
378 global file_states current_diff_path
380 if {![lock_index begin-update
]} return
384 foreach path
$paths {
385 switch -glob -- [lindex $file_states($path) 0] {
389 lappend pathList
$path
390 if {$path eq
$current_diff_path} {
391 set after {reshow_diff
;}
397 set n
[llength $pathList]
402 set s
"[short_path [lindex $pathList]]"
404 set s
"these $n files"
407 set reply
[tk_dialog \
409 "[appname] ([reponame])" \
410 "Revert changes in $s?
412 Any unstaged changes will be permanently lost by the revert." \
422 [concat $after [list ui_ready
]]
428 proc do_revert_selection
{} {
429 global current_diff_path selected_paths
431 if {[array size selected_paths
] > 0} {
433 {Reverting selected files
} \
434 [array names selected_paths
]
435 } elseif
{$current_diff_path ne
{}} {
437 "Reverting [short_path $current_diff_path]" \
438 [list $current_diff_path]
442 proc do_select_commit_type
{} {
443 global commit_type selected_commit_type
445 if {$selected_commit_type eq
{new
}
446 && [string match amend
* $commit_type]} {
448 } elseif
{$selected_commit_type eq
{amend
}
449 && ![string match amend
* $commit_type]} {
452 # The amend request was rejected...
454 if {![string match amend
* $commit_type]} {
455 set selected_commit_type new