1 # git-gui index (add/remove) support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc update_indexinfo {msg pathList after} {
7 if {![lock_index update]} return
10 set pathList [lsort $pathList]
11 set totalCnt [llength $pathList]
12 set batch [expr {int($totalCnt * .01) + 1}]
13 if {$batch > 25} {set batch 25}
16 "$msg... %i/%i files (%.2f%%)" \
20 set fd [git_write update-index -z --index-info]
27 fileevent $fd writable [list \
28 write_update_indexinfo \
38 proc write_update_indexinfo {fd pathList totalCnt batch msg after} {
39 global update_index_cp
40 global file_states current_diff_path
42 if {$update_index_cp >= $totalCnt} {
50 {$update_index_cp < $totalCnt && $i > 0} \
52 set path [lindex $pathList $update_index_cp]
55 set s $file_states($path)
56 switch -glob -- [lindex $s 0] {
63 set info [lindex $s 2]
64 if {$info eq {}} continue
66 puts -nonewline $fd "$info\t[encoding convertto $path]\0"
67 display_file $path $new
71 "$msg... %i/%i files (%.2f%%)" \
74 [expr {100.0 * $update_index_cp / $totalCnt}]]
77 proc update_index {msg pathList after} {
78 global update_index_cp
80 if {![lock_index update]} return
83 set pathList [lsort $pathList]
84 set totalCnt [llength $pathList]
85 set batch [expr {int($totalCnt * .01) + 1}]
86 if {$batch > 25} {set batch 25}
89 "$msg... %i/%i files (%.2f%%)" \
93 set fd [git_write update-index --add --remove -z --stdin]
100 fileevent $fd writable [list \
111 proc write_update_index {fd pathList totalCnt batch msg after} {
112 global update_index_cp
113 global file_states current_diff_path
115 if {$update_index_cp >= $totalCnt} {
123 {$update_index_cp < $totalCnt && $i > 0} \
125 set path [lindex $pathList $update_index_cp]
128 switch -glob -- [lindex $file_states($path) 0] {
134 if {[file exists $path]} {
143 puts -nonewline $fd "[encoding convertto $path]\0"
144 display_file $path $new
148 "$msg... %i/%i files (%.2f%%)" \
151 [expr {100.0 * $update_index_cp / $totalCnt}]]
154 proc checkout_index {msg pathList after} {
155 global update_index_cp
157 if {![lock_index update]} return
159 set update_index_cp 0
160 set pathList [lsort $pathList]
161 set totalCnt [llength $pathList]
162 set batch [expr {int($totalCnt * .01) + 1}]
163 if {$batch > 25} {set batch 25}
166 "$msg... %i/%i files (%.2f%%)" \
170 set fd [git_write checkout-index \
183 fileevent $fd writable [list \
184 write_checkout_index \
194 proc write_checkout_index {fd pathList totalCnt batch msg after} {
195 global update_index_cp
196 global file_states current_diff_path
198 if {$update_index_cp >= $totalCnt} {
206 {$update_index_cp < $totalCnt && $i > 0} \
208 set path [lindex $pathList $update_index_cp]
210 switch -glob -- [lindex $file_states($path) 0] {
214 puts -nonewline $fd "[encoding convertto $path]\0"
215 display_file $path ?_
221 "$msg... %i/%i files (%.2f%%)" \
224 [expr {100.0 * $update_index_cp / $totalCnt}]]
227 proc unstage_helper {txt paths} {
228 global file_states current_diff_path
230 if {![lock_index begin-update]} return
234 foreach path $paths {
235 switch -glob -- [lindex $file_states($path) 0] {
239 lappend pathList $path
240 if {$path eq $current_diff_path} {
241 set after {reshow_diff;}
246 if {$pathList eq {}} {
252 [concat $after [list ui_ready]]
256 proc do_unstage_selection {} {
257 global current_diff_path selected_paths
259 if {[array size selected_paths] > 0} {
261 {Unstaging selected files from commit} \
262 [array names selected_paths]
263 } elseif {$current_diff_path ne {}} {
265 "Unstaging [short_path $current_diff_path] from commit" \
266 [list $current_diff_path]
270 proc add_helper {txt paths} {
271 global file_states current_diff_path
273 if {![lock_index begin-update]} return
277 foreach path $paths {
278 switch -glob -- [lindex $file_states($path) 0] {
283 lappend pathList $path
284 if {$path eq $current_diff_path} {
285 set after {reshow_diff;}
290 if {$pathList eq {}} {
296 [concat $after {ui_status {Ready to commit.}}]
300 proc do_add_selection {} {
301 global current_diff_path selected_paths
303 if {[array size selected_paths] > 0} {
305 {Adding selected files} \
306 [array names selected_paths]
307 } elseif {$current_diff_path ne {}} {
309 "Adding [short_path $current_diff_path]" \
310 [list $current_diff_path]
318 foreach path [array names file_states] {
319 switch -glob -- [lindex $file_states($path) 0] {
322 ?D {lappend paths $path}
325 add_helper {Adding all changed files} $paths
328 proc revert_helper {txt paths} {
329 global file_states current_diff_path
331 if {![lock_index begin-update]} return
335 foreach path $paths {
336 switch -glob -- [lindex $file_states($path) 0] {
340 lappend pathList $path
341 if {$path eq $current_diff_path} {
342 set after {reshow_diff;}
348 set n [llength $pathList]
353 set s "[short_path [lindex $pathList]]"
355 set s "these $n files"
358 set reply [tk_dialog \
360 "[appname] ([reponame])" \
361 "Revert changes in $s?
363 Any unadded changes will be permanently lost by the revert." \
373 [concat $after [list ui_ready]]
379 proc do_revert_selection {} {
380 global current_diff_path selected_paths
382 if {[array size selected_paths] > 0} {
384 {Reverting selected files} \
385 [array names selected_paths]
386 } elseif {$current_diff_path ne {}} {
388 "Reverting [short_path $current_diff_path]" \
389 [list $current_diff_path]
393 proc do_select_commit_type {} {
394 global commit_type selected_commit_type
396 if {$selected_commit_type eq {new}
397 && [string match amend* $commit_type]} {
399 } elseif {$selected_commit_type eq {amend}
400 && ![string match amend* $commit_type]} {
403 # The amend request was rejected...
405 if {![string match amend* $commit_type]} {
406 set selected_commit_type new